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
Object is the default root of all Ruby objects. Object inherits from BasicObject which allows creating alternate object hierarchies. Methods on Object are available to all classes unless explicitly overridden. Object mixes in the Kernel module, making the built-in kernel functions globally accessible.
An object is a unit of data. A class is what kind of data it is.
Ruby is a pure object-oriented language and everything appears to Ruby as an object. Every value in Ruby is an object, even the most primitive things: strings, numbers and even true and false. Even a class itself is an object that is an instance of the Class class.
You can create objects in Ruby by using the method new of the class. The method new is a unique type of method, which is predefined in the Ruby library. The new method belongs to the class methods. Here, cust1 and cust2 are the names of two objects.
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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With