Ruby gives you a quick way to create writable attributes with attr_writer : Writable Attributes « Class « Ruby






Ruby gives you a quick way to create writable attributes with attr_writer


class Animal
  attr_reader :color
  attr_writer :color

  def initialize(color)
    @color = color
  end
end

animal = Animal.new("brown")
puts "The new animal is " + animal.color
animal.color = "red"
puts "Now the new animal is " + animal.color

 








Related examples in the same category

1.Creating Writable Attributes: using an accessor method followed with an equals sign (=)