This is a scope issue. A local variable is visible / applicable only in the method in which it is defined (i.e., it disappears when the method returns).
On the other hand, an instance variable is visible elsewhere in the instance of the class in which it was defined (this is different from a class variable that is shared between all instances of the class). However, keep in mind that it is important when defining an instance variable. If you define an instance variable in one method, but try to use it with another method before calling the first, your instance variable will be nil:
def method_one @var = "a variable" puts @var end def method_two puts @var end
@var will have a different meaning depending on when you call each method:
method_two()
Peter Roe
source share