5. Math is easy

Let's explore math with Ruby!

Most math you will encounter in web development won't be calculus or advanced math at all. Most of the math will be simple operations that you probably know already.

In the terminal window, let's type in irb:

irb

image.png

irb stands for Interactive Ruby console. In this console, we can write and execute Ruby code. In other words, instead of creating a new Ruby file, saving it, and running it with the ruby command, we can simply run the Ruby code inside this console with irb.

In our irb, let's do some math. Type in the following command and press enter:

3 * 4

You will see that it outputs 12.

You can do any type of math - addition(+), subtraction(-), multiplication(*), and division(/) all work. Try them out in the Ruby console.

Integers

In Ruby, numbers such as 1, 11, and 111 are called integers. Integers are whole numbers without any decimal points.

Integers in Ruby can be tricky, especially when dealing with division. For example, take a look at this example:

100/3
=> 33

The answer should be 33.333, but instead, it gives us 33. In Ruby, it is important to remember that dividing two integers will result in an integer result.

To get the proper answer, we need to use floats.

Floats

A float is short for floating decimal point. In short, floats are numbers with decimal points. You can read more about the theory here, but you don't have to worry about it right now.

In our ruby console, let's type in the following and press enter:

100.0/3.0

Notice how this time, we got this answer:

=> 33.333333333333336

We can also divide an integer with a float, and we get a float answer:

100/3.0
=> 33.333333333333336

Data Types

If you are just beginning in your programming career, you might sometimes accidentally write code like this:

"100" * 3

This won't work since "100" is a string. The result of this would be the following:

=> "100100100"

Notice how this resulted in three "100"s, which is not what we wanted.

This brings us to a topic of data types. We saw that there were three different ways to represent numbers.

  • "10" - String
  • 10 - Integer
  • 10.0 - Float

All of the different data types have different properties. We can ask for the class to see what the data's type is:

"10".class
=> String
10.class
=> Integer
10.0.class
=> Float

Let's create a new file and name it math.rb.

Inside this file, let's type in the following:

puts "2 + 3 is #{2 + 3}"

puts "5 * 10 is #{5 * 10}"

puts "0.4 * 12 is #{0.4 * 12}"

puts "10.0 / 3 is #{10.0 / 3}"

puts "10 / 3 is #{10/3}"

Save the file and run the file:

ruby math.rb

Your output should look like this:

image.png

As you can see, the math equations inside #{} were evaluated and displayed.

You can embed Ruby code inside the hashtag and bracket (#{}), and Ruby will run it and display the results there. **Note that you can only do this when using double quotes (""). This will not work with single quotes (''). More about this in next lesson.

Remainders

Calculating the remainder of two numbers that are divided by each other is an important concept in computer programming. To calculate this, we can use the % (modulo) operator. For example, the remainder of 3 divided by 2 is 1.

3 % 2
=> 1

Modulos are used in computer programming, especially when seeing if a number is divisible by another number:

15 % 5
=> 0

9 % 3
=> 0

10 % 7
=> 3

As you can see, if a number is divisble by another number, then the remainder is 0. This pattern is very useful and will come up when you are trying to solve various problems.

Overview

Note: If you use Ruby v2.3 or earlier, 10.class will return Fixnum instead of Integer. Ruby v2.4 unified Fixnum and Bignum into Integer