Method: Module#+

Defined in:
lib/core/facets/module/op.rb

#+(other) ⇒ Object

Combine modules.

module APlus
  def a; "a"; end
end

module BPlus
  def b; "b"; end
end

CPlus = APlus + BPlus

class XPlus; include CPlus; end

XPlus.new.a    #=> "a"
XPlus.new.b    #=> "b"

Note that in the old version of traits.rb we cloned modules and altered their copies…

def +(other)
  mod1 = other.clone
  mod2 = clone
  mod1.module_eval{ include mod2 }
end

Later it was realized that this thwarted the main benefit that Ruby’s concept of modules has over traditional traits, inheritance.

CREDIT: Thomas Sawyer, Robert Dober



35
36
37
38
39
40
41
# File 'lib/core/facets/module/op.rb', line 35

def +(other)
  base = self
  Module.new do
    include base
    include other
  end
end