Creating Writable Attributes: using an accessor method followed with an equals sign (=)
class Animal
attr_reader :color
def color=(color)
@color = color
end
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