Prev | Current Page 23 | Next

Brad Ediger

"Advanced Rails"


From this diagram, it is easy to see why we need proxy classes: the same module may
be mixed in to any number of different classes??”classes that may inherit from different
classes (thus having different super pointers). We could not directly insert Mixin
into the lookup chain, because its super pointer would have to point to two different
things if it were mixed in to two classes with different parents.
When we instantiate A, the structures are as shown in Figure 1-5:
objA = A.new
Figure 1-4. Inclusion of a module into the lookup chain
Object
Mixin
super
A
super
Mixin klass
10 | Chapter 1: Foundational Techniques
We invoke the mixed_method method from the mixin, with objA as the receiver:
objA.mixed_method
# >> Hello from mixin
The following method-lookup process takes place:
1. objA??™s class, A, is searched for a matching method. None is found.
2. A??™s super pointer is followed to the ICLASS that proxies Mixin. This proxy object
is searched for a matching method. Because the proxy??™s m_tbl is the same as
Mixin??™s m_tbl, the mixed_method method is found and invoked.
Many languages with multiple inheritance suffer from the diamond problem, which is
ambiguity in resolving method calls on objects whose classes have a diamond-shaped
inheritance graph, as shown in Figure 1-6.
Given this diagram, if an object of class D calls a method defined in class A that has
been overridden in both B and C, there is ambiguity about which method should be
called.


Pages:
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35