Hi --
The following is quoted from ruby-1.8.4-core-rdocs
Classes in Ruby are first-class objects—each is an instance of class Class.
All metaclasses are instances of the class `Class’.
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
=> 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
=> "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
···
On Sun, 21 May 2006, uncutstone wu wrote:
--
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!
> Ruby for Rails