Call a member method : getter setter « Class « Ruby






Call a member method


class Animal
  def initialize
    @color = "red"
  end

  def get_color
    return @color
  end
end

animal = Animal.new

puts "The new animal is " + animal.get_color

 








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.getter and setter
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 =