How do I get the list of all classes that are defined in a module
(from inside a Ruby program)?
My current solution is using ObjectSpace.each_object(Class), which
gets every class defined in the whole program, but then I have to
filter it to get just the classes in a particular module, and this
doesn’t seem like the right way of doing it.
Hi,
In mail “Getting list of classes in a module?”
···
Philip Mak pmak@aaanime.net wrote:
How do I get the list of all classes that are defined in a module
(from inside a Ruby program)?
Try this:
def classes_under( mod )
mod.constants.map {|name| mod.const_get(name) }.select {|v| Class === v }
end
classes_under(YourFavoriteModule)
– Minero Aoki
Tokomar Reid wrote:
Philip Mak wrote:
How do I get the list of all classes that are defined in a module
(from inside a Ruby program)?
My current solution is using ObjectSpace.each_object(Class), which
gets every class defined in the whole program, but then I have to
filter it to get just the classes in a particular module, and this
doesn’t seem like the right way of doing it.
import inspect
Major apologies. I forgot to check the name of the newsgroup before
posting. That’s Python.
···
–
Tokomar
Try this:
def classes_under( mod )
mod.constants.map {|name| mod.const_get(name) }.select {|v| Class === v }
end
classes_under(YourFavoriteModule)
Thanks! I ended up doing this, which is essentially the same but a bit
simpler:
def classes_under(mod)
mod.constants.select{|v| mod.const_get(v).type == Class}
end
by the way (only slightly off topic) is there a difference between the
methods Object#type and Object#class ?
···
On Mon, 2002-08-26 at 21:54, Philip Mak wrote:
Try this:
def classes_under( mod )
mod.constants.map {|name| mod.const_get(name) }.select {|v| Class === v }
end
classes_under(YourFavoriteModule)
Thanks! I ended up doing this, which is essentially the same but a bit
simpler:
def classes_under(mod)
mod.constants.select{|v| mod.const_get(v).type == Class}
end
–
~transami