views:

57

answers:

1

This is what I've got, but it also finds classes and other constants.. is there a better way?

class Module
    def children
        constants.collect { |c| const_get(c) }.compact
    end
end
+3  A: 

By 'children' you seem to mean modules nested under a given module, right? Nothing to do with inheritance relationships?

Assuming you just mean nested modules, the following should work:

class Module
    def children
        constants.collect { |c| const_get(c) }.
            select { |m| m.instance_of?(Module) }
    end
end

EDIT: You may need to use constants(false) to prevent constant look-up on modules further up the inheritance chain.

banister