From: dblack@... Date: 2006-05-21T20:12:47+09:00 Subject: Re: About , meta class and Cl ---2049402039-596971795-1148209964=:18133 Content-Type: MULTIPART/MIXED; BOUNDARY="-2049402039-596971795-1148209964=:18133" This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. ---2049402039-596971795-1148209964=:18133 Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed Content-Transfer-Encoding: QUOTED-PRINTABLE Hi -- On Sun, 21 May 2006, uncutstone wu wrote: > The following is quoted from ruby-1.8.4-core-rdocs >> Classes in Ruby are first-class objects=E2=80=94each is an instance of c= lass Class. >> All metaclasses are instances of the class `Class=E2=80=99. > > This makes me confused. In my understanding, all metaclasses are > subclass of the class 'Class' as I posted in previous post. > > Somebody can explain it? The class Class has no subclasses: irb(main):004:0> class C < Class; end TypeError: can't make subclass of Class Every class is an instance of Class itself, including singleton classes of all flavors. You can test this like this: class C class << self puts self.class end end =3D> Class What happens with inheritance is this: Given the following: class C end class D < C end C is the superclass of D, and C's singleton class (yes, I prefer to avoid the term metaclass entirely :-) is the superclass of D's singleton class. So it's like this, looking at it side by side: class C class C' end end class D < C class D' < C' end end where the thing on the right happens automatically. You can verify this: irb(main):013:0> class << C; object_id; end 537746792 irb(main):014:0> class << D; superclass.object_id; end 537746792 This, by the way, is why you can do this: def C.x; puts "A singleton method of C"; end D.x =3D> "A singleton method of C" It's strange for one object (D) to be able to call the singleton methods of another (C) -- except that classes are treated as a special case. So it's as if you'd done this: class C' def x puts "A singleton method of C" end end class D' < C' end So "instances" of D' will respond to that x method -- and the only "instance" of D', by definition, is D itself. David --=20 David A. Black (dblack@wobblini.net) * Ruby Power and Light, LLC (http://www.rubypowerandlight.com) > Ruby and Rails consultancy and training * Author of "Ruby for Rails" from Manning Publications! > http://www.manning.com/black ---2049402039-596971795-1148209964=:18133-- ---2049402039-596971795-1148209964=:18133--