Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby |= assignment operator

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

How the |= assignment operator works?

like image 949
A B Avatar asked Dec 19 '11 23:12

A B


People also ask

What does |= mean in Ruby?

Ruby has an or-equals operator that allows a value to be assigned to a variable if and only if that variable evaluates to either nil or false .

What is the meaning of |= operator?

The bitwise OR assignment operator ( |= ) uses the binary representation of both operands, does a bitwise OR operation on them and assigns the result to the variable.

What is an assignment operator in Ruby?

In Ruby assignment operator is done using the equal operator "=". This is applicable both for variables and objects, as strings, floats, and integers are actually objects in Ruby, you're always assigning objects.

What is the name of this operator === in Ruby?

In Ruby, the === operator is used to test equality within a when clause of a case statement. In other languages, the above is true. To my knowledge, Ruby doesn't have true operators, they are all methods which are invoked on the LHS of the expression, passing in the RHS of the expression.


1 Answers

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] 
like image 163
mlm Avatar answered Sep 29 '22 12:09

mlm