Message Passing Through Module Composition

suggest change

Ruby walks the ancestor chain of an object. That chain can contain both modules and classes. The same rules apply to modules.

class Example
end

module Prepended
  def initialize *args
    return super :default if args.empty?
    super
  end
end

module FirstIncluded
  def foo
    :first
  end
end

module SecondIncluded
  def foo
    :second
  end
end

class SubExample < Example
  prepend Prepended
  include FirstIncluded
  include SecondIncluded

  def initialize data = :subexample
    puts data
  end
end

SubExample.ancestors # => [Prepended, SubExample, SecondIncluded, FirstIncluded, Example, Object, Kernel, BasicObject]

s = SubExample.new # prints: default
s.foo # => :second

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents