Getting a list of classes that include a module
module MyMod; end
class A; include MyMod; end
class B < A; end
class C; end
ObjectSpace.each_object(Class).select { |c| c.included_modules.include? MyMod }
#=> [B, A]
See ObjectSpace::
each_object.
Trying to find all classes that include a module
[Note: @engineersmnky illustrates a way to do this using flat_map
eliminating the need for the matching_classes
parameter. I found it more difficult to understand, but it's a perfectly sound use of flat_map
and a worthy solution. The code is posted at https://repl.it/@engineersmnky/IllinformedMountainousAnkole ]
The following code uses recursion to descend the inverted tree of modules. The result (printed at the very end) is correct, and includes classes in two modules. (I've coded a minimal module and class hierarchy to use as an example.)
#!/usr/bin/env ruby
module ParentModule
module OtherModule; end
class ParentClassYes; include OtherModule; end
class ParentClassNo; end
module ChildModule
class ChildClassYes; include OtherModule; end
class ChildClassNo; end
end
end
def classes_for_module_tree(the_module, matching_classes = [])
the_module.constants.each_with_object(matching_classes) \
do |const, matching_classes|
value = the_module.const_get(const)
if value.is_a?(Class)
if value.included_modules.include?(ParentModule::OtherModule)
matching_classes << value
end
elsif value.is_a?(Module)
# Here is where we call this method recursively. We suspend collecting
# matches for this module, call the method to process the newly found
# (sub)module, then use the array returned by that invocation to resume
# processing this module.
matching_classes = classes_for_module_tree(value, matching_classes)
end
end
matching_classes
end
p classes_for_module_tree(ParentModule)
# prints: [ParentModule::ParentClassYes, ParentModule::ChildModule::ChildClassYes]
How can I get a list of all classes within current module in Python?
Try this:
import sys
current_module = sys.modules[__name__]
In your context:
import sys, inspect
def print_classes():
for name, obj in inspect.getmembers(sys.modules[__name__]):
if inspect.isclass(obj):
print(obj)
And even better:
clsmembers = inspect.getmembers(sys.modules[__name__], inspect.isclass)
Because inspect.getmembers()
takes a predicate.
How to find the modules or classes that include a module?
To monitor when a module is included in another module or a class, use the included
hook:
module Parent
class << self
attr_reader :includers
end
def self.included(base)
@includers ||= []
@includers << base.name
end
end
Import a list of classes from a module
class_list = ['x', 'y', 'z', 'zz', 'zzz']
for c in class_list:
exec('from my_module import ' + c)
How to find all the classes that have included a module
Well, I've figured out how to find at least the name of all the classes.
Module.constants.select { |c| (eval c.to_s).is_a?(Class) && (eval c.to_s).include?(Nameable)}
Don't know if it's the best way to do it though.
Find classes available in a Module
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}
Related Topics
Error Install Rubyracer with Error "Invalid Gem: Package Is Corrupt"
How to Give a Sub-Module the Same Name as a Top-Level Class
Rails ( Set_No_Cache Method) Cannot Disable Browser Caching in Safari and Opera
How to Get the Final Url After Redirects Using Ruby
Removing or Overriding an Activerecord Validation Added by a Superclass or Mixin
Scanning for Unicode Numbers in a String with \D
Tcp Socket Communication Between Processes on Heroku Worker Dyno
How to Get the Array Index or Iteration Number with an Each Iterator
Automatically Open a File as Binary with Ruby
How to Do String Comparison in Ruby