Ruby Object Model : Hierarchy of Class, Object and Module

Objective


To learn the hierarchy of Ruby built-in classes, Class, Object and Module.

Steps


Step 1

In the previous article we experimented with user defined classes and learned that it implicitly extends from Object. What is the Ruby's built-in Object's class? In other words, Object is an object, so it must be an instance of some class, what is that class? We can ask Ruby:

p Object.class

This prints : Class

Step 2

The Ruby's built-in Class itself is an object. So we can find out the class that is used to make instances of Class.

p Class.class

This prints: Class. This seems to be similar to chicken and egg problem. How can Class be created from Class? But Ruby is very consistent. Whenever you use the language construct class to create a Class, either user defined or the existing Ruby's built-in classes, the Class will be used to create instances.

Step 3

In the previous article, we saw that Object was the superclass of any user defined class. What is the superclass of Object?

p Object.superclass

This prints : BasicObject

Step 4

p BasicObject.superclass

prints nil. This means BasicObject is the root of the hierarchy. The BasicObject is an instance of Class. You can verify it like this:

p BasicObject.class

Step 5

What is the superclass of Class?

p Class.superclass

This prints : Module.

Step 6

The class Module is an object. What class is it an instance of?

p Module.class

This prints : Class

Step 7

How can module be a class? Let's say we have a Vehicle module:

module Vehicle
  def wheels
    1000
  end
end

p Vehicle.class

This prints : Module. The Module is a class because Ruby defines Module like this:

class Module

end 

Step 8

So instead of doing this:

module Vehicle
  def wheels
    100
  end
end

class Car
  include Vehicle
end

c = Car.new
p c.wheels

We can do the same thing we did above like this:

Vehicle = Module.new do
  def wheels
    100
  end
end

class Car
  include Vehicle
end

c = Car.new
p c.wheels

Why would you want to do it the second way? The answer to this question will be discussed in an upcoming article.

Step 9

Let's now compare the methods in Module, Class and Object. Here are the methods in Module.

p Module.public_methods(false).sort

This prints:

[:allocate, :constants, :nesting, :new, :superclass]

Here is the methods in Class.

p Class.public_methods(false).sort
[:allocate, :constants, :nesting, :new, :superclass]

Here is the methods in Object.

p Object.public_methods(false).sort
[:allocate, :new, :superclass]

So you can see we can create instances of Module, Class and Object. Because they have the method new().

Step 10

We already know that user defined classes are instances of Class. So instead of doing this:

class Car
  def drive
    p 'driving...'
  end
end

c = Car.new
c.drive

We can do this:

Car = Class.new do
  def drive
    p 'driving...'
  end

end

c = Car.new
c.drive

Both versions of the Car examples print 'driving...'. Why would you want to do it the second way? The answer to this question will be discussed in an upcoming article.

Summary


In this article you learned the Ruby's built-in class hierarchy consisting of Class, Object, Module and BasicObject. You also saw how we can create modules and classes by creating them on the fly and adding methods to it.


Related Articles


Ace the Technical Interview

  • Easily find the gaps in your knowledge
  • Get customized lessons based on where you are
  • Take consistent action everyday
  • Builtin accountability to keep you on track
  • You will solve bigger problems over time
  • Get the job of your dreams

Take the 30 Day Coding Skills Challenge

Gain confidence to attend the interview

No spam ever. Unsubscribe anytime.