It’s probably not a paradox at all, but from a newbies perspective, it sure seems that way.
> Class.superclass
=> Module
> Class.superclass.class
=> Class
> Class.superclass.class.superclass
=> Module
So a class’s parent is module, but module is a class?
How can I make sense of this?
TL;DR: Module is the superclass of Class. Module is an instance of Class.
Let me try to explain it more clearly. Please forgive my handwritten drawings – I don’t have any fancy drawing software.
Every class in Ruby has 1 superclass*.
*Except for BasicObject, which doesn’t have a superclass.
Read the above graphic like this: The superclass of Float is Numeric. The superclass of Numeric is Object, etc…
When you instantiate an object, the object will be an instance of some class. For example, “Nathan” is an instance of the String class. So is “Joe” or “John”. 1 is an instance of the Fixnum class, as are 2, 3, 4, etc…
Read the above graphic like this: “Joe” is an instance of String. 1 is an instance of Fixnum, etc…
Well, in Ruby, unlike in most other languages, Class is a just another class, and it can be instantiated, too, just like Fixnum or String.
Read the above graphic like this: 0.01 is an instance of Float. String is an instance of Class, etc…
Realize that Fixnum is an instance of Class, just like “Nathan” is an instance of String. Just like “John” is an instance of String, Float is just an instance of Class. Every class is just an an instance of Class, even Class itself!
Whenever you write a new class in your app, you are just instantiating a new object whose class is Class, just like Hash.new instantiates a new Hash, or “Nathan” instantiates a new String.
Furthermore, Module is just another instance of Class. Whenever you write a new module in your app, you are just instantiating a new Module.
An aside: A Module is just a collection of methods and constants. Classes are also a collection of methods and constants, but with the added functionality of being able to be instantiated. A module cannot be instantiated. That is,
m.newwill not work.So, referring back to the top graphic, your question can be answered directly:
You can see from the top graphic: Module is the superclass of Class.
From the bottom graphic: Module is an instance of Class.