-5

I want to know what the difference is between = and ||= operator in ruby. In the documentation, it says = %= { /= -= += |= &= >>= <<= = &&= ||= *= are assignment operators .

sawa
  • 165,429
  • 45
  • 277
  • 381
not 0x12
  • 19,360
  • 22
  • 67
  • 133

2 Answers2

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.

jbr
  • 6,198
  • 3
  • 30
  • 42
Luiz E.
  • 6,769
  • 10
  • 58
  • 98