Skip to content

Instantly share code, notes, and snippets.

@keithrbennett
Created October 28, 2022 21:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keithrbennett/6df41096513a145b49c1b76714d920ac to your computer and use it in GitHub Desktop.
Save keithrbennett/6df41096513a145b49c1b76714d920ac to your computer and use it in GitHub Desktop.
def modules_including_module(target_module)
ObjectSpace.each_object(Module).select do |object|
# All Class'es are modules, but we are not interested in them, so we exclude them.
!object.is_a?(Class) \
&& \
object.ancestors.include?(target_module) \
&& \
!object.equal?(target_module)
end
end
module_function :modules_including_module
def classes_including_modules(modules)
ObjectSpace.each_object(Class).select do |klass|
(klass.ancestors & modules).any?
end
end
module_function :classes_including_modules
def classes_not_having_ancestor(classes, ancestor)
classes.select do |klass|
!klass.ancestors.include?(ancestor)
end
end
module_function :classes_not_having_ancestor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment