Define getter for a attribute : getter setter « Class « Ruby






Define getter for a attribute


#!/usr/bin/env ruby

class Horse

  def initialize( name )
    @name = name
  end

  def horse_name
    @name
  end

end

horse = Horse.new( "Doc Bar" )
puts horse.horse_name # => Doc Bar

 








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.Call a member method
3.getter and setter
4.Accessors
5.Make an attribute readable and writable by using attr_reader, attr_writer
6.Provide the getter
7.Setter with calculation
8.Setter with =