Module: CoreExt::Module::RetroactiveModuleInclusion

Defined in:
lib/retroactive_module_inclusion.rb

Instance Method Summary collapse

Instance Method Details

#retroactively_include(mod) ⇒ Object

Includes mod retroactively, i.e., extending to all classes and modules which had included self beforehand.

Examples:

Retroactively include a module in Enumerable.


module Stats
  def mean
    inject(&:+) / count.to_f
  end
end

Enumerable.retroactively_include Stats

(1..2).mean  #=>  1.5

Returns:

  • self

Raises:

  • (TypeError)


22
23
24
25
26
27
28
29
30
# File 'lib/retroactive_module_inclusion.rb', line 22

def retroactively_include(mod)
  raise TypeError, "wrong argument type #{mod.class} (expected Module)" unless mod.is_a? ::Module # ::Module would in general be equivalent to Object::Module and simply Module would mean CoreExt::Module in this context
  
  pseudo_descendants.each do |pd|
    pd.module_eval { include mod }
  end
  
  self
end