On 6/2/07, Dorren <dorrenchen / gmail.com> wrote:
> Q1: in pick axe book page 364, it talks about metaclass should be a
> singleton class. but when I run the following code,  class's object_id
> are the same for all instance, but the metaclass object_id are
> different for each object instance, is the code wrong or what?

Here we are talking about singleton classes used to provide instance
specific behavior.

Singleton here means that the singleton class has only one instance.
So each instance which needs one, has its own singleton class.

> Q2: from pick axe book 364-367, it used singleton, metaclass, and
> virtual class definition interchangeably. are they all the same thing
> or not?

No they arent.

Singleton classes are classes which are marked internally with a flag
bit called FL_SINGLETON.  You don't normally see these classes unless
you use the class <<self;self;end trick.

Metaclass isn't really a Ruby term, it's a term from Smalltalk in
which the classes of class objects were instances of a class named
Metaclass.  Although Ruby class object do indeed have classes, they ar
not explicit metaclasses.  Instead the Ruby implementation uses
classes marked as singletons for this purpose. In the pickaxe, Matz is
quoted as saying that instead of metaclasses, Ruby has singleton
classes of classes which "behave just like Smalltalk's metaclasses."

Why do class objects need classes?  Classes provide a place to store a
hash of method names to methods for their instances.  So when a method
of a particular object is invoked, or equivalently as us old
Smalltalkers like to say a message is sent to an object, the search
for the corresponding method starts with the objects class then up a
chain of that class' superclass etc.

Virtual classes don't appear in the current ruby implementation, maybe
they did when the pickaxe was written, maybe not. I think that the
most interesting thing Matz has to say in the Pickaxe is:

  "IN THE CURRENT IMPLEMENTATION (emphasis mine) singleton classes are
 specially flagged class objects between objects and their class.
These can be called "virtual" classes if the language implementer
chooses."

If you look at the current (1.8.5-1.9) implementation there are two
kinds of  class-like objects which are "between objects and their
class." The first are classes marked with FL_SINGLETON which I've been
talking about, the second are internal objects of type T_ICLASS which
represent modules included by a Module/Class or extended by an object.
 So I interpret the pickaxe term 'virtual class' to mean either a
singleton class or one of these T_ICLASS module proxies.

> Q3: from page 365,
>   "To handle per-object attributes, Ruby provides a classlike some-
>     thing for each object that is sometimes called a singleton class."
> if runtime create something new for EACH object, how can it be called
> singleton?

This is really the same question as Q1.

This relates to an earlier thread here from last week. I just
converted my reply to that thread to a blog entry at:
http://talklikeaduck.denhaven2.com/articles/2007/06/02/chain-chain-chain

Which might make some of this more understandable.

HTH

-- 
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/