Prev | Current Page 26 | Next

Brad Ediger

"Advanced Rails"

When we ask Ruby for objA??™s class, it traverses
the klass and super pointers up the hierarchy until it finds the first nonvirtual class.
Figure 1-8. Singleton class of an object
Singleton Class Terminology
The term metaclass is not particularly accurate when applied to singleton classes. Calling
a class ???meta??? implies that it is somehow more abstract than an ordinary class. This
is not the case; singleton classes are simply classes that belong to a particular instance.
True metaclasses are found in languages such as Smalltalk that have a rich metaobject
protocol. Smalltalk??™s metaclasses are classes whose instances are classes. By parallel,
Ruby??™s only metaclass is Class, because all Ruby classes are instances of Class.
A somewhat popular alternate term for a singleton class is eigenclass, from the German
eigen (???its own???). An object??™s singleton class is its eigenclass (its own class).
Object
A
super
Class:objA
(virtual)
super
objA klass
objB klass
14 | Chapter 1: Foundational Techniques
Therefore, it tells us that objA??™s class is A. This is important to remember: an object??™s
class (from Ruby??™s perspective) may not match the object pointed to by klass.
Singleton classes are called singleton for a reason: there can only be one singleton
class per object. Therefore, we can refer unambiguously to ???objA??™s singleton class??? or
Class:objA.


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