How would you list out the modules that have been included in a specific class in a class hierarchy in Ruby? Something like this:
module SomeModule
end
class ParentModel < Object
include SomeModule
end
class ChildModel < ParentModel
end
p ChildModel.included_modules #=> [SomeModule]
p ChildModel.included_modules(false) #=> []
Listing the ancestors makes the module appear higher in the tree:
p ChildModel.ancestors #=> [ChildModel, ParentModel, SomeModule, Object, Kernel]
As far as I understand your question, something like this is what you are looking for:
However, I don’t fully understand your testcases: why is
SomeModulelisted as an included module ofChildModeleven though it isn’t actually included inChildModelbut inParentModel? And conversely, why isKernelnot listed as an included module, even though it is just as much in the ancestors chain asSomeModule? And what does the boolean argument to the method mean?(Note that boolean arguments are always bad design: a method should do exactly one thing. If it takes a boolean argument, it does by definition two things, one if the argument is true, another is the argument is false. Or, if it does only one thing, then this can only mean that it ignores its argument, in which case it shouldn’t take it to begin with.)