(The target of a klass pointer will always be a
class object.)
2. If no method is found, Ruby follows that class object??™s super pointer and continues
the search in the superclass??™s m_tbl.
3. Ruby progresses in this manner until the method is found or the top of the super
chain is reached.
4. If the method is not found in any object on the chain, Ruby invokes method_
missing on the receiver of the original method. This starts the process over again,
this time looking for method_missing rather than the original method.
These rules apply universally. All of the interesting things that method lookup
involves (mixins, class methods, and singleton classes) are consequences of the structure
of the klass and super pointers. We will now examine this process in detail.
Class inheritance
The method lookup process can be confusing, so we??™ll start simple. Here is the simplest
possible class definition in Ruby:
class A
end
This code generates the following data structures in memory (see Figure 1-1).
The double-bordered boxes represent class objects??”objects whose klass pointer
points to the Class object. A??™s super pointer refers to the Object class object, indicating
that A inherits from Object. For clarity, from now on we will omit default klass
pointers to Class, Module, and Object where there is no ambiguity.
Figure 1-1. Data structures for a single class
Object
A
Class super
klass
klass
klass
Ruby Foundations | 7
The next-simplest case is inheritance from one class.
Pages:
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32