In Ruby, you prefix instance variables with an at sign @ : instance variables « Class « Ruby






In Ruby, you prefix instance variables with an at sign @


# An instance variable is one in which you can store the data in an object - an instance of a class is an object. 
# Instance variables hold their values for as long as the object is in existence. 
# That's what helps make objects self-contained: the capability to store data.



class Animal
  def initialize
    @color = "red"
  end
end

 








Related examples in the same category

1.Using a Constructor to Configure Objects
2.Print out the instance variable
3.Adding more accessors
4.class variable vs object variable