Initialize three attributes : initialize class « Class « Ruby






Initialize three attributes


class Person
  attr_reader :name, :age, :occupation

  def initialize(name, age, occupation)
    @name, @age, @occupation = name, age, occupation
  end

  def isIn?
    true
  end
end

jimmy = Person.new('J', 21, 'reporter')
clark = Person.new('C', 35, 'reporter')
jimmy.isIn?                                     # => true
clark.isIn?                                     # => true

 








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.Define constructor for a class