1 00:00:00,840 --> 00:00:03,000 You can chain math operations together. 2 00:00:03,000 --> 00:00:07,927 So if we said 1 + 2 + 3, that would add 1 to 2 and get 3, 3 00:00:07,927 --> 00:00:11,050 then add 3 to that and get 6. 4 00:00:11,050 --> 00:00:14,760 Sometimes the order isn't so straight-forward though, 5 00:00:14,760 --> 00:00:17,120 let's take 1 + 2 * 3. 6 00:00:17,120 --> 00:00:21,530 Should we add 1 + 2 first, or should we take 2 * 3 first? 7 00:00:21,530 --> 00:00:25,570 The answer we get is going to vary based on the order we choose. 8 00:00:25,570 --> 00:00:29,010 Fortunately, order of operations, like you were taught in math class, 9 00:00:29,010 --> 00:00:30,790 applies in Ruby too. 10 00:00:30,790 --> 00:00:35,482 If you didn't have order of operations, you might assume that you add 1 to 2, 11 00:00:35,482 --> 00:00:40,680 getting 3, and then multiply 3 times 3 to get 9, but that's not what we get. 12 00:00:40,680 --> 00:00:42,450 Instead, we get 7. 13 00:00:42,450 --> 00:00:45,960 This is because of the standardized order of operations. 14 00:00:45,960 --> 00:00:48,690 In chained math operations, multiplication and 15 00:00:48,690 --> 00:00:54,440 division operations always come first and addition and subtraction come second. 16 00:00:56,000 --> 00:01:00,820 Ruby respects this concept by following something called operator precedence. 17 00:01:00,820 --> 00:01:01,400 That is, 18 00:01:01,400 --> 00:01:07,000 the evaluation of some operators precedes the evaluation of some other operators. 19 00:01:07,000 --> 00:01:11,290 The multiplication and division operators have higher precedence than addition and 20 00:01:11,290 --> 00:01:13,210 subtraction operators. 21 00:01:13,210 --> 00:01:19,130 So that's why when we evaluate 1 + 2 * 3, we get 7 and not 9. 22 00:01:19,130 --> 00:01:23,960 The multiplication operator has higher precedence than the addition operator, so 23 00:01:23,960 --> 00:01:26,810 we do the multiplication first, giving us 6. 24 00:01:26,810 --> 00:01:30,410 We then add 1 and 6, giving us 7. 25 00:01:30,410 --> 00:01:34,760 But suppose we wanted to ensure that the addition operation occurs first. 26 00:01:34,760 --> 00:01:38,960 If we were working in a math textbook, we'd add parenthesis around the operation 27 00:01:38,960 --> 00:01:41,920 to indicate it should go first no matter what. 28 00:01:41,920 --> 00:01:47,340 (1 + 2), in parenthesis, * 3. 29 00:01:47,340 --> 00:01:50,100 And that same notation works in Ruby. 30 00:01:50,100 --> 00:01:54,230 Ruby will always evaluate math operations within parenthesis first 31 00:01:54,230 --> 00:01:57,550 before it goes on to evaluate the rest of the expression. 32 00:01:57,550 --> 00:02:02,220 So with the parenthesis, (1 + 2) is evaluated first, giving 3. 33 00:02:02,220 --> 00:02:05,940 Then that's multiplied by 3 to give 9. 34 00:02:05,940 --> 00:02:09,210 If you're not comfortable with operator precedence or 35 00:02:09,210 --> 00:02:12,140 you want to learn more, check the teacher's notes for more info.