Created
October 28, 2022 21:03
-
-
Save keithrbennett/6df41096513a145b49c1b76714d920ac to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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