Ruby craziness: Class vs Object?
Basically the key thing to understand is that every class is an instance of the Class
class and every class is a subclass of Object
(in 1.8 - in 1.9 every class is a subclass of BasicObject
). So every class is an object in the sense that it is an instance of a subclass of Object
, i.e. Class
.
Of course this means that Class
is an instance of itself. If that makes your brain hurt, just don't think about it too deeply.
Object
and Class
are is_a? Object
x.is_a? y
returns true
if x.class == y or x.class < y
, i.e. if x
's class is y
or x
's class inherits from y
. Since every class inherits from object x.is_a? Object
returns true no matter what x
is. (In 1.8 anyway, in 1.9 there's also BasicObject
which is now the most basic class in the inheritance hierarchy).
They are also is_a? Class
Both Object
and Class
are indeed classes, so that should not be surprising.
They are also instance_of? Class
, but not instance_of? Object
.
Unlike is_a?
, x.instance_of? y
only returns true if x.class == y
, not if x.class
is a subclass of y
. So since both x
and y
are instance_of? Class
, they're not instance_of? Object
.
right, nothing can be instance of object.
That's not true. Object.new.instance_of? Object
is true.
kind_of?
kind_of?
is an alias for is_a?
, so see above.
So both are exactly same, then why do we have both these.?
It should be pointed out that everything up to now is true for all classes. E.g. String.is_a? Object
, String.is_a? Class
and String.instance_of? Class
are true and String.instance_of? Object
is false for the same reasons as above. (Also String.is_a? String
and String.instance_of? String
are both false for the same reasons - String is a class, not a string).
You can not conclude from this that all the classes are the same. They're just all instances of the same class.
Comparing methods
Since both Object
and Class
are classes, they both have all the instance methods defined by Class
. Class
additionally has the singleton method nesting
. nesting
tells you which module you're currently nested in, it has nothing to do with inheritance.
For any given class TheClass.methods
will return the instance methods defined by Class
(e.g. superclass
, which returns the class which TheClass
inherits from, and new
which creates a new instance of TheClass
) plus the singleton methods defined by that class.
Anyway methods
only tells you which methods can be called directly on a given object. It does not tell you which methods can be called on an instance of a class. For that you can use instance_methods
, which returns significantly different results for Object
and Class
.
In Ruby, everything is an Object
including classes and modules. Object
is the most low-level class (well, in Ruby 1.9.2 there is also BasicObject
but this is an other story).
See the following output.
> Object.ancestors
# => [Object, Kernel, BasicObject]
> Class.ancestors
# => [Class, Module, Object, Kernel, BasicObject]
> Module.ancestors
# => [Module, Object, Kernel, BasicObject]
> String.ancestors
# => [String, Comparable, Object, Kernel, BasicObject]
As you can see, both Class
and Module
inherits from Object
.
Back to your original assertions, you have to understand the difference bewteen
is_a?
kind_of'
instance_of?
They are not interchangeable. is_a?
and kind_of?
returns true if other is the same class or an ancestor. Conversely, instance_of?
returns true only if other is the same class.
> Class.is_a? Object
# => true
> Class.kind_of? Object
# => true
> Class.instance_of? Object
# => false