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
You can chain math operations together. But sometimes the order you carry those operations out affects the final result. Let's look at the rules surrounding the order of operations.
 You can chain operations together:
1 + 2 + 3
 Order of operations you were taught in math class applies in Ruby too.
 Take
1 + 2 * 3
. If you didn't have order of operations, you'd assume that you add1
to2
, getting 3, and then multiply3 * 3
to get9
. But instead we get7
.  This is because of order of operations. In chained math operations, multiplication and division operations always come first, and addition and subtraction second:
* / + 
 Ruby respects this concept by following something called operator precedence. That is, the evaluation of some operators precedes the evaluation of some other operators. The multiplication and division operators have higher precedence than addition and subtraction operators.
 So that's why when we evaluate
1 + 2 * 3
, we get7
and not9
. The multiplication operator has higher precedence than the addition operator, so we do the multiplication first, giving us6
. We then add1
and6
, giving us 7.  Suppose we wanted to ensure that the addition operation occurs first. If we were working in a math textbook, we'd add parentheses around the operation to indicate it should go first, no matter what:
(1 + 2) * 3
.  And that same notation works in Ruby. Ruby will always evaluate math operations within parentheses first, before it goes on to evaluate the rest of the expression. So with the parentheses,
1 + 2
is evaluated first, giving3
, and then that's multiplied by3
to give9
.
Helpful links:

0:00
You can chain math operations together.

0:03
So if we said 1 + 2 + 3, that would add 1 to 2 and get 3,

0:07
then add 3 to that and get 6.

0:11
Sometimes the order isn't so straightforward though,

0:14
let's take 1 + 2 * 3.

0:17
Should we add 1 + 2 first, or should we take 2 * 3 first?

0:21
The answer we get is going to vary based on the order we choose.

0:25
Fortunately, order of operations, like you were taught in math class,

0:29
applies in Ruby too.

0:30
If you didn't have order of operations, you might assume that you add 1 to 2,

0:35
getting 3, and then multiply 3 times 3 to get 9, but that's not what we get.

0:40
Instead, we get 7.

0:42
This is because of the standardized order of operations.

0:45
In chained math operations, multiplication and

0:48
division operations always come first and addition and subtraction come second.

0:56
Ruby respects this concept by following something called operator precedence.

1:00
That is,

1:01
the evaluation of some operators precedes the evaluation of some other operators.

1:07
The multiplication and division operators have higher precedence than addition and

1:11
subtraction operators.

1:13
So that's why when we evaluate 1 + 2 * 3, we get 7 and not 9.

1:19
The multiplication operator has higher precedence than the addition operator, so

1:23
we do the multiplication first, giving us 6.

1:26
We then add 1 and 6, giving us 7.

1:30
But suppose we wanted to ensure that the addition operation occurs first.

1:34
If we were working in a math textbook, we'd add parenthesis around the operation

1:38
to indicate it should go first no matter what.

1:41
(1 + 2), in parenthesis, * 3.

1:47
And that same notation works in Ruby.

1:50
Ruby will always evaluate math operations within parenthesis first

1:54
before it goes on to evaluate the rest of the expression.

1:57
So with the parenthesis, (1 + 2) is evaluated first, giving 3.

2:02
Then that's multiplied by 3 to give 9.

2:05
If you're not comfortable with operator precedence or

2:09
you want to learn more, check the teacher's notes for more info.
You need to sign up for Treehouse in order to download course files.
Sign up