LoginSignup
0
0

More than 1 year has passed since last update.

【Ruby】自己代入演算子「||=」

Posted at
hoge ||= 5

「||」演算子の自己代入演算子。hogeが偽か未定義ならhogeに5を代入する。
わかりやすく書き直すと↓

hoge = hoge || 5

hogeがfalse,nil以外 → 5を返す
hogeがfalse → hogeに5を代入する
hogeがnil(未定義) → hogeに5を代入する

参考

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