The phrase "instance of" is a small but significant part of the mental gymnastics you need to do here.
That, and objects of type Class
, and a class called Object
.
If you can understand these, at least while repeating to yourself slowly, then you have got it:
Object.new
creates an instance of class Object
Object
is a reference to the class Object, which is itself an object of class Class
Class
is a reference the class Class, which is also an object of class Class (!)
Class.new
creates an instance of class Class.
- This is part of what happens under the hood when you write
class Foo
- In fact
Foo = Class.new( String )
is much the same as class Foo < String; end
The class hierarchy of Class
, Module
, Object
is an implementation detail in Ruby. Almost all classes inherit from Object
, so it is no real surprise that Class
does so too.
The rest is just repeated use and experience.
It is worth noting something else going on here: The labels in the code you type are symbols/variable names, which are pointers to the underlying objects which have types and contain data. There is no requirement to use those labels directly, they are pretty much the same as any other Ruby variable:
o_klass = Object
o_instance = o_klass.new
o_instance.class
=> Object