Code Editor
Learn
Introduction to Ruby

Math

Ruby isn't limited to simple expressions of assignment like my_num = 100; it can also do all the math you learned about in school.

There are six arithmetic operators we're going to focus on:

Addition (+)

Subtraction (-)

Multiplication (*)

Division (/)

Exponentiation (**)

Modulo (%)

The only ones that probably look weird to you are exponentiation and modulo. Exponentiation raises one number (the base) to the power of the other (the exponent). For example, 2**3 is 8, since 2**3 means "give me 2 * 2 * 2" (2 multiplied together 3 times). 3**2 is 9 (3 * 3), and so on.

Modulo returns the remainder of division. For example, 25 % 7 would be 4, since 7 goes into 25 three times with 4 left over.

Report a Bug
If you see a bug or any other issue with this page, please report it here.