Ruby: get a list of extended modules?
When you include modules in a class or other module you can call
@mymod.included_modules
to get a list of modu开发者_JS百科les included.
Is there an equivalent for listing the modules a module was extended by?
module Feature1
end
module Feature2
extend Feature1
end
Feature2.extended_modules #=> [Feature1]
Feature2.singleton_class.included_modules # => [Feature1, ...]
They're there, you just have to look in the right place:
(class << Feature2; self end).included_modules # [Feature1, Kernel]
We can generalize like this:
class Module
# Return any modules we +extend+
def extended_modules
(class << self; self end).included_modules
end
end
# Now get those extended modules peculiar to Feature2
Feature2.extended_modules - Module.extended_modules # [Feature1]
All Ruby modules can be listed from CLI (Command Line), itself as follows:
ruby -e 'puts Gem::Specification.all().map{|g| [g.name, g.version.to_s] }'
OR
ruby -rubygems -e 'puts Gem::Specification.all().map{|g| [g.name, g.version.to_s] }'
Hope this helps to some extent!!!
精彩评论