Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does single vertical bar and equals bar mean in Ruby? [duplicate]

Tags:

ruby

In ruby, what does |= operator do?

Example:

a = 23
a |= 3333 # => 3351
like image 589
konnigun Avatar asked Jul 11 '13 10:07

konnigun


People also ask

What does a single vertical bar represent?

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.

What are vertical bars in Ruby?

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.

What does || do in Ruby?

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 does &: mean in Ruby?

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.


2 Answers

The single vertical bar is a bitwise OR operator.

a |= 3333 is equivalent to a = a | 3333

like image 130
revolver Avatar answered Oct 17 '22 18:10

revolver


|= 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

like image 32
Arup Rakshit Avatar answered Oct 17 '22 17:10

Arup Rakshit