Find classes available in the module

I have a module MyModule . I dynamically load classes into it. How can I get a list of classes defined in its namespace?

Example:

 def load_plugins Dir.glob(File.dirname(__FILE__) + '/plugins/*.rb') do |f| MyModule.class_eval File.read(f) end # now how can I find the new classes I've loaded into MyModule? end 

I have to say that every f contains something like "class Foo; end".

You can also think of it this way: in Rails, how could I programmatically find all the classes defined in the ActiveRecord module?

+55
ruby metaprogramming
May 7, '09 at 6:18
source share
1 answer

Classes are accessed through constants. Classes defined in the module are listed as constants in this module. Therefore, you just need to select the constants related to the classes.

 MyModule.constants.select {|c| MyModule.const_get(c).is_a? Class} 
+94
May 7 '09 at 6:36 a.m.
source share



All Articles