From: dblack@... Date: 2006-03-02T23:13:34+09:00 Subject: Re: Class method in singleton_methods? Hi -- Adding something to my previous reply: On Thu, 2 Mar 2006, dblack@wobblini.net wrote: > Hi -- > > On Thu, 2 Mar 2006, Minkoo Seo wrote: > [...] >> It is interesting that a class method is actually a singleton method. I >> know that there are tons of posting regarding metaclasses/objects. >> Unfortunately, there are simply too many to read and understand all of >> them. So, please forgive my naive question. >> [...] >> Am I correct? > > A class method is indeed a singleton method of a Class object. The > term "class method" is really just a convenient label for this case, > because it occurs quite frequently. The one way in which class methods differ from other singleton methods is that they're the only case, as far as I know, where more than one object can call a specific singleton method: class C def self.x end end class D < C end D.x # OK, because D is a subclass of C, even though x is # a singleton method on another object (C) The reason is this: C's singleton class (or "metaclass", as singleton classes of Class objects are sometimes known) is the superclass of D's singleton class. Since the method x resides in C's singleton class, it is visible to instances of that singleton class (namely C), and to instances of descendants of that singleton class (namely, D). In other words, when you send the message "x" to D, D looks in its singleton class, and then in the superclass of its singleton class -- and there it finds a method "x", and executes it. David -- David A. Black (dblack@wobblini.net) Ruby Power and Light (http://www.rubypowerandlight.com) "Ruby for Rails" chapters now available from Manning Early Access Program! http://www.manning.com/books/black