2

Class.instance_of?(Object) returns false, while Class.instance_of?(Class) returns true. I find this surprising because I thought Class, being an object, had to be an instance of the Object class. How can it be an instance of itself?

But assuming that Class is an instance of the Object class seems to lead to an even more confusing issue; since Object is a class, it has to be an instance of the Class class. What is going on here?

sawa
  • 165,429
  • 45
  • 277
  • 381
Train Heartnet
  • 785
  • 1
  • 12
  • 24
  • 2
    Have a look at [Ruby: kind_of? vs. instance_of? vs. is_a?](http://stackoverflow.com/questions/3893278/ruby-kind-of-vs-instance-of-vs-is-a) – Wand Maker Dec 28 '15 at 18:21

3 Answers3

2

Classes in Ruby are first-class objects: each is an instance of class Class

So every class you define gives you:

  > MyClass.instance_of?(Class)
  => true

Check out the diagram with the inheritance on this page: http://ruby-doc.org/core-2.2.3/Class.html

Classes, modules, and objects are interrelated. In the diagram that follows, the vertical arrows represent inheritance, and the parentheses metaclasses. All metaclasses are instances of the class `Class'.

                         +---------+             +-...
                         |         |             |
         BasicObject-----|-->(BasicObject)-------|-...
             ^           |         ^             |
             |           |         |             |
          Object---------|----->(Object)---------|-...
             ^           |         ^             |
             |           |         |             |
             +-------+   |         +--------+    |
             |       |   |         |        |    |
             |    Module-|---------|--->(Module)-|-...
             |       ^   |         |        ^    |
             |       |   |         |        |    |
             |     Class-|---------|---->(Class)-|-...
             |       ^   |         |        ^    |
             |       +---+         |        +----+
             |                     |
obj--->OtherClass---------->(OtherClass)-----------...

This means:

  > Class.ancestors 
  => [Class, Module, Object, Kernel, BasicObject]

  > Class.superclass
  => Module
  > Module.superclass
  => Object
  > Object.superclass
  => BasicObject
  > BasicObject.superclass
  => nil

To get a deeper understanding of this, I highly recommend Dave Thomas's screencast series: The Ruby Object Model and Metaprogramming

Tilo
  • 33,354
  • 5
  • 79
  • 106
1
BasicObject.superclass # => nil
Object.superclass # => BasicObject
String.superclass # => Object
Class.superclass # => Module
Module.superclass # => Object

Class.ancestors # => [Class, Module, Object, Kernel, BasicObject]

As you can see the Class is a module and when you create any class you are creating an object of the Module. And Class.ancestors returns all ancestors of objects. Because classes in Ruby is an object.

Alexander Shlenchack
  • 3,779
  • 6
  • 32
  • 46
0

Class as an instance of itself

Class is a special class in that it can create new classes. Now since Class itself is a class, it can only be created by Class and this is how Ruby defines this. Your take away:

Class is an instance of Class.

But how can Class create itself if it doesn't exist? That's a good point. I would say just think of it as a special entity that has created itself.


Class as a kind of instance of Object

Class is also a subclass of Object. So all instances of Class are really just specialised instances of Object. In other words an instance of Class is an instance of Object plus all the powers of the class Class.

Using this thinking and as defined earlier: Class is an instance of Class. Thus Class is a specialised instance of Object.

Let's now consider Object. Object is a class and all classes are instances of Class. It follows that Object is an instance of the class Class.


So which came first? Object or Class?

Object is a class so Class must exist first in order to create Object. But Class is a subclass of Object, so Object must exist first. So which is it? Just accept it as a circular definition, i.e. Object needs Class, but Class needs Object, but Object needs Class etc etc.

Sagar Pandya
  • 9,323
  • 2
  • 24
  • 35