LoginSignup
3
2

More than 5 years have passed since last update.

Ruby演算子 a ||= 1 の意味 (or イコール)

Last updated at Posted at 2016-04-21

見慣れない演算子があったのでメモ。

a ||= 1

検索をしてみると「aが偽か未定義ならば1を代入。初期化時のイディオムの一種。」とのこと。
わかりづらい。
pry で試してみる。

[2] pry(main)> a ||= 1
=> 1
[3] pry(main)> a = 5
=> 5
[4] pry(main)> a ||= 1
=> 5

a が既に定義されている場合は代入できないのね。

3
2
2

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
3
2