Ruby Arithmetic Operators Tutorial

In this section, we will learn what the Arithmetic 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 Arithmetic Operator in Ruby?

In Ruby, those operators that are used to run mathematical operations are called Arithmetic Operators.

For example, the multiplication operator is used for multiplying values to each other, division operator is used for dividing one value to another, addition operator is used for adding two values together, etc.

Arithmetic Operators List in Ruby

In the table below you can see the list of arithmetic operators supported in Ruby.

Operator

Symbol

Description

Addition Operator

+

Using this operator, we can add two values together.

Subtraction Operator

Using this operator, we can subtract two values from each other.

Multiplication Operator

*

Using this operator, we can multiply two values to each other.

Division Operator

/

Using this operator, we can subtract two values from each other.

Exponentiation Operator

**

This operator is used to raise the value of the left operand to the power of the right operand and then return the result.

Modulus Operator

%

Using this operator, we can run modulus operation on operands.

Ruby Addition `+` Operator:

The addition operator with the symbol `+` is used to add to values to each other and return the result.

Addition `+` Operator Syntax:

left_operand + right_operand

Example: using addition `+` operator in Ruby

res = 10 + 20

puts res

Output:

30

Ruby Subtraction `-` Operator:

The subtraction operator with the symbol `-` is used to subtract one value from another and return the result.

Subtraction `-` Operator Syntax:

left_operand - right_operand

Example: using subtraction `-` operator in Ruby

val1 = 10

val2 = 5

res = val1 – val2

puts res

Output:

5

Ruby Multiplication `*` Operator:

The multiplication operator with the symbol `*` is used to multiply two values to each other and return the result.

Multiplication `*` Operator Syntax:

left_operand * right_operand

Example: using multiplication `*` operator in Ruby

val1 = 10

val2 = 5

res = val1 * val2

puts res

Output:

50

Ruby Division `/` Operator:

The division operator with the symbol `/` is used to divide the left_operand by the right_operand and return the result.

Division `/` Operator Syntax:

left_operand / right_operand

Example: using division operator in Ruby

val1 = 10

val2 = 5

res = val1 / val2

puts res

Output:

2

Division with Ruby Fixnum data type value

Note that when dividing two whole numbers (of type Fixnum) by each other, the result will become a Fixnum! That means even if the result has decimal numbers in it, it will be stripped away and only the whole number part of the result returns instead.

So if we want to get the decimal part as well, one of the involved operands must become of type float then.

One of the easiest way to convert a Fixnum type to Float is by calling the `to_f` method on the target integer value.

Example: using fixnum values in division operation

val1 = 10.to_f

val2 = 3

res = val1 / val2

puts res

Output:

3.3333333333333335

In the last example, if we remove the `to_f` method and divide the two values by each other, the result will be only the value 3.

Ruby Modulus `%` Operator:

The modulus operator with the symbol `%` is used to find the remainder value (whole number) when dividing one value by another one.

Modulus `%` Operator Syntax:

left_operand % right_operand

Example: using modulus operator in Ruby

val1 = 14

val2 = 3

res = val1 % val2

puts res

Output:

2

Ruby Exponent `**` Operator:

The exponent operator with the symbol `**` is used to run exponential (power) operation on the operands involved.

Exponent `**` Operator Syntax:

left_operand ** right_operand

Example: using exponent operator in Ruby

val1 = 2

val2 = 3

res = val1 ** val2

puts res

Output:

8

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies