Prev - DUP - Next - TOC

Instance variables


Instance variables are specified by `@'-prepended name. Instance variables are unique for the object which is referred by `self'. Any different object has a different value for the instance variables if even the objects belong to same class. Instance variables cannot be read or changed from the outer objects, so, one must do it via a method. The value of an uninitialized instance variable is `nil'.

Instance variables of ruby do not need declaration. This implies a flexible structure of objects. The instance variables of ruby, indeed, are dynamically appended.

 ruby> class InstTest
 ruby|   def foo(n)
 ruby|     @foo = n
 ruby|   end
 ruby|   def bar(n)
 ruby|     @bar = n
 ruby|   end
 ruby| end
 nil
 ruby> i = InstTest.new
 #<InstTest:0x83678>
 ruby> i.foo(2)
 2
 ruby> i
 #<InstTest: @foo=2>
 ruby> i.bar(4)
 4
 ruby> i
 #<InstTest: @foo=2, @bar=4>

Prev - DUP - Next - TOC