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
2010-08-24 14:38:05
Thank you for the answer.
Bijan
2010-08-24 18:20:41