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

(Tuis.) #1

8 | Chapter 1: Foundational Techniques


The single-bordered box aroundobjrepresents a plain-old object instance. Note that
each box in this diagram is an object instance. However, the double-bordered boxes
represent objects that are instances of theClassclass (hence theirklasspointer
points to theClass object).


When we sendobj a message:


obj.to_s

this chain is followed:


1.obj’sklasspointer is followed toB;B’s methods (inm_tbl) are searched for a
matching method.


  1. No methods are found inB.B’ssuperpointer is followed, andAis searched for
    methods.

  2. No methods are found inA.A’ssuperpointer is followed, andObjectis searched
    for methods.

  3. TheObjectclass contains ato_smethod in native code (rb_any_to_s). This is
    invoked, yielding a value like"#". Therb_any_to_smethod exam-
    ines the receiver’sklasspointer to determine what class name to display; there-
    fore,B is shown even though the method invoked resides inObject.


Including modules


Things get more complicated when we start mixing in modules. Ruby handles mod-
ule inclusion with ICLASSes,*which are proxies for modules. When you include a


Figure 1-3. Class instantiation



  • ICLASS is Mauricio Fernández’s term for these proxy classes. They have no official name but are of type
    T_ICLASS in the Ruby source.


Object

A

super

B

super

obj klass
Free download pdf