Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Now that you know how to create numbers, it's time to operate on them! This video shows you how to perform mathematical functions on numbers in your ruby programs.

0:00
[? upbeat music ?] [Treehouse]

0:08
In this video we'll be learning about the different operators available to us in Ruby.

0:14
These include things like addition, subtraction, multiplication, and more.

0:20
Now we're going to be talking about operators in Ruby.

0:24
Operators let you perform addition, subtraction, multiplication, and more when working with numbers.

0:31
Let's get started and I'll show you some examples.

0:34
Let's say we have the number 10.

0:37
If we wanted to add 10 to that we could use the addition operator.

0:41
We do that by using the plus sign,

0:46
and as you can see we get back 20, just like we would expect.

0:51
Next we'll check out the subtraction operator,

0:57
and as you can see subtracting 10 from 10 gives us 0.

1:02
There's also the multiplication operator

1:05
and that gives us 100, just like we'd expect.

1:09
We'll also check out the division operator.

1:12
This is represented using a slash,

1:16
and that also functions just like we would expect.

1:21
An operator you'll probably be familiar with from working in other languages is the modulus operator.

1:27
This is represented using the percent sign.

1:30
This divides the left operator by the right operator

1:34
and returns the remainder.

1:38
In the case of dividing 10 by 10 there is no remainder,

1:43
so we get back 0.

1:45
Finally, we have the exponent operator,

1:48
and this performs exponential calculations.

1:52
We could do 10 to the tenth power,

1:56
and we get back exactly what we would expect.

2:00
Each of these operators also works with assignment.

2:03
Let's say we had a variable.

2:05
We're going to call it number and we'll set that equal to 10.

2:11
If we wanted to add 10 to that number

2:15
we could use the addition operator and an equal sign.

2:22
That returns 20.

2:24
It also adds 10 to our original variable,

2:30
and we can see here that 20 is the current value of number.

2:36
This works for each of the different operators.

2:57
It also works for the exponential operator.

3:04
Something else to keep in mind when dealing with numbers in Ruby

3:07
is the order of operations.

3:10
This functions just as it does in regular math, so let's check it out.

3:17
In this case, Ruby will use the multiplication operator first

3:22
and the subtraction operator second,

3:25
and that functions exactly like we'd expect.

3:28
[? upbeat music ?]

3:30
In this video we learned how to operate on the numbers we created in our first video.

3:36
In our next video we'll learn how to compare numbers to each other.

3:39
[Treehouse]
You need to sign up for Treehouse in order to download course files.
Sign up