Find classes available in a Module
Asked Answered
T

3

83

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?

Transmissible answered 7/5, 2009 at 6:18 Comment(0)
C
148

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}
Cryptozoic answered 7/5, 2009 at 6:36 Comment(6)
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.Transmissible
No compelling reason. The === version was just more readable for me. Using is_a? would work just as well.Cryptozoic
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]Cohe
Don't know why but this doesn't work on Ruby built-in modules like Kernel, Comparable and Enumerable.Nonstandard
@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.Cryptozoic
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? @CryptozoicSpikelet
L
5

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]**
Leeth answered 6/11, 2019 at 14:41 Comment(0)
W
2

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

Weissman answered 8/4, 2021 at 14:22 Comment(1)
Great code thx. It could use a seen kwarg to avoid infinite loops, and protection against exceptions from const_get.Gilead

© 2022 - 2024 — McMap. All rights reserved.