Austin Ziegler
4/5/2005 4:59:00 PM
On Apr 5, 2005 12:48 PM, Jim Weirich <jim@weirichhouse.org> wrote:
> On Tuesday 05 April 2005 10:05 am, Austin Ziegler wrote:
>> I've modified your diagram; ocObject* is an instance of
>> otherClass, and the (ocObject)* metaclass is the metaclass for
>> this instance, and it is a different metaclass than any other
>> instance of otherClass.
> Do all instances have their own metaclass? I thought metaclasses
> (singleton classes) sprang into existence only when needed to have
> a place to put singleton methods.
That's my understanding, too. But since you can't look at a
metaclass without instantiating it, the minor sleight of hand that I
performed above is (IMO) acceptable.
Consider:
a = Object.new; b = Object.new
puts a.id, b.id
# 22756396
# 22756384
class << a; puts self.id; end; class << b; puts self.id; end
# 22738408
# 22738396
In effect, all instances have their own metaclass. As an
implementation detail, though, they aren't instantiated until they
are needed. I'm not sure that there's a meaningful difference
between the lazy instantiation and constant instantiation from the
Ruby programmer's point of view -- unless you're doing something
really freaky with AST parsers ;)
-austin
--
Austin Ziegler * halostatue@gmail.com
* Alternate: austin@halostatue.ca