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 1 year has passed since last update.

インスタンス変数の簡略化メモ

Last updated at Posted at 2022-01-06

●インスタンス変数
3段活用
以下、全て出力結果は同じになる

①基本の書き方
class Test
  # セッター
  def name=(name)
    @name = name
  end

  # ゲッター
  def name
    @name
  end
end

②セッターとゲッターをそれぞれ簡略化

class Test
  # セッター
  attr_writer :name
  # ゲッター
  attr_reader :name
end

③全て簡略化

class Test
  attr_accessor :name
end

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?