0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Ruby 入門 [インスタンス変数を簡単に操作]

Posted at

● インスタンス変数を簡単に操作する

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
↪️カフェラテ
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?