Hi --

On Thu, 12 Dec 2002, Chris Pine wrote:

> Hmm....  I see what you're saying, I think.  I was going to give you a
> counter-example, but I can't because you can't subclass Class.  This, of
> course, begs the question:  Why can't you subclass Class?

I'm now researching that question :-)  From the previous discussions
I've found, which include some posts I'm still puzzling through, the
basic answer seems to be: if you subclass Class, then you propagate
the weird circularity at the top of the tree down into the whole
tree.  That weird circularity being the thing where Object is a Class,
but Class is an object, and Class inherits from Module, but Module is
a Class....  Luckily Ruby takes care of all of this at that level, and
the idea is not to recapitulate it later down the line in other
cases.

> From: <dblack / candle.superlink.net>
>
> [snip]
>
> Mind you, I don't want to discourage the loosening of the notion of an
> object's "class".  Imagine what would happen to the idea of testing
> for type:
>
>   case thing
>   when String ...
>   when Array  ...
>   when Thingy ...
>
> if adding a singleton method to thing changed its class!
> ----------------------------
>
> Well, I thought it was understood that testing for type is a Bad Thing.  If

Yes, I certainly tend to look at it that way.  My point was that
changing an object's class might have the positive effect of making
such tests so meaningless so often that people would stop doing them
:-)  By the way, there have been discussions of the idea of having a
'become' method in Ruby, which you might find interesting.

>> To get all of them you'd want to traverse both the singleton class and
>> the non-singleton [do we have a better word than that?] class:

> Are you sure?  I don't think you need to even look at self.class.ancestors;

Whoops, you're right.

> everything you need is in singleton_class.ancestors (suggesting what
> I am advocating above... it really seems like Ruby only went
> half-way with this one).  In any case, regarding the above, I was
> asking if there was a way to get the singleton class *without*
> creating one in the process (as you have done), in which you get the
> singleton class if it already exists, or nil if it doesn't.  But, it
> probably isn't possible.

I don't know how you'd do that, but I don't think it's very expensive
or significant to create the singleton class.


David

-- 
David Alan Black
home: dblack / candle.superlink.net
work: blackdav / shu.edu
Web:  http://pirate.shu.edu/~blackdav