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 5 years have passed since last update.

代入演算子"||="の使い方

0
Last updated at Posted at 2019-12-23

||=の意味

||=は代入演算子である。

a ||= b → a = a || b

他にも使い方がある
a = a + b → a += b
a = a - b → a -= b
a = a * b → a *= b
a = a / b → a /= b

||=の使い方

「変数の値がnilなら変数に代入するが、nilでなければ代入しない (変数の値を変えない)」という操作を行いたい時に使う。

>> @foo 
=> nil
>> @foo = @foo || "bar"
=> "bar"
>> @foo = @foo || "baz"
=> "bar"
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?