Found table http://phrogz.net/programmingruby/language.html#table_18.4 but unable to find description for |=
How the |= assignment operator works?
Found table http://phrogz.net/programmingruby/language.html#table_18.4 but unable to find description for |=
How the |= assignment operator works?
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
x ||= y differ from x = x || y ?||= and &&= are not exceptions. They both seem to function identically to a = a || b and a = a && b, respectively. If there are any exceptions to this, can you please provide an example?a is a setter (e.g. foo.bar=), then a = a || b will always call both the setter and the getter, whereas a ||= b will only call the setter if a is falsey (or truthy in the case of &&=). In other words: I can write a program which can output whether you used ||= or = … || …, therefore the two are not equivalent.a ω= b for all operators ω are evaluated AS-IF they were written as a = a ω b, but that is only true for operators other than || and &&.