Ruby - Root of All Classes

Introduction

All Ruby classes will ultimately descend from the Object class.

Object is the "root" class of the Ruby hierarchy.

In Ruby 1.8 this is literally true.

In Ruby 1.9, Object is derived from a new class called BasicObject.

This new class was created to provide programmers with a very lightweight class.

The Ruby 1.9 Object class inherits the methods from BasicObject and adds a number of new methods of its own.

The root class itself has no superclass, and any attempt to locate its super-class will return nil.

The following code shows how to get the super classes.

Demo

class One 
end # from  ww w . j  av  a  2  s. c  o  m

class Two < One 
end 

class Three < Two 
end 


# Create ob as instance of class Three 
# and display the class name 
ob = Three.new 
x = ob.class 
puts( x ) 
# now climb back through the hierarchy to 
# display all ancestor classes of ob 
 begin 
    x = x.superclass 
    puts(x.inspect)  
end until x == nil

Result

Related Topic