initializes the instance variable @name with the standard initialize method. : initialize class « Class « Ruby






initializes the instance variable @name with the standard initialize method.


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.This code starts by creating the Animal class, and then adds a method named initialize:
2.the code adds an instance variable named @color that stores the color of the animal to the initialize method:
3.Define constructor for a class
4.Initialize three attributes