Ruby - assignment operators example
The example below shows the usage of assignment and compound assignment operators:
- = Assignment operator
- += Addition AND assignment operator
- -= Subtraction AND assignment operator
- *= Multiply AND assignment operator
- /= Division AND assignment operator
- %= Modulo AND assignment operator
- **= Exponent AND assignment operator
a = 25.0 puts "a = #{a} \n\n" #Addition AND assignment operator a += 5 puts "a += 5 makes a = #{a}" #Subtraction AND assignment operator a -= 8 puts "a -= 8 makes a = #{a}" #Multiply AND assignment operator a *= 4 puts "a *= 4 makes a = #{a}" #Division AND assignment operator a /= 2 puts "a /= 2 makes a = #{a}" #Modulo AND assignment operator a %= 5 puts "a %= 5 makes a = #{a}" #Exponent AND assignment operator a **= 3 puts "a **= 3 makes a = #{a}"
The output of the above code will be:
a = 25.0 a += 5 makes a = 30.0 a -= 8 makes a = 22.0 a *= 4 makes a = 88.0 a /= 2 makes a = 44.0 a %= 5 makes a = 4.0 a **= 3 makes a = 64.0
❮ Ruby - Operators