I just started playing with JRuby. This is my first ruby post. I had a hard time understanding classes vs objects in Ruby. It doesnt mean like what classes & objects in other Object oriented laguages. for an example
Class.is_a? Object
returns true and
Object.is_a? Object
too.
so class & Object are both objects
here comes another one
Class.is_a? Class
returns true and
Object.is_a? Class
too.
wait, i am not done yet
Object.instance_of? Class
Class.instance_of? Class
Both are true
Object.instance_of? Object
Class.instance_of? Object
Both are false. right, nothing can be instance of object.
And
Class.kind_of? Class
Object.kind_of? Class
both are true
Class.kind_of? Object
Object.kind_of? Object
both are true
So both are exactly same, then why do we have both these.?
After some more digging, i wrote this simple method to return method list supported by both
irb(main):054:0> def print_methods(obj)
irb(main):055:1> obj.methods.each do |mm|
irb(main):056:2* puts mm
irb(main):057:2> end
irb(main):058:1> end
Only method difference between print_methods(Object) and print_methods(Class) is
Nesting
if Nesting means inheritance, Is Object similar to the sealed class??
Can someone clarify me what is all this?
Update: To Edds comment
Interestingly i see lot of differences in the method list in
c=Class.new
print_methods(c)
&
o=Object.new
print_methods(o)
Now I understand Instance of a class is really an class instance (And this class instance is actually a Object) not an object instance. And even this instance allow me to span another instances
xx = c.new //works - c is an Object / and xx is a instance of an Object c
yy = o.new //nope - o is already a instance of an Object, so it cannot be instantiated again
So Finally, Object is really an instance of a Class. Because
xx.is_a? Class
is false, but
xx.is_a? Object
returns true
Am i right, ??
Ramesh, in ruby everything is an object, and a Class is no exception.
try this in irb
in this case, I've created an instance of an Object, and checked if it's a class (false) or a Object (true).
A Class in ruby, is some kind of template object used to create instances of that class. Sorry that this is not super clear. The key concept is that ruby is a pure object oriented language, as opposed to Java.
As _why writes in this article
The first couple sections have some good points about classes vs objects
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 alsoBasicObject
but this is an other story).See the following output.
As you can see, both
Class
andModule
inherits fromObject
.Back to your original assertions, you have to understand the difference bewteen
is_a?
kind_of'
instance_of?
They are not interchangeable.
is_a?
andkind_of?
returns true if other is the same class or an ancestor. Conversely,instance_of?
returns true only if other is the same class.One of the answers mentions this:
I just want to word it differently for those who have a little brain twist. First ask yourself: What is an instance in programming? And what is a subclass in programming? An instance is just a realized variation of a blueprint (the Class). A subclass is simply a class (blueprint) that inherits from another class (blueprint). So when you create a new class:
Apple is an instance of Class, that is, it is a realized variation of the blueprint. It takes the blueprint and fills in the details (methods and variables) with its own variation. Well, the blueprint inherits from another blueprint, which is Object. So every class is an instance of Class, which is a subclass of Object.
Note Class has Module in its inheritance chain (Module included in Class as a mixin perhaps since Class's parent is Object?).
Instances (A.new) of class A will have their own realized variations of A. But they are object instances. So we must distinguish class instances (e.g. class A end) and object instances ( a = A.new). Object instances have a different inheritance chain. They are a realized variation of a class instance blueprint, not a variation of class Class.
This means in their inheritance chain is not Class or Module. But rather other object instances, so if A has object instances and B has object instances and A inherits from B, when we instantiate a new object instance of A, this instance will have B instances in its inheritance chain.
They will also inherit from Object, since everything in Ruby inherits from Object.
And this is the best way to think about it all. Do not go too deep with your thinking. Accept this as I have written it.
Basically the key thing to understand is that every class is an instance of the
Class
class and every class is a subclass ofObject
(in 1.8 - in 1.9 every class is a subclass ofBasicObject
). So every class is an object in the sense that it is an instance of a subclass ofObject
, 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
andClass
areis_a? Object
x.is_a? y
returnstrue
ifx.class == y or x.class < y
, i.e. ifx
's class isy
orx
's class inherits fromy
. Since every class inherits from objectx.is_a? Object
returns true no matter whatx
is. (In 1.8 anyway, in 1.9 there's alsoBasicObject
which is now the most basic class in the inheritance hierarchy).They are also
is_a? Class
Both
Object
andClass
are indeed classes, so that should not be surprising.They are also
instance_of? Class
, but notinstance_of? Object
.Unlike
is_a?
,x.instance_of? y
only returns true ifx.class == y
, not ifx.class
is a subclass ofy
. So since bothx
andy
areinstance_of? Class
, they're notinstance_of? Object
.That's not true.
Object.new.instance_of? Object
is true.kind_of?
kind_of?
is an alias foris_a?
, so see above.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
andString.instance_of? Class
are true andString.instance_of? Object
is false for the same reasons as above. (AlsoString.is_a? String
andString.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
andClass
are classes, they both have all the instance methods defined byClass
.Class
additionally has the singleton methodnesting
.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 byClass
(e.g.superclass
, which returns the class whichTheClass
inherits from, andnew
which creates a new instance ofTheClass
) 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 useinstance_methods
, which returns significantly different results forObject
andClass
.The class/metaclass hierarchy is always a little puzzling :) Just for comparison, here's the one in Smalltalk; in Ruby, the setup is based on the same principles, except it doesn't have the
Behavior
andClassDescription
distinctions, and there are modules and eigenclasses to take into account.A full explanation of the Smalltalk object model is available in Pharo by Example, as pointed by this related question.