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.