From: Nathan Smith Date: 2006-08-25T01:21:16+09:00 Subject: Re: Call functions of superclass On Fri, 25 Aug 2006 dblack@wobblini.net wrote: > Hi -- > > On Thu, 24 Aug 2006, Nathan Smith wrote: > > > Hello, > > > > On Thu, 24 Aug 2006 dblack@wobblini.net wrote: > > > >> No; it will point to the superclass of the class of self :-) > >> > >> class C > >> end > >> class D < C > >> end > >> D.new.class.superclass # C > > > > > > I think we are using different terminology. Take a look at this code: > > > > class A > > def A.hi > > puts "A.hi" > > end > > > > def hi > > puts "hi" > > end > > end > > > > class B < A > > def hi > > self.class.superclass.hi > > end > > end > > > > b = B.new > > b.hi > > > > > > This will output > > > > A.hi > > Yes, because B.new.class.superclass is A, and A responds to "hi". The > method A.hi is defined in A's singleton class (or "metaclass") -- but > the object to which you are sending the message "hi" is A, not A's > singleton class. > > > In "Programming Ruby", they define the "metaclass" as the object which > > contains the class-wide objects of the class. IE, the metaclass of A would > > contain A.hi, and not A#hi (I could be getting the notation of A.hi and > > A#hi backwards, or completely wrong -- A.hi is the class method, and A#hi > > is the instance method.) > > > > Therefore, in B#hi, self.class.superclass does not refer to the superclass > > of B -- rather it refers to the metaclass of the super of B. If it pointed > > to the superclass of B, then calling > > > > self.class.superclass.hi > > > > would print "hi", instead of "A.hi" > > No, because A is the superclass of the class of "self" in your > example. A is an object in its own right; it responds to "hi". > > I think you're adding an extra level of reference or indirection or > something. Look at this: > > irb(main):001:0> class C; object_id; end > => 1615634 > irb(main):002:0> C.new.class.object_id > => 1615634 > irb(main):003:0> C.new.class.superclass.object_id > => 1683084 > irb(main):004:0> C.new.class.superclass > => Object > irb(main):005:0> Object.object_id > => 1683084 > irb(main):006:0> class << C; object_id; end > => 1615554 > > As you can see, the singleton class of C is not the same object as > either C or the class of C. > > ("Singleton class" is the most general term; the Pickaxe uses > "metaclass" to mean singleton class of a Class object. I tend to just > use "singleton".) I concede your point -- I was using singleton as the object-specific class, while it is the opposite of that. Thanks for the clarification. Maybe now my arguments will make more sense! Nate