Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Object.class == Class in Ruby?

Tags:

oop

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

like image 580
fwoncn Avatar asked Aug 02 '09 19:08

fwoncn


People also ask

What is object class in Ruby?

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.

What is the difference between a class and an object in Ruby?

An object is a unit of data. A class is what kind of data it is.

Why is Ruby a object-oriented language?

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.

How do you create an object in Ruby?

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.


4 Answers

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.

like image 135
Remus Rusanu Avatar answered Oct 07 '22 01:10

Remus Rusanu


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

like image 23
Vitaly Kushner Avatar answered Oct 07 '22 02:10

Vitaly Kushner


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.

like image 6
Sinan Taifour Avatar answered Oct 07 '22 02:10

Sinan Taifour


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
like image 5
ennuikiller Avatar answered Oct 07 '22 01:10

ennuikiller