Hello,
I have a question about Figure 19.2, page 244 in Programming Ruby.
The picture is explaining the following code:
class Guitar
def Guitar.strings()
return 6
end
def play()
…
end
…
end
lucille = Guitar.new
(Which, BTW, if `lucille’ is a reference to something, I didn’t get it.
So, my question is in the picture. As I understand it, the above code
creates a Class object, Guitar, and also creates a singleton class for
Guitar. (Guitar is a class, but it’s also an object, and any object (except
immediate ones) can have a singleton class.)
If this is so, why is this picture so different from the one on the
following page, which describes how singleton classes work? Does a
singleton class sit inbetween the object and the class (like in Figure 19.3)
or does it set above the class (like in Figure 19.2)? What about the
singleton’s super pointer? It is very different in the two figures. Is
Guitar’s klass pointer really pointing to the singleton class instead of to
Class, like I thought all classes did?
Or are these two totally different situations? And if so, why were
metaclasses (as they seem to be called, though they are very rarely referred
to in this way) implemented in such a different way from how singletons are
implemented? Would it not work to make them like singletons?
Chris