Zoonk icon

How to perform simple calculations using Ruby

Created by Will Ceolin
Created on Feb 6, 20 - Updated on May 10, 20

Doing simple calculations in Ruby is similar to other programming languages. First of all, open your Interactive Ruby (IRB) environment.

Sum

5+2
=> 7

Subtraction

5-2
=> 3

Multiplication

5*2
=> 10

Division

10/2
=> 5

Power

3**3
=> 27

Square root

For calculating the square root of a number, you can use Ruby's module:

Math.sqrt(9)
=> 3.0