N> metaclass != singleton class
Well, it's well known : metaclass exist in Smalltalk, singleton class in
ruby. Nothing new
The difference's between Ruby and Smalltalk here are subtle, and
somewhat just terminology.
In both Smalltalk and Ruby, classes hold the methods of their
instances, and classes themselves must have a 'class' to hold any
class methods, which are really instance methods of the object which
represents the class.
The difference is that Ruby and Smalltalk factor things a little differently.
In Smalltalk all classes are ultimately subclasses of Class. If I
recall correctly the class Class provides the base methods for making
subclasses, since in Smalltak subclasses are created by sending a
message to the superclass. In Ruby subclass definition is done
syntactically as part of eval. In Smalltalk there is a class called
Metaclass, from which all of the classes of classes descend. The
common superclass of both Class and Metaclass is Behavior which
provides the methods involved with holding methods. To some extent
the Module class in Ruby serves some of the purposes of Behavior in
Smalltalk, although Smalltalk doesn't have the concepts of name
scoping and implementation mixing which the Ruby Module provides.
And touching on the theme of this particular thread, there's been some
confusion about just what super means. It's not really a 'reference'
to the superclass of self in a method, it's a 'reference' to the
superclass of the class which contains the method, which may well be a
superclass of the class of self. In both Smalltalk and Ruby, the
objects which contain methods i.e descendants of Behavior in
Smalltalk, or Module in Ruby, act as nodes in a tree which is searched
for methods when they are invoked. Normally when a message is sent to
an object, the search starts in that object's class, and goes up the
superclass chain until it is found, or if the chain runs out, the
bailout method is called instead, method_missing in Ruby, or
doesNotUnderstand: in Smalltalk. In both Ruby and Smalltalk, super is
a syntactic construct which causes the search to start just above the
point where the current method was found.
One aspect of Smalltalk which I miss in Ruby is that it provided
deeper reflection, not just at the class level, but also at the method
level. Smalltalk method objects can do things like enumerate the
messages they send, which is very useful in developing an IDE.
Matz is reluctant to call the class of a Ruby class a metaclass,
although it serves much of the same purpose in Ruby as a Smalltalk
Metaclass. Since Ruby already has singleton classes to provide
instance-specific behavior, another way of thinking about classes of
classes in Ruby is as singleton classes of classes, which seems to be
Matz's preferred way of viewing them. One difference is that instance
singletons aren't created until they are needed, while the class of a
class is created along with the class.
I'm pretty sure that the delay in creating a 'class' for instance
behavior is why they are called singleton classes, in effect a
singleton pattern is used to ensure that a given instance has at most
one class to hold it's instance specific behavior.
I had been thinking that the class of a class in Ruby could also be
lazily constructed, but in typing this note, I realized that they need
to be instantiated along with a new class because there needs to be a
place to start looking for methods when a message is sent to a class.
I guess an alternate implementation in Ruby might have been to make
the intial class of a new class, the class of it's first superclass
which had one, and then change that when class methods were added, but
I suspect that such an implementation would be messier than the simple
expedient of just creating an empty meta/singleton class.
Sorry for the long post. I've been working on something for my blog
on this very topic for about two weeks, and this chain of
consciousness just started pouring out.. I guess I'll store it away as
fodder for that article.