I usually don't disagree with my younger friend Jim, but...
In general, although in some languages the implementation of
metaclasses is involved with creating other classes, the more general
meaning is that a metaclass is a class whose instance(s) are classes.
Granted Ruby is a bit sneaky in this regard since the class METHOD
sometimes hides the klass linkage, but if you look at ruby
documentation such as
Class: Class (Ruby 1.8.7) you'll see that
it talks about multiple metaclasses, and although it point's out that
the vertical lines in the diagram represent inheritance, it leaves the
description of the horizontal lines, which link Object to (Object),
OtherClass to (OtherClass) etc. These are of course the klass
pointers, and despite the smoke thrown by the class method which skips
over 'virtual/singleton' classes,
It might be of interest to note that early Smalltalk versions had only
one metaclass and it was Class, but this was limiting because it meant
that all classes had to have the same message protocol, since the
metaclass is where the method dictionary used to find methods for a
class lives, much as the String class is where the method dictionary
used to find methods for instances of strings is stored (of course I'm
simplifying a bit here, these are really where the first method
dictionary consulted is found before looking for inherited methods).
So, in Smalltalk at least, the first step in making Metaclasses more
'First class' was to allow each class to have its own metaclass,
And in Smalltalk metaclasses don't create other classes, much as in
Ruby, when a class is created, by sending it the subclass:... method,
a metaclass for the new class is created automatically, so it's the
other way around really.
The real role of a metaclass is to serve as the class of its sole
class instance, much like (singleton) classes serve as the class of
their instance or instances.
Now Smalltalk-80 makes Metaclasses more apparent, and factors things
by Having a class Hierarchy like this
Object
Behavior - provides the minimal state necessary for objects that
have instances.
It is really the interface with the VM. For
Smalltalk this is where the
instance variables for the method dictionary, the
superclass link,
and the form of instances (Smalltalk instances can
contain either
a sequence of either object pointers, bits, bytes or words,
and this sequence may be fixed or variable in length.
ClassDescription - this is the interface to the IDE and the compiler,
it provides instance variables for naming the 'Class',
naming the instance variables, and for a comment
shown in the browser
Class - adds support for things like class variable names,
and shared pool dictionaries which provide for something
like Module scoped names in Ruby
Metaclass - primarily this adds the behavior needed to create a new
Metaclass, initialize it and link it into the
parallel inheritance hierarchy.
Now Ruby doesn't have an equivalent for ClassDescription, and might
not have a class named Metaclass, but if you account for that, the
inheritance/instance-of diagram in the official document of Class
looks very much like the same diagram for Smalltalk with the
appropriate removals.
The real difference is that Ruby makes an attempt to hide the "Man
behind the curtains." and Smalltalk makes the "wizard" visible, and
refactors him into multiple bits.
···
On Tue, Nov 17, 2009 at 6:30 AM, David A. Black <dblack@rubypal.com> wrote:
Actually, I think it was Jim Weirich who pointed out that, given the
usual sense of the term "metaclass" as a class which produces other
classes, Class itself is the only metaclass in Ruby. The term is
sometimes used to refer to singleton classes in general, and sometimes
to refer only to singleton classes of Class objects:
--
Rick DeNatale
Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale