Ruby - Root of All Classes


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.


class One 
end # from w w w.j a  va2s  .com

class Two < One 

class Three < Two 

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


Related Topic