Progate learning notes Click here for instance methods and instance variables [Ruby] method, instance method ... [Ruby] classes, instance variables, instances, etc ...
State of handling instance variables in instance method ↓
class Menu
#Instance variables
attr_accessor :name
attr_accessor :price
#Handle instance variables in instance methods
# 「self.By using "variable name", you can handle instance variables in the instance method.
def info
return "#{self.name}: ¥#{self.price}"
end
end
#instance
menu = Menu.new()
menu.name = "Cream bun"
menu.price = 300
puts menu.info
output
Cream bun: ¥300
Reference: Progate