In ruby, what does |=
operator do?
Example:
a = 23
a |= 3333 # => 3351
As with computing, mathematics uses the vertical bar in a variety of ways. For example, it can indicate the absolute value of a number, as in |-5| = 5. This means that the absolute value of -5 is 5. The vertical bar can also express divisibility.
The vertical lines are use to denote parameters to the block. The block is the code enclosed within { }. This is really the syntax of the ruby block, parameters to the block and then the code.
The || operator returns the Boolean OR of its operands. It returns a true value if either of its operands is a true value. If both operands are false values, then it returns a false value. Like && , the || operator ignores its righthand operand if its value has no impact on the value of the operation.
What you are seeing is the & operator applied to a :symbol . In a method argument list, the & operator takes its operand, converts it to a Proc object if it isn't already (by calling to_proc on it) and passes it to the method as if a block had been used.
The single vertical bar is a bitwise OR operator.
a |= 3333
is equivalent to a = a | 3333
|=
is called syntactic sugar.
In Ruby a = a | 3333
is the same as a |= 3333
.
|
means
Binary OR Operator copies a bit if it exists in either operand.Ruby Bitwise Operators
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With