Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Abbreviated Assignment
2.1.
Idiom in Ruby
2.1.1.
Code Implementation
2.2.
Explanation 
3.
Frequently Asked Questions
3.1.
What is an assignment operator?
3.2.
What are Expression and Operator?
3.3.
What are Pseudo-Operators?
3.4.
How many different forms of assignment expressions are there in Ruby?
4.
Conclusion
Last Updated: Mar 27, 2024
Medium

What is The ||= Idiom in Ruby

Author Sanchit Kumar
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

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

Output Image

Explanation 

Output Image

Output Image

Output Image

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Frequently Asked Questions

What is an assignment operator?

The equal operator "=" is used in Ruby as an assignment operator. This is applicable for variables as well as objects because strings, floats, and integers are all objects in Ruby, and we're always assigning objects.

What are Expression and Operator?

An expression is a section of Ruby code that the Ruby interpreter can evaluate to produce a value. An operator is a token or a particular symbol in the Ruby language representing an operation (such as addition or comparison) performed on one or more operands. 

What are Pseudo-Operators?

Pseudo-Operators are not actual operators themselves; they are simply shorthand for expressions that use other operators. For example, '+=' and '-=' are Pseudo-Operators

How many different forms of assignment expressions are there in Ruby?

There are three different forms of assignment expressions in Ruby which are simple assignment (x = 1), abbreviated assignment (x += 1) and parallel assignment (x,y,z = 1,2,3).

Conclusion

This article extensively discussed the '||=' Idiom in Ruby. We started with a brief introduction about the simple assignment, the abbreviated assignment in general, and the '||='( Idiom in Ruby) abbreviated assignment in particular.

After reading about the What is The ||= Idiom in Ruby? are you not feeling excited to read/explore more articles on the topics related to Ruby? Don't worry; Coding Ninjas has you covered. To learn, see History of RubyExpressions and Operators in Ruby8 reasons why Ruby should be your first languageDocumentation of Ruby, and Ruby vs Python.

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc., you must look at the problemsinterview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Ninjas Logo

Previous article
Abbreviated Assignment in Ruby
Next article
Parallel Assignment in Ruby
Live masterclass