getter and setter : getter setter « Class « Ruby






getter and setter


# The setter method name= follows a Ruby convention: the name of the method ends with an equals sign (=). 


class Horse

  def name
    @name
  end

  def name=( value )
    @name = value
  end

end

h = Horse.new
h.name= "Poco Bueno"
h.name # => "Poco Bueno"

 








Related examples in the same category

1.Add a method named get_color, which returns the color of animal objects created from this class:
2.Call a member method
3.Accessors
4.Define getter for a attribute
5.Make an attribute readable and writable by using attr_reader, attr_writer
6.Provide the getter
7.Setter with calculation
8.Setter with =