fwoncn
fwoncn

Reputation: 189

Why Object.class == Class in Ruby?

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

Upvotes: 5

Views: 4215

Answers (4)

Vitaly Kushner
Vitaly Kushner

Reputation: 9455

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

Upvotes: 9

Sinan Taifour
Sinan Taifour

Reputation: 10805

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.

Upvotes: 6

ennuikiller
ennuikiller

Reputation: 46965

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

Upvotes: 5

Remus Rusanu
Remus Rusanu

Reputation: 294387

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.

Upvotes: 10

Related Questions