81

I have a module MyModule. I dynamically load classes into it. How can I get a list of the classes defined within 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 should say that each f contains something like "class Foo; end".

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

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
joshuaxls
  • 950
  • 1
  • 7
  • 11

3 Answers3

143

Classes are accessed through constants. Classes defined within a module are listed as constants in that module. So you just need to choose the constants that refer to classes.

MyModule.constants.select {|c| MyModule.const_get(c).is_a? Class}
ggorlen
  • 44,755
  • 7
  • 76
  • 106
Chuck
  • 234,037
  • 30
  • 302
  • 389
  • 7
    One thing: why do you use that test instead of "MyModule.const_get(c).is_a? Class"? I'm not familiar with using "===" like that. – joshuaxls May 07 '09 at 06:57
  • 3
    No compelling reason. The === version was just more readable for me. Using is_a? would work just as well. – Chuck May 07 '09 at 07:04
  • 6
    _Avoid explicit use of the case equality operator `===`. As its name implies it is meant to be used implicitly by `case` expressions and outside of them it yields some pretty confusing code._ [[Style Guide](https://github.com/bbatsov/ruby-style-guide#no-case-equality)] – James Fernandes Feb 10 '16 at 19:50
  • Don't know why but this doesn't work on Ruby built-in modules like Kernel, Comparable and Enumerable. – vishless Dec 06 '16 at 11:28
  • 1
    @Vizkrig: AFAIK those modules aren't supposed to have any classes in them. If you're looking for classes that *include* a module, rather than classes that *are in* a module, that's a [different question](https://stackoverflow.com/questions/3917857/getting-a-list-of-classes-that-include-a-module). – Chuck Dec 15 '16 at 23:56
  • This does not work for me in rails (i have rails 6.0.0 and ruby v 2.6.3). I have a class `DeleteInvalidData` inside a module `Maintenance` stored in my serices folder `services > maintenance` but get an empty array back. Any ideas why? @Chuck – dcts Oct 29 '19 at 09:35
4

If you're on rails, you need to access the constants first for them to show up, because they're lazyly loaded.

MyModule::NotAClass = "not a class"

MyModule.constants => [:NotAClass]

MyModule::AClass => :AClass # Access class for it to be autoloaded

MyModule.constants => [:AClass, :NotAClass]

# Now, select the constants which are class instances

MyModule.constants
        .map(&MyModule.method(:const_get))
        .select { |constant| constant.is_a? Class} 

 => [MyModule::AClass]**
cesartalves
  • 1,507
  • 9
  • 18
1

If you'd like to get all classes in a module recursively, you can do something like

def get_classes(mod)
  mod.constants.map do |c|
    const = mod.const_get(c)
    if const.is_a? Class
      const
    elsif const.is_a? Module
      get_classes(const)
    else
      next
    end
  end.flatten
end

Then, given some module structure like:


module MyModule
  module Submodule1
    class Class1
    end
  end

  module Submodule2
    class Class2
    end
  end
end

the output looks like:

puts get_classes(MyModule)

# => MyModule::Submodule1::Class1
# => MyModule::Submodule2::Class2

blaedj
  • 322
  • 2
  • 9
  • Great code thx. It could use a seen kwarg to avoid infinite loops, and protection against exceptions from const_get. – Vajk Hermecz May 26 '21 at 10:40