32

Found table http://phrogz.net/programmingruby/language.html#table_18.4 but unable to find description for |=

How the |= assignment operator works?

39

Bitwise OR assignment.

x |= y

is shorthand for:

x = x | y

(just like x += y is shorthand for x = x + y).

| improve this answer | |
  • Bah, my bad, thanks for the correction. Updated my answer to reflect bitwise or, not logical or. – mynameiscoffey Dec 19 '11 at 23:47
46

When working with arrays |= is useful for uniquely appending to an array.

>> x = [1,2,3]
>> y = [3,4,5]

>> x |= y
>> x
=> [1, 2, 3, 4, 5]
| improve this answer | |
10

With the expection of ||= and &&= which have special semantics, all compound assignment operators are translated according to this simple rule:

a ω= b

is the same as

a = a ω b

Thus,

a |= b

is the same as

a = a | b
| improve this answer | |
  • 1
    In what ways does x ||= y differ from x = x || y ? – mynameiscoffey Dec 20 '11 at 15:57
2

It is listed in the link you provided. It's an assignment combined with bitwise OR. Those are equivalent:

a = a | b
a |= b
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.