Progate learning notes The initialize method is called immediately after the instance is created
class Menu
attr_accessor :name
attr_accessor :price
#Called after the menu instance is created
def initialize
self.name = "soup" # インスタンス変数nameにsoupという情報が入る
self.price = 500 #The instance variable price contains the information 500
end
def info
return "#{self.name}: ¥#{self.price}"
end
end
#A menu instance is created from the Menu class
menu = Menu.new
puts menu.info
result
soup: ¥500
class Menu
attr_accessor :name
attr_accessor :price
def initialize(name:, price:)
self.name = name
self.price = price
end
def info
return "#{self.name}: ¥#{self.price}"
end
end
#Set the information in the argument of initialize method here
menu = Menu.new(name:"curry", price:850)
puts menu.info
result
curry: ¥850
Reference: Progate
Recommended Posts