Introduction
In this blog, we will see what is assignment and abbreviated assignment and then we will learn about Idiom in Ruby with code implementation.
An abbreviated assignment is a shorthand expression that updates the value of a variable by performing other operations (such as addition) on the variable's current value. It is used to express something more concisely.
So, now let us learn about '||=' abbreviated assignment (Idiom in Ruby).
Abbreviated Assignment
The equal operator '=' is used in Ruby as an assignment operator. This is applicable for variables and objects because strings, floats, and integers are all objects in Ruby, and we're always assigning objects.
A combination of an assignment and another operation is an abbreviated assignment. It's most commonly used to increase the value of a variable:
x += 9
'+=' isn't an actual Ruby operator(it is a pseudo-operator), and the expression above is just a shorthand for x = x + 9
Note - Pseudo-Operators are not true operators themselves; they are simply shorthand for expressions that use other operators.
An abbreviated assignment works when there is a single Ivalue on the left and a single value on the right. When the lvalue is a constant, it should not be used since it will reassign the constant, resulting in a warning. When the lvalue is an attribute, the abbreviated assignment can be used.
Note -
- An lvalue is something present on the left hand side of an assignment operator
-
Values on the right hand side of an assignment operator are commonly referred to as rvalues.
To learn more about the abbreviated assignment with various pseudo-operators and code implementation, you can click here.
Now, let us dive deeper into the Idiom in Ruby:
Idiom in Ruby
'||=' assign passd value if the lvalue is nil or false, and no assignment is actually made if the lvalue of '||=' is neither nil nor false.
Example 1:
a = a
a ||= 3
# '||=' sets value 3 to variable
In this example, a is not assigned with any value hence it is being set to 3. If it would have been assigned with any value, ||= idiom would have not assigned the new value to a. That’s the power of ||= idiom.
Interesting right?
Let us see one more example:
Example 2:
b = 6
b = b || 3
# no assignment is actually made
Note - Contrary to popular belief, a ||= b is equivalent to a || a = b instead of being equivalent to a = a || b which lead to the concept Idiom in Ruby as a ||= b behave more like a || a = b than a = a || b.
- a || a = b value is set to variable a only if a is logically false (that is, if it is nil or false)
-
a = a || b sets a to something on each run. If the || comparison's left side is true, the right side need not be validated.
Now with this background detail, we understood ||= Idiom in Ruby.
Code Implementation
# understanding Idiom in Ruby
# ||= Idiom in Ruby with nil lvalue
a = nil
a ||= 3
puts a
# ||= Idiom in Ruby with non nil or false lvalue
b = 6
b ||= 3
puts b
# ||= Idiom in Ruby with true lvalue
c = true
c ||= 9
puts c
Output