Creating Readable Attributes : Readable attributes « Class « Ruby






Creating Readable Attributes



# object. color here (as in animal.color) is called an attribute.
# color is a readable attribute because you can read the value of the internal @color instance variable.

class Animal
  def color()
    @color
  end

  def initialize(color)
    @color = color
  end
end

animal = Animal.new("brown")

puts "The new animal is " + animal.color

 








Related examples in the same category

1.Ruby makes creating readable attribute easier with attr_reader