On 12/17/07, Robert K. [email protected] wrote:
2007/12/17, Rick DeNatale [email protected]:
The statement I wanted to correct, maybe I misunderstood it was:
The meta class is there but it’s hidden from #ancestors as you found
out. Inheritance wise it sits between an object and its class.
I read object as an instance of “its class.” The above statement is
wrong in two regards.
But note that any class is also an instance.
But that doesn’t seem to be relevant to the 'slightly restated assertion
Inheritance wise the metaclass sits between an object and its class.
Let’s say that the object is an array, we have
array -klass-> Array ->klass-> Array’ (i.e. Arrays metaclass)
which isn’t an inheritance chain anyway, but it illustrates that the
metaclass doesn’t sit between an instance and its class. Now lets’
say the object was a class, say Array
we have two parallel inheritance chains:
Again, since a class is also an instance I choose to use the more
general term. But the statement holds true for classes as well because
with regard to this there is no difference between an ordinary
instance and an instance of Class (or Module for that matter). There
is in fact some difference in behavior that results from this (see my
posting) but the singleton class of a class sits between the instance
(the class) and its class (Class).
Yes, but not all singleton classes are metaclasses, I maintain that
only singleton classes of classes are properly called metaclasses.
The meaning of metaclass is “class of a class”, so calling the
singleton class of a non-class object a metaclass is a misnomer,
albeit, sadly, a common one.
Given this definition, it only holds true if the object in the general
statement is a class which makes the statement incorrect in general.
Generally singleton classes of class instances and of ordinary instances
behave the same. The only difference I can see so far is that, since
classes can inherit from each other, a subclass also shares instance
methods defined for its superclass (see my irb session). Btw, something
similar happens with instances when cloned:
Not quite the same, what happens here is that cloning an object with a
singleton class gives the clone a singleton class which looks like the
original object’s singleton class:
That’s why I wrote “similar” and not “same”…
The point is that there’s no inheritance involved, just copying.
One last point, you have to be careful in looking at the results from
experiments like this in irb/ruby programs because some of the
reflection methods in ruby deliberately lie about what’s really
happening under the covers.
Can you elaborate which methods do actually lie?
Off the top of my head, ancestors for one.
My blog on Ruby