Message Passing Through Module Composition
suggest changeRuby 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
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents