Math


You can use Ruby to perform mathematical calculations. The four standard operators are probably familiar to you:

  • + for addition
  • - for subtraction
  • * for multiplication
  • / for division

Two other less-familiar but important operators are ** and %.

** is used for exponents; 5**2 is 5 to the power of 2, which is 25.

% is a little weird. In Ruby, it isn't percent, it is modulo. Modulo is like division, but it gives you just the remainder of a division problem.

moduloAnswer=5%2 #moduloAnswer=1
exponentAnswer=2**3 #exponentAnswer=8

Exercise

Use math operations to turn old variables into new ones that the if statement will print true to the console when run.


Copyright © learnrubyonline.org. Read our Terms of Use and Privacy Policy