I am a Ruby newbie and having some problems getting my mind around certain
aspects of classes, especially anonymous classes and singleton classes.
I composed a nice long mail to you all then realised that I should check the
mail archive first and found the February thread "Singleton Classes". Now I am
even more confused!
I had just decided that class << obj; ...; end didn't actually create an
anonymous class (even though FAQ 6.6 says it does) and then matz says that it
does.
<code>
class C1
def to_s
">>> C1.to_s"
end
end
def C1.inherited(subclass)
print ">>> inherited ", subclass, "\n" # never called
end
obj = C1.new
class << obj
def to_s
super + " => C2.to_s"
end
end
print obj, "\n" # >>> C1.to_s => C2.to_s
print obj.type, "\n" # hmm, still says C1
print obj.type.id == C1.id, "\n" # true, it really is C1
</code>
Well it looks like it does when it comes to calling super() as C1.to_s gets
called. But obj.type is unchanged and Class.inherited() doesn't get called. So
what is going on?
Actually Class.inherited() doesn't seem to get called when subclassing with
Class.new() either. Is this a bug?
ruby -v
ruby 1.4.3 (1999-12-08) [i686-cygwin]
<code>
class C1
end
def C1.inherited(subclass)
print ">>> inherited ", subclass, "\n"
end
class C3 < Class.new(C1)
end
print C3.ancestors.join(", "), "\n"
</code>
outputs:
>>> new subclass C3
C3, #<Class 0lx1208af08>, C1, Object, Kernel
Thanks,
Bob
-----------------------------------------------------------------
Visit our Internet site at http://www.reuters.com
Any views expressed in this message are those of the individual
sender, except where the sender specifically states them to be
the views of Reuters Ltd.