Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Ruby Basics!
You have completed Ruby Basics!
Preview
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:
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You can chain math operations together.
0:00
So if we said 1 + 2 + 3,
that would add 1 to 2 and get 3,
0:03
then add 3 to that and get 6.
0:07
Sometimes the order isn't so
straight-forward though,
0:11
let's take 1 + 2 * 3.
0:14
Should we add 1 + 2 first, or
should we take 2 * 3 first?
0:17
The answer we get is going to vary
based on the order we choose.
0:21
Fortunately, order of operations,
like you were taught in math class,
0:25
applies in Ruby too.
0:29
If you didn't have order of operations,
you might assume that you add 1 to 2,
0:30
getting 3, and then multiply 3 times 3
to get 9, but that's not what we get.
0:35
Instead, we get 7.
0:40
This is because of the standardized
order of operations.
0:42
In chained math operations,
multiplication and
0:45
division operations always come first and
addition and subtraction come second.
0:48
Ruby respects this concept by following
something called operator precedence.
0:56
That is,
1:00
the evaluation of some operators precedes
the evaluation of some other operators.
1:01
The multiplication and division operators
have higher precedence than addition and
1:07
subtraction operators.
1:11
So that's why when we evaluate 1 + 2 * 3,
we get 7 and not 9.
1:13
The multiplication operator has higher
precedence than the addition operator, so
1:19
we do the multiplication first,
giving us 6.
1:23
We then add 1 and 6, giving us 7.
1:26
But suppose we wanted to ensure that
the addition operation occurs first.
1:30
If we were working in a math textbook,
we'd add parenthesis around the operation
1:34
to indicate it should go
first no matter what.
1:38
(1 + 2), in parenthesis, * 3.
1:41
And that same notation works in Ruby.
1:47
Ruby will always evaluate math
operations within parenthesis first
1:50
before it goes on to evaluate
the rest of the expression.
1:54
So with the parenthesis,
(1 + 2) is evaluated first, giving 3.
1:57
Then that's multiplied by 3 to give 9.
2:02
If you're not comfortable
with operator precedence or
2:05
you want to learn more,
check the teacher's notes for more info.
2:09
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up