Bijan
Bijan

Reputation: 6772

How do you determine the nested classes of a class?

In Ruby, how do you determine the nested classes of a class?

Upvotes: 2

Views: 173

Answers (1)

horseyguy
horseyguy

Reputation: 29895

Assuming you mean nested classes in the following sense:

class A
    class B; end
    class C; end
end

Where B and C are 'nested' within A then the following should work:

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

A.nested_classes =>  [A::B, A::C]

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

Upvotes: 2

Related Questions