Best way to find kids module?

This is what I have, 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
source share
1 answer

By "kids" you seem to mean modules nested in this module, right? Nothing to do with inheritance relationships?

Assuming that you mean only 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)it to prevent the modules from constantly searching further down the inheritance chain.

+6
source

Source: https://habr.com/ru/post/1743999/


All Articles