Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7day trial. Sign In Enroll
Start a free Courses trial
to watch this video
If we're going to multiply the quantity of widgets by the price to get the total, we're going to need to know how to do math operations. Really, math operations like addition, subtraction, multiplication, and division are central to almost any programming language, so most languages make them easy to do. Ruby is no exception.
Math operators take the values to their left and right and perform a math operation on them. The four most common operators add, subtract, multiply, or divide values.
+

*
/
Let's launch irb
, so we can try out a bunch of operations and immediately see the results.
$ irb
2.3.0 :001 > 2 + 3
=> 5
2.3.0 :002 > 12  4
=> 8
2.3.0 :003 > 5 * 8
=> 40
 For division operations, if you use two
Fixnum
/Integer
objects, any fractional part of the result will be truncated. Ensure that at least one of the operands is aFloat
for division operations.
2.3.0 :004 > 7 / 4
=> 1 # Should be 1.75, but gets truncated!
2.3.0 :005 > 7.0 / 4
=> 1.75
2.3.0 :006 > 7 / 4.0
=> 1.75
 Variables can be used in any part of the math operation.
2.3.0 :007 > number = 2
=> 2
2.3.0 :008 > number + 3
=> 5
2.3.0 :009 > 4 * number
=> 8
 Using a variable in a math operation leaves the value in that variable unchanged
2.3.0 :010 > number = 2
=> 2
2.3.0 :011 > number + 3
=> 5
2.3.0 :012 > number
=> 2
 You can do a math operation on a variable, and then assign the result back to that variable
2.3.0 :013 > number = 2
=> 2
2.3.0 :014 > number = number + 1
=> 3
2.3.0 :015 > number
=> 3
2.3.0 :016 > number = number + 1
=> 4
2.3.0 :017 > number
=> 4
2.3.0 :018 > number = number  1
=> 3
2.3.0 :019 > number
=> 3
2.3.0 :020 > number = number * 2
=> 6
2.3.0 :021 > number
=> 6
2.3.0 :022 > number = number / 2.0
=> 3.0
2.3.0 :023 > number
=> 3.0
 Abbreviated assignment operators let you take the value in a variable and add to it, subtract from it, multiply it, or divide it, then reassign the result back to the same variable.
2.3.0 :001 > number = 2
=> 2
2.3.0 :002 > number += 1
=> 3
2.3.0 :003 > number
=> 3
2.3.0 :004 > number += 1
=> 4
2.3.0 :005 > number
=> 4
2.3.0 :006 > number = 1
=> 3
2.3.0 :007 > number
=> 3
2.3.0 :008 > number *= 2
=> 6
2.3.0 :009 > number
=> 6
2.3.0 :010 > number /= 2.0
=> 3.0
2.3.0 :011 > number
=> 3.0
If we're going to multiply the quantity of widgets by the price to get the total, 0:00 we're going to need to know how to do math operations. 0:04 Really, math operations like addition, subtraction, multiplication, and 0:07 division are central to almost any programming language. 0:11 So most languages make them easy to do. 0:14 Ruby is no exception. 0:17 [SOUND] Math operators take the values to their left and right and 0:19 perform a math operation on them. 0:22 The four most common operators, add, subtract, multiply, or divide values. 0:24 Let's go back to our workspace console and launch irb. 0:30 So we can try out of bunch of math operations and 0:33 immediately see the results. 0:35 The plus sign does the addition, so 2 + 3 gives us the result 5. 0:37 The minus sign does subtraction, so 124 gives us the result 8. 0:42 The asterisk does multiplication, so 5 * 8 gives us the result 40. 0:47 And the forward slash does division. 0:53 Let's try 7 / 4. 0:56 Now the result for 7 divided by 4 should be 1.75. 0:59 But because we used two fixnum values, the result gets truncated. 1:03 The reason Ruby does this is that if you're using fixnum values, 1:08 it's usually a signal that you're expecting to work with whole numbers. 1:12 And if there's any decimal places in a result, they should be thrown away. 1:16 The fix for 1:20 this is to replace at least one of those fixnum values with a float value. 1:21 And you may remember that we do that by including a decimal point in the value. 1:25 So we can still use a whole number, but we'll just say 7.0 / 4. 1:29 We can still use a fixnum for one of the values, as long as one of them is a float. 1:34 And now we get the proper result of 1.75. 1:39 We can use a float for the second value instead, if we prefer. 1:43 So 7 / 4.0, that also gives us 1.75. 1:46 Variables can be used in place of hardcoded numbers for 1:51 any part of the math operation. 1:54 So lets assign to a variable named number, let's assign the value 2 to that. 1:56 And then let's try using this number in math operations. 2:03 So we'll say, number + 3, we get the result 5, 2:05 because number contains 2, that + 3 is 5. 2:09 Let's try 4 * number, and we get the result 8, cuz 4 * 2 is 8. 2:13 Using a variable in a math operation leaves the value in that variable 2:20 unchanged, though. 2:23 If we take a look at the current value of number, we can see it's still 2, 2:24 despite the math operations we did on it, previously. 2:28 If you need to change the value that a variable holds, you can do 2:32 a math operation on the variable and then assign the result back to that variable. 2:35 So we could say, number = number + 1, 2:40 which gives us the result 3. 2:46 And then if we print out the value that number holds, 2:50 we can see that the number variable has been updated as well. 2:53 We can do the same thing again, number = number + 1. 2:57 And if we print out the value number holds, 3:01 we'll see that it's increased to 4 again. 3:03 If we say number = number 1, 3:05 that'll subtract 1 from the value that number holds. 3:08 We can double the value in number by saying number = number * 2. 3:12 And you can see that the result's been written back to the number variable. 3:18 And we can divide it in half by saying number = number / 2.0. 3:22 Remember, you should always use a float as part of your division operations in Ruby. 3:28 By the way, all the math operations we've shown you so 3:33 far work just like this in just about every programming language out there. 3:36 So you'll be able to apply what you've seen in almost any programming 3:40 language you want. 3:43 The next Ruby feature we're going to show you is something that not every 3:45 language has. 3:48 Abbreviated assignment operators let you take the value in a variable and 3:49 add to it, subtract from it, multiply it, or divide it. 3:53 Then reassign the result back to the same variable. 3:57 So we can say, number += 1, that's an abbreviated 4:00 assignment operator right there that adds one to whatever value is in number. 4:05 And we can see that we get the result 4, and 4:10 if we take a look at the value that number holds, we can see that's been updated. 4:12 If we call that again, we can increment the value in number again. 4:17 We can use a different abbreviated assignment operator, number =1, 4:22 to subtract 1 from the value that number holds. 4:26 And again, the value is permanently updated. 4:31 We can double it again by saying number *=2, and it's permanently updated to 8. 4:34 And we can permanently divide it in half by saying 4:42 number /= 2.0, and the value is halved. 4:46
You need to sign up for Treehouse in order to download course files.
Sign up