Metaclass - in Ruby

In Ruby

Ruby purifies the Smalltalk-80 concept of metaclasses by introducing eigenclasses and (un)redefining the class-of map. In particular, there is no Metaclass class in Ruby and the class of every non-terminal object is constantly the Class class.

The change can be schematized as follows (note that we are using terminology in which classes and metaclasses form disjoint sets) :

Smalltalk-80 Ruby
Classes Metaclasses
Terminal
objects

Classes Eigenclasses of
classes
Terminal
objects
Eigenclasses of
terminal objects

The Smalltalk-80 class-of map is treated as a hidden "actualclass" map and is provided in its decomposition into the eigenclass-of map and the (real) class-of map (which are available via the singleton_class and the class methods, respectively). The maps can be briefly described as follows:

  • The eigenclass of a terminal object x provides "singleton methods" for x and does not have any correspondent in Smalltalk-80. In most cases, there is no need to define singleton methods, so that the eigenclass of x exists only conceptually and is not allocated.
  • The eigenclass of a class x is the metaclass for x in the Smalltalk-80 sense (but also might exist only conceptually).
  • The class of a terminal object x is equal to "the-class-of" x in the Smalltalk-80 sense.
  • The class of anything else (i.e. a class or an eigenclass) is the Class class.

In this description, the eigenclass map is presented in its restriction to primary objects (classes and terminal objects). But the concept of eigenclasses is uniform: there are also eigenclasses of eigenclasses, their eigenclasses, and so on, constituting infinite chains, thus establishing infinite regress. The "higher order" eigenclasses usually exist purely conceptually – they do not contain any methods or store any (other) data in most Ruby programs.

The decomposition purifies the actualclass map in the sense that the eigenclass map is one-to-one and the (real) class map is many-to-one, without any inherent one-to-one part.

In accordance to the alternative definition, Ruby contains exactly one explicit metaclass: the Class class which is the metaclass root. Creation of subclasses of this class is disallowed. Implicit metaclasses are then all eigenclasses that are descendants of the metaclass root, in particular, eigenclasses of classes.

Read more about this topic:  Metaclass

Famous quotes containing the word ruby:

    Thy great leaves enfold
    The ancient beards, the helms of ruby and gold
    Of the crowned Magi; and the king whose eyes
    Saw the Pierced Hands and Rood of elder rise
    In Druid vapour and make the torches dim....
    William Butler Yeats (1865–1939)

    It is better to be looked over than overlooked.
    Mae West, U.S. actor, screenwriter, and Leo McCarey. Ruby Carter (Mae West)