First, ALWAYS an instance of Class in Ruby?
No, not all of these are Class examples. Only classes are instances of Class .
There are many things that are not instances of Class : strings, for example, are instances of String , not Class . Arrays are instances of Array , integers are instances of Integer , floats are instances of Float , true is an instance of TrueClass , false is an instance of FalseClass , nil is an instance of NilClass , etc.
Each class is an instance of Class , just as each row is an instance of String .
if Object is a superclass of Class , how can it be both a superclass of Class and an instance of it at the same time (in most diagrams on the Ruby object model this hierarchy is clearly indicated)?
Magic.
As in most other languages, there are some basic entities that are supposed to exist. They fall from the sky, materialize from the air, magically appear.
In Ruby, some of these magical things are:
Object does not have a superclass, but you cannot define a class without a superclass; the implicit direct superclass is always Object . [Note: there may be implementation superclasses of Object , but ultimately there will be one that does not have a superclass.]Object is an instance of Class , which is a subclass of Object (which means that, indirectly, Object is an instance of Object itself)Class is a subclass of Module , which is an instance of ClassClass is an instance of Class
None of these things can be explained in Ruby.
BasicObject , Object , Module and Class all have to exist spring at the same time, because they have circular dependencies.
Just because this relationship cannot be expressed in Ruby code does not mean that the Ruby language specification cannot say that it should be so. It is up to the developer to figure out a way to do this. In the end, the Ruby implementation has a level of access to objects that you, as a programmer, do not have.
For example, a Ruby implementation may first create a BasicObject by setting both its superclass pointer and the Class pointer to null .
He then creates an Object by setting the superclass pointer to BasicObject and its Class pointer to null .
He then creates a Module by setting the superclass pointer to Object and its Class pointer to null .
Finally, he creates a Class by setting the superclass pointer to Module and its Class pointer to null .
Now we can rewrite BasicObject 's, Object ' s, Module 'and Class Class pointer to Class , and we are done.
This is easy to do from outside the system; it just looks weird from the inside.