Class, Object and Module Hierarchy

In this chapter, you will learn about the hierarchy of Ruby built-in classes, Class, Object and Module.

Object is an Instance of Class

In the previous chapter we experimented with user defined classes. We learned that user defined classes implicitly extend 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

Object is an Instance of Class

Class is an Instance of Class

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

p Class.class

This prints:

Class

Class is an Instance of Class

This seems to be like the chicken and egg problem. How is Class created from Class? But Ruby is consistent. Whenever you use the language construct class to create a Class, Ruby uses Class to create instances.

The class can be either user defined or the existing Ruby's built-in classes.

name = 'Bugs Bunny'
p name.class

This prints:

String

String is the Ruby's built-in class. The above code is the same as doing this:

name = String.new('Bugs Bunny')
p name.class

BasicObject is Parent of Object

In the previous chapter, we saw that Object was the super-class of any user defined class. What is the super-class of Object?

p Object.superclass

This prints:

BasicObject

BasicObject is Parent of Object

BasicObject is the Root

What is the super-class of BasicObject?

p BasicObject.superclass

This prints:

nil

This means BasicObject is the root of the hierarchy. It does not have a parent.

Adam and Eve

BasicObject is an Instance of Class

The BasicObject is an instance of Class. You can verify it like this:

p BasicObject.class

This prints:

Class

BasicObject is instance of Class

Module is the Parent of Class

What is the super-class of Class?

p Class.superclass

This prints:

Module

Visual Summary

Here is the visual summary of what we have learned so far.

Hierarchy of Class, Object and Module

Module is an Instance of Class

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

p Module.class

This prints:

Class

Module is Instance of Class

Why is Module a Class?

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

Can we create an instance of Module class? Yes, we will see that next.

How to Create a Module Instance?

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

Fabio Asks

Why would you want to create a module the second way?

The short answer is blocks are closures. We have access to the variables before the do-end block.

Methods in Module, Class and Object

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 are the methods in Class.

p Class.public_methods(false).sort

This prints:

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

Here are the methods in Object.

p Object.public_methods(false).sort

This prints:

[:allocate, :new, :superclass]

You can see we can create instances of Module, Class and Object. Because they have the method new(). You can also notice that class and module have the same set of public methods.

Dynamic Creation of Car Class

We already know that user defined classes are instances of Class. 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...

Rhonda Asks

Why would you want to create a class the second way?

The short answer is blocks are closures. We have access to the variables before the do-end block.

Summary

In this chapter you learned the Ruby's built-in inheritance hierarchy. This consists 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.

results matching ""

    No results matching ""