Table of contents
1.
Introduction
2.
Assignment Operators in Ruby
2.1.
i) += Operator
2.2.
ii) -= Operator
2.3.
iii) *= Operator
2.4.
iv) /= Operator
2.5.
v) %= Operator
2.6.
vi) **= Operator
3.
Example
4.
Output
5.
Frequently Asked Questions
5.1.
What does Ruby's |= stand for?
5.2.
In Ruby, what is a lambda?
5.3.
What does || mean in Ruby?
5.4.
In Ruby, what is a yield?
6.
Conclusion
Last Updated: Mar 27, 2024

Assignment Operators in Ruby

Author SHIVANGI MALL
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Ruby is an excellent language for making desktop programs, static webpages, data processing services, and even automation solutions. Web servers, DevOps, and web scraping and crawling are all examples of where it's employed. When you bring in the Rails application framework's features, you can accomplish even more, especially with database-driven web apps.

In this article, we will discuss some Assignment Operators in Ruby to enable you to go to higher levels of expertise. Before reading this article, you need to have a basic understanding of computer programming terms and ruby.  If this isn't the case, come back here after reading our Ruby article.

Assignment Operators in Ruby

The Assignment operators are used to assign the variable a value. In Ruby, we have the following types of assignment operators:

i) += Operator

This assignment operator is used to add the left and right operands and then assign the result to the left-hand variable.

Syntax

var1 += var2;

 

Above statement is same as var1=var1+var2.

ii) -= Operator

This assignment operator is used to subtract the left with the right operands and then assign the result to the left-hand variable.

Syntax

var1 -= var2;

 

Above statement is same as var1=var1 - var2.

iii) *= Operator

This assignment operator is used to multiply the left with the right operands and then assign the result to the left-hand variable.

Syntax

var1 *= var2;

 

Above statement is same as var1=var1 * var2.

iv) /= Operator

This assignment operator is used to divide the left with the right operands and then assign the result to the left-hand variable.

Syntax

var1 /= var2;

 

Above statement is same as var1=var1 / var2.

v) %= Operator

This assignment operator is used to modulo the left with the right operands and then assign the result to the left-hand variable.

Syntax

var1 %= var2;

 

Above statement is same as var1=var1 % var2.

vi) **= Operator

This assignment operator is used to calculate the exponent and reassign the variable.

Syntax

var1  **= var2;

 

Above statement is same as var1=var1 ** var2.

Example

puts ("assignment operator in Ruby")
x = 30
puts ("abbreviated assignment add")
puts x += 20
puts ("abbreviated assignment subtract")
puts x -= 20
puts ("abbreviated assignment multiply")
puts x *= 2
puts ("abbreviated assignment divide")
puts x /= 3
puts ("abbreviated assignment modulus") 
puts x %= 6
puts ("abbreviated assignment exponent")
puts x **= 2
You can also try this code with Online Ruby Compiler
Run Code

Output

assignment operator in Ruby
abbreviated assignment add
50
abbreviated assignment subtract
30
abbreviated assignment multiply
60
abbreviated assignment divide
20
abbreviated assignment modulus
2
abbreviated assignment exponent
4
You can also try this code with Online Ruby Compiler
Run Code

Frequently Asked Questions

What does Ruby's |= stand for?

It's short for or-equals-to. It first checks whether the value on the left is defined, after which it uses it. If it isn't, use the right-hand value.

In Ruby, what is a lambda?

A lambda is a Ruby object that is comparable to a proc. A lambda, unlike a proc, requires a fixed number of arguments to be supplied to it, and instead of returning immediately, it returns to its calling procedure.

What does || mean in Ruby?

A conditional assignment operator is denoted by the symbol ||=. It functions similarly to =, with the exception that it does nothing if a variable has already been assigned.

In Ruby, what is a yield?

Yield is a Ruby keyword that allows a developer to give an argument to a block. The amount of arguments that can be passed to the block is unlimited.

Conclusion

In this article, we have discussed assignment Operators in Ruby. We looked in-depth using examples.

We hope this article helps you to learn something new. And if you're interested in learning more, see our posts on 8 reasons why Ruby should be your first language!RubyIntroduction to Ruby on RailsRuby on rails for your next web development project!Ruby on Rails.

Visit our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, and much more.!

Thank you for reading this post :-)

Feel free to upvote and share this article if it has been useful for you.

 

Live masterclass