AHA!!!
Thank you!
That damn hidden singleton class again…
So is there a way to get your real ancestors without jumping into (and thus
creating, if it didn’t already exist) the singleton class using the
following trick?
class << obj
…
end
I don’t understand why these singleton classes are “hidden” from us, when
they clearly make a difference from the coding side of things. And they
aren’t even hidden, since we can get references to them if we want to (but
again, only by creating them if they didn’t exist):
klass = class << obj; self; end
How am I supposed to get all of the Classes/Modules which some object 'obj’
is a kind_of? Get klass (as above) and call klass.ancestors?
I guess all of my confusion, and thus my ultimate complaint comes back to
this:
Why doesn’t the ‘class’ method return the singleton class?
Moreover, check this out:
class Foo
end
foo = Foo.new
klass = class << foo; self; end
p foo.instance_of? Foo # --> true
p foo.instance_of? klass # --> false
That doesn’t seem right to me at all. (Though it does seem consistent, just
consistently wrong… or at least confusing.) I would say that foo was an
instance of Foo, but that changed when you made foo do things other Foo’s
couldn’t. At that point (when foo got it’s own singleton class), foo became
an instance of klass.
Why do we make singleton classes special at all? What I mean is, why make
them singletons?? (I’m sure there’s a good reason… I just want to know
what it is.) If I say:
class Foo
end
foo = Foo.new
def foo.bar
’bar’
end
…why not just have the new regular (non-singleton) class created? Is it
such a terrible thing that someone might try to instantiate the class??
It seems to be that Ruby would be just that much simpler to understand (and
maybe even to implement).
What would we lose by switching singleton classes to regular classes?
Chris