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.
# By running this, you will be instantiating a new Class, and
# it will be named Post
class Post < ActiveRecord::Base
end
# Here is another perfectly valid way to write the above code:
Post = Class.new(ActiveRecord::Base)
# you can even instantiate a Class without giving it an explicit name:
x = Class.new(ActiveRecord::Base)
# and since your new things are classes, they can be instantiated
obj1 = Post.new
obj2 = x.new
Furthermore, Module is just another instance of Class. Whenever you write a new module in your app, you are just instantiating a new Module.
# this will instantiate a new Module, and assign it to Foo
module Foo
end
# Here is another perfectly valid way to write the above code:
Foo = Module.new
# you can even instantiate a Module without giving it an explicit name.
m = Module.new
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.new
will not work.
So, referring back to the top graphic, your question can be answered directly:
So a class's parent is module, but module is a class?
You can see from the top graphic: Module is the superclass of Class.
From the bottom graphic: Module is an instance of Class.