Ruby Assignment Operators Tutorial

In this section, we will learn what the Assignment Operators are and how to use them in Ruby.

Note: in this section we’re assuming you’re familiar with the Ruby operators in general.

What Is Assignment Operator in Ruby?

The Ruby Assignment Operators are used to assign a value to a variable.

For example, one of the assignment operators is `=` operator which we know it as the equal sign!

The Assignment operator `=` is used to assign a value to a variable and if there’s a value already in the target variable, it will be replaced by the new value as a result of this operation.

Note that in the world of programming, the `=` symbol represents the assignment operator and the `==` operator is used for checking if two values are equal or not. (You’ll learn more about the equal operator in late section)

List of assignment operators in Ruby

Operator Symbol
Assignment operator =
Addition and Assignment operator +=
Subtraction and Assignment operator -=
Multiplication and Assignment operator *=
Division and Assignment operator /=
Exponentiation and Assignment Operator **=
Modulus and Assignment Operator %=

Ruby Assignment `=` Operator:

The assignment operator is used to assign a value to a variable.

Assignment `=` Operator Syntax:

LValue = RVlaue

The LValue must be a variable but the RValue could be anything from raw value, the value of a variable or the return value of a function, etc.

Example: using assignment operator in Ruby

val1 = 2

val2 = 3

puts val1, val2

Output:

2

3

Ruby Addition Assignment `+=` Operator

The Addition Assignment operator is used to take the current value of a variable, add a new value to it and then reassign the result value to the target variable.

Addition Assignment `+=` Operator Syntax:

LValue += RVlaue

The LValue must be a variable, but the RValue could be anything from raw value, the value of a variable or the return value of a function etc.

Example: using addition assignment operator in Ruby

val1 = 2

val1 += 3

puts val1

Output:

5

Ruby Subtract Assignment `-=` Operator

The Subtract Assignment operator is used to take the value of a variable, subtract that value from another value and then assign the result to the variable itself.

Subtract Assignment `-=` Operator Syntax:

LValue -= RVlaue

The LValue must be a variable, but the RValue could be anything from raw value, the value of a variable or the return value of a function etc.

Example: using subtract assignment operator in Ruby

val1 = 2

val1 -= 3

puts val1

Output:

-1

Ruby Multiplication Assignment `*=` Operator

The multiplication assignment operator is used to multiply the value of a variable to another value and then reassign the result to the variable itself.

Multiplication Assignment `*=` Operator Syntax:

LValue *= RVlaue

The LValue must be a variable, but the RValue could be anything from raw value, the value of a variable or the return value of a function etc.

Example: using multiplication assignment operator in Ruby

val1 = 2

val1 *= 3

puts val1

Output:

6

Ruby Division Assignment `/=` Operator

The Division Assignment operator is used to divide the value of a variable to another value and then assign the result to the variable itself again.

Division Assignment `/=` Operator Syntax:

LValue /= RVlaue

The LValue must be a variable, but the RValue could be anything from raw value, the value of a variable or the return value of a function etc.

Example: using division assignment operator in Ruby

val1 = 12

val1 /= 3

puts val1

Output:

4

Ruby Conditional Assignment `||=` Operator

The Conditional Assignment operator is used to check the current value of a variable and if that value was a falsy value, then replace it with the right operand (the value on the right side of this operator).

Note: if the value of a variable is either false (which is a boolean value) or the value ‘nil`, it is considered as a falsy value. Other than these values, any other value is considered as truthy and so this operator won’t change that value with the right operand.

Conditional Assignment `||=` Operator Syntax:

LValue ||= RVlaue

The LValue must be a variable, but the RValue could be anything from raw value, the value of a variable or the return value of a function, etc.

Example: using conditional assignment operator in Ruby

val1 = 10

val1 ||= 3

puts val1

Output:

10

As you can see, the value 10 which was the initial value of the `val1` variable is printed to the output stream. This is because the initial value was not a falsy value and so it wasn’t replaced with the Rvalue.

Another example:

val1 = false

val1 ||= 3

puts val1

Output:

3

Note that here the value of the variable was replaced with the value 3. This is because the current value of the variable is a falsy value and hence it is replaced with the RValue.

Ruby Modulus Assignment `%=` Operator

The modulus assignment operator is used to find the remainder if the current value of the LValue (the variable on the left side of this operator) is divided by another value (Rvalue) and then assign the result to the current variable (LValue).

Modulus Assignment `%=` Operator Syntax:

LValue /= RVlaue

The LValue must be a variable, but the RValue could be anything from raw value, the value of a variable or the return value of a function, etc.

Example: using modulus assignment operator in Ruby

val1 = 10

val1 %= 6

puts val1

Output:

4

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies