Prev | Current Page 22 | Next

Brad Ediger

"Advanced Rails"


4. The Object class contains a to_s method in native code (rb_any_to_s). This is
invoked, yielding a value like "#". The rb_any_to_s method examines
the receiver??™s klass pointer to determine what class name to display; therefore,
B is shown even though the method invoked resides in Object.
Including modules
Things get more complicated when we start mixing in modules. Ruby handles module
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
Ruby Foundations | 9
module into a class, Ruby inserts an ICLASS representing the included module into
the including class object??™s super chain.
For our module inclusion example, let??™s simplify things a bit by ignoring B for now.
We define a module and mix it in to A, which results in data structures shown in
Figure 1-4:
module Mixin
def mixed_method
puts "Hello from mixin"
end
end
class A
include Mixin
end
Here is where the ICLASS comes into play. The super link pointing from A to Object
is intercepted by a new ICLASS (represented by the box with the dashed line). The
ICLASS is a proxy for the Mixin module. It contains pointers to Mixin??™s iv_tbl
(instance variables) and m_tbl (methods).


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