I want to know what the difference is between = and ||= operator in ruby. In the documentation, it says = %= { /= -= += |= &= >>= <<= = &&= ||= *= are assignment operators .
Asked
Active
Viewed 92 times
2 Answers
1
a ||= b
is short for a = a || b
In ruby nil
, evaluates to false. So if a is nil
or false
, a will be assigned b's value

Aaron Cronin
- 2,093
- 14
- 13

Santhosh
- 28,097
- 9
- 82
- 87
-
This is wrong. There is no simple expansion of `||=` like there is with `+=` and the others, and *if* you want to find a simple expansion that is at least somewhat close to the actual semantics, it would be `a || a = b`, not `a = a || b`. – Jörg W Mittag Jul 31 '13 at 19:49
-1
The variable before ||=
will receive the value after the operator if it is != nil
.