Ruby Classes and Superclasses

An interesting question came up in this week’s Seven Languages in Seven Weeks skype call. We initially thought it might be a difference between versions of Ruby, but having looked more closely, i don’t think it is. Both 1.8.7 and 1.9.2 behave in this slightly odd way.

For reference, we are talking about Figure 2.1: Ruby Metamodel on page 26.

As we all now know, 4 is an object.

> 4.class
 => Fixnum

Fixnum inherits from Integer which inherits from Numeric.

> Fixnum.superclass
 => Integer
> Integer.superclass
 => Numeric

Each of Fixnum, Integer and Numeric are of class Class.

> [Fixnum, Integer, Numeric].map(&:class)
 => [Class, Class, Class]

The superclass of Numeric is Object.

> Numeric.superclass
 => Object

But the superclass of Class goes to Module and then to Object.

> Class.superclass
 => Module 
> Module.superclass
 => Object

And now, through the power of my very limited Open Office Drawing skills, i shall try to demonstrate what i think the diagram really looks like.

So where did Module come from? Why is a Class also a Module? And if Numeric is of class Class, why is its superclass not also Module?

I don’t think it’s particularly important to know these things, but they were interesting questions and i don’t know the answers.

Does anyone know?