Define constructor for a class : initialize class « Class « Ruby






Define constructor for a class


#!/usr/bin/env ruby

class Hello
  def initialize( hello )
    @hello = hello
  end
  def hello
    @hello
  end
end

salute = Hello.new( "Hello, myValue!" )
puts salute.hello

 








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.initializes the instance variable @name with the standard initialize method.
4.Initialize three attributes