views:

70

answers:

1

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

+1  A: 

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.

banister
Thank you for the answer.
Bijan