Advanced Rails - Building Industrial-Strength Web Apps in Record Time

(Tuis.) #1
Ruby Foundations | 11

module B
include A
def hello
"Hello from B"
end
end

module C
include A
def hello
"Hello from C"
end
end

class D
include B
include C
end

D.new.hello # => "Hello from C"

And if we change the order of inclusion, the result changes correspondingly:


class D
include C
include B
end

D.new.hello # => "Hello from B"

In this last example, whereBis included last, the object graph looks like Figure 1-7
(for simplicity, pointers toObject andClass have been elided).


Figure 1-6. The diamond problem of multiple inheritance


A

BC

D
Free download pdf