Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby: get a list of extended modules?

Tags:

When you include modules in a class or other module you can call

@mymod.included_modules 

to get a list of modules 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] 
like image 375
Mario Avatar asked Mar 04 '11 17:03

Mario


2 Answers

Feature2.singleton_class.included_modules # => [Feature1, ...] 
like image 184
Damien Avatar answered Jan 17 '23 17:01

Damien


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] 
like image 43
pilcrow Avatar answered Jan 17 '23 17:01

pilcrow