6 | Chapter 1: Foundational Techniques
- Ruby follows the receiver’sklasspointer and searches them_tblof that class
object for a matching method. (The target of aklasspointer will always be a
class object.) - If no method is found, Ruby follows that class object’ssuperpointer and contin-
ues the search in the superclass’sm_tbl. - Ruby progresses in this manner until the method is found or the top of thesuper
chain is reached. - If the method is not found in any object on the chain, Ruby invokesmethod_
missingon the receiver of the original method. This starts the process over again,
this time looking formethod_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 struc-
ture of theklass andsuper 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 sim-
plest 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 whoseklasspointer
points to theClassobject.A’ssuperpointer refers to theObjectclass object, indicat-
ing thatAinherits fromObject. For clarity, from now on we will omit defaultklass
pointers toClass,Module, andObject where there is no ambiguity.
Figure 1-1. Data structures for a single class
Object
A
super Class
klass
klass
klass