Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Abbreviated Assignment
3.
Code Implementation
4.
Frequently Asked Questions
4.1.
What is an assignment operator?
4.2.
What are Expression and Operator?
4.3.
What are Pseudo-Operators?
4.4.
What is an lvalue?
4.5.
How many different forms of assignment expressions are there in Ruby?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Abbreviated Assignment in Ruby

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

Introduction

Ruby

In this blog, we will see what is assignments and abbreviated assignments and then we will learn about abbreviated assignments using different pseudo-operators 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 in detail.

Abbreviated Assignment

The equal operator "=" is used in Ruby as an assignment operator. This applies to 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.

An abbreviated assignment works when there is a single lvalue 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

 

Abbreviated Assignment pseudo-operators

Assignment

Expansion

x += y

x = x + y

x -= y

x = x - y

x *= y

x = x * y

x /= y

x = x / y

x %= y

x = x % y

x **= y

x = x ** y

x &&= y

x = x && y

x ||= y

x = x || y

x &= y 

x = x & y

x |= y

x = x | y

x ^= y

x = x ^ y

x <<= y

x = x << y

x >>= y

x = x >> y

Pseudo-Operators are not true operators themselves; they are simply shorthand for expressions that use other operators.

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

Code Implementation

puts ("'=' Assignment Operator in Ruby")
puts x = 49

puts ("'+=' Abbreviated Assignment in Ruby : Add")
puts x += 20

puts ("'-=' Abbreviated Assignment in Ruby : Subtract")
puts x -= 20

puts ("'*=' Abbreviated Assignment in Ruby : Multiply")
puts x *= 4

puts ("'/=' Abbreviated Assignment in Ruby : Divide")
puts x /= 4

puts ("'%=' Abbreviated Assignment in Ruby : Modulus") 
puts x %= 6

puts ("'**=' Abbreviated Assignment in Ruby : Exponent")
puts x **= 4


Output

'=' Assignment Operator in Ruby
49
'+=' Abbreviated Assignment in Ruby : Add
69
'-=' Abbreviated Assignment in Ruby : Subtract
49
'*=' Abbreviated Assignment in Ruby : Multiply
196
'/=' Abbreviated Assignment in Ruby : Divide
49
'%=' Abbreviated Assignment in Ruby : Modulus
1
'**=' Abbreviated Assignment in Ruby : Exponent
1

Frequently Asked Questions

What is an assignment operator?

The equal operator "=" is used in Ruby as an assignment operator. This applies to variables and 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 expressions that use other operators. For example, '+=' and '-=' are Pseudo-Operators.

What is an lvalue?

An lvalue is something present on the left-hand side of an assignment operator.

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 Abbreviated Assignment in Ruby. We started with a brief introduction about the simple assignment and the abbreviated assignment and learned how to use it with code implementation.

After reading about the Abbreviated Assignment 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 languageRuby vs PythonDocumentation of RubyOfficial Ruby FAQ, and Learn Ruby with the Edgecase Ruby Koans.

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! 

Happy Learning!

Coding Ninjas
Previous article
Assigning to Attributes and Array Elements in Ruby
Next article
What is The ||= Idiom in Ruby
Live masterclass