tags:

views:

36

answers:

2

Hi, I have an Foo module, that it is namespace for many classes ie: Foo::Bar, Foo::Baz an so on. Is there an way to return all class names namespaced by Foo?

+5  A: 

Foo.constants returns all constants in Foo. This includes, but is not limited to, classnames. If you want only class names, you can use Foo.constants.select {|c| Foo.const_get(c).is_a? Class}. If you want class and module names, you can use is_a? Module instead of is_a? Class.

sepp2k
This is a great answer. You sort of forget sometimes that constants in ruby are anything that _starts_ with a capital, so class names are constant instances of type class. +1
Matt Briggs
+1  A: 

If, instead of the names of the constants, you want the classes themselves, you could do it like this:

Foo.constants.map(&Foo.method(:const_get)).grep(Class)
Jörg W Mittag
I *love* point-free programming. Is my Haskell showing through?
Jörg W Mittag