● インスタンス変数を簡単に操作する
class Drink
def name ———#①attr_reader :name
@name
end
def name=(text) ———#②attr_write :name
@name = text
end
end
drink = Drink.new
drink.name="カフェラテ"
puts drink.name
↪️カフェラテ
①と②の省略できる書き方
class Drink
attr_reader :name ———#①
attr_writer :name ————#②
end
drink = Drink.new
drink.name="カフェラテ"
puts drink.name
↪️カフェラテ
①と②を合体させたattr_accessor
class Drink
attr_accessor :name
end
drink = Drink.new
drink.name="カフェラテ"
puts drink.name
↪️カフェラテ