Creating Readable and Writable Attributes: use attr_accessor : attr_accessor « Class « Ruby






Creating Readable and Writable Attributes: use attr_accessor



class Animal
  attr_accessor :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.attr_accessor add accessor to a class
2.Get new defined instance methods
3.Use attr_accessor to add a new attribute and set it
4.A logger