Ruby Yuan Biancheng.

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

Look at the following code

class D
 def my_methond
   @v = 'demo'

d =
puts d.class
puts d.instance_variables #puts nothing

puts d.instance_variables #puts @v

Let us think in what?

Before calling my_methond, not what is case, after calling my_methond, the instance variable instance.

So, in ruby, the class of the object and its instance variables without any relationship, when to assign instance variables, they generate. So for the same class, you can create a different object instance variables. You can put the ruby instance variables as the hash table keys, each key of object may be different.

We think the object and method, in fact the objects and no method. In the inside of an object contains only the assignment of instance variables and the reference to the class. Examples of real places in the class. This is why the objects of the same class share the same methods and do not share the same instance variables.


Summary: an object's instance variables in the object itself, and a method of an object in the object's class.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Simon at December 17, 2013 - 12:17 PM