Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the operator ||= stand for in Ruby? [duplicate]

Tags:

Possible Duplicate:
What does ||= (or equals) mean in Ruby?

It's hard to search this in Google because it is a symbol, not text.

What does ||= stand for?

And how does it work?

like image 790
Marc Vitalis Avatar asked Sep 07 '09 12:09

Marc Vitalis


People also ask

What does ||= mean in Ruby?

a ||= b is a conditional assignment operator. It means: if a is undefined or falsey, then evaluate b and set a to the result. Otherwise (if a is defined and evaluates to truthy), then b is not evaluated, and no assignment takes place.

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

Triple Equals Operator (More Than Equality) Our last operator today is going to be about the triple equals operator ( === ). This one is also a method, and it appears even in places where you wouldn't expect it to. Ruby is calling the === method here on the class.

What does operator mean in Ruby?

An operator is a symbol that represents an operation to be performed with one or more operand. Operators are the foundation of any programming language. Operators allow us to perform different kinds of operations on operands. There are different types of operators used in Ruby as follows: Arithmetic Operators.

What are the types operators in Ruby?

Ruby Arithmetic OperatorsSubtraction − Subtracts right hand operand from left hand operand. Multiplication − Multiplies values on either side of the operator. Division − Divides left hand operand by right hand operand. Modulus − Divides left hand operand by right hand operand and returns remainder.


1 Answers

It assigns a value if not already assigned. Like this:

a = nil a ||= 1  a = 1 a ||= 2 

In the first example, a will be set to 1. In the second one, a will still be 1.

like image 170
peku Avatar answered Sep 16 '22 16:09

peku