Ruby |= assignment operator Ruby |= assignment operator ruby ruby

Ruby |= assignment operator


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]


Bitwise OR assignment.

x |= y

is shorthand for:

x = x | y

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


With the exception 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