-5

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

sawa
  • 160,959
  • 41
  • 265
  • 366
ox12
  • 17,618
  • 20
  • 63
  • 121

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,033
  • 12
  • 13
Santhosh
  • 26,663
  • 9
  • 73
  • 86
  • 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
  • 5,968
  • 3
  • 29
  • 41
Luiz E.
  • 6,177
  • 8
  • 51
  • 92