I think Object is everyone's ancestor, including Class. So I think it should be Class.class == Object. I feel a bit of confused and twisted
class returns the class (#type) not the ancestor. Objects's class is Class. Class's class is Class. Class is an Object. Truth in advertising: I never learned Ruby, but the Object-Class relation has to be the one Smalltalk set forth 30 years ago.
-
4Metaclasses work differently in Ruby: they get injected into the method lookup chain, thus Ruby doesn't have the two parallel hierarchies of classes and metaclasses, but, as you say, the basics are identical. – Jörg W Mittag Aug 3 '09 at 0:38
-
@Jorg: thanks. As soon as I finish my current project I'll pick up on rails and ruby, I've hear so much praise that I'm intrigued :) – Remus Rusanu Aug 3 '09 at 1:34
-
Thank you. I think I'm clear about the relations now. @Jörg_W_Mittag 's information helps, too. That is, classes and Metaclasses are two different but related concepts. – fwoncn Aug 3 '09 at 3:05
-
Yeah but this explanation doesn't really detail the purpose of class Class and why to extend methods to it, presumably so all classes can have class methods, as opposed to objects which are just instances and inherit from Object class. – JohnMerlino May 13 '11 at 20:02
Class, Object, Module and all other classes are instances of a class Class :)
Class.class == Module.class == Object.class == Hash.class == Class
Class is also is an Object (like any other object in the system) but it is not direct instance of Object, it is an instance of a derived class (Class in this case)
Class.superclass.superclass == Object (with Module in the middle)
Object itself is also a class. so Object.class == Class
Class, Module and Object have a circular dependency as they are in the core of the OO model.
Object.class.superclass.superclass == Object
=> parent (.superclass)
-> instance-of (.class)
alt text http://www.grabup.com/uploads/b10b2ffa9976953e3d6f88e6fcbf6f28.png?direct
-
The circular dependency works because these are all built-in classes. They are part of the core Ruby runtime and are set up this way at start-up and aren't added on the way you would normally add a class. The runtime has the power to set up the pointers however it wants, and the designer chose to do it this way. – Rose Perrone May 9 '12 at 19:37
Object
's class is Class
(since Object
itself is a class), and Object
is an ancestor of Class
.
There is a circular reference, it is pretty complex. My personal recommendation, if you don't really need to play with it, don't go there.
This is the way it works in ruby 1.9:
Class.class = Class
Class.superclass = Module
Module.class = class
Module.superclass = Object
Object.class = Class
Object.superclass = BasicObject
BasicObject.class = Class
BasicObject.superclass = nil