"David A. Black" <dblack@wobblini.net> schrieb im Newsbeitrag
news:Pine.LNX.4.44.0409130546200.13044-100000@wobblini...
Hi --
> So just because your object above tells the world it's a "Class", I'm
not
> necessarily convinced. I realise that, if this _is_ a benign
deception,
> it's a deep one, because you'd expect the #class method to be
truthful.
> However, that's to be expected when we're talking about the deep
internals
> of Ruby.
>
> If the singleton class of an object is not treated exactly the same as
a
> "normal" class in Ruby's implementation, then I think there's some
> justification for the word "virtual".
Originally I was going to argue that "virtual" is inappropriate because it
was deceiving. While thinking about this issue it occurred to me that
"singleton classes" aka "virtual classes" share a property with C++'s
"virtual classes": you cannot instantiate objects from them.
OTOH, you can have exactly one instance of this class, but this instance
preceedes the class (i.e. first the instance is created and then - maybe -
the class). Strictly speaking, the instance is the factory for the
class - not the other way round as it is normally.
<philosophical>I depends on everyones notion of "class" whether he or she
thinks this no longer makes it a class. Although in that case I'd be
interested to learn a more appropriate name for this
thing.</philosophical>
I think this introduces an unnecessary level of complexity. You're
taking the long way around -- it's a Class object, but it's not a
class, but it acts like one, so we might as well say it is -- when in
fact it's simply a Class object. There's nothing more or less
"normal" about it than about other Class objects;
I regard the lack of the ability to create instances a major difference.
Just as a reminder
s="foo"
=> "foo"
class << s; new ; end
TypeError: can't create instance of virtual class
from (irb):6:in `new'
from (irb):6
class << s; allocate ; end
TypeError: can't create instance of virtual class
from (irb):7:in `allocate'
from (irb):7
class << s; ancestors ; end
=> [String, Enumerable, Comparable, Object, Kernel]
the whole per-object
behavior model of Ruby is based on the idea that every object has both
a class of origin and a class of its own where the definitions
exclusive to that object reside. This isn't abnormal, nor an
aberration; it's the way the whole thing works.
Well, it's normal in Ruby. But generally speaking the naive OO expert
would expect either not to have singleton/virtual classes (SVC :-)) *or*
have Object#class return the singleton class - if that would exist. Note,
I'm not advocating to change this in any way - in fact it's good the way
it is. I just try to uncover why SVC often create confusion.
The schizophrenic thing about SVC is, that instances change their class at
most once without showing it to the outside world (i.e. #class still
returns the original class).
All this shows, what weired things men can come up with - that in practice
do work really great.
Kind regards
robert
PS: Sorry for thinking aloud...
···
On Mon, 13 Sep 2004, Gavin Sinclair wrote: