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
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

0:00
If we're going to multiply the quantity of widgets by the price to get the total,

0:04
we're going to need to know how to do math operations.

0:07
Really, math operations like addition, subtraction, multiplication, and

0:11
division are central to almost any programming language.

0:14
So most languages make them easy to do.

0:17
Ruby is no exception.

0:19
[SOUND] Math operators take the values to their left and right and

0:22
perform a math operation on them.

0:24
The four most common operators, add, subtract, multiply, or divide values.

0:30
Let's go back to our workspace console and launch irb.

0:33
So we can try out of bunch of math operations and

0:35
immediately see the results.

0:37
The plus sign does the addition, so 2 + 3 gives us the result 5.

0:42
The minus sign does subtraction, so 124 gives us the result 8.

0:47
The asterisk does multiplication, so 5 * 8 gives us the result 40.

0:53
And the forward slash does division.

0:56
Let's try 7 / 4.

0:59
Now the result for 7 divided by 4 should be 1.75.

1:03
But because we used two fixnum values, the result gets truncated.

1:08
The reason Ruby does this is that if you're using fixnum values,

1:12
it's usually a signal that you're expecting to work with whole numbers.

1:16
And if there's any decimal places in a result, they should be thrown away.

1:20
The fix for

1:21
this is to replace at least one of those fixnum values with a float value.

1:25
And you may remember that we do that by including a decimal point in the value.

1:29
So we can still use a whole number, but we'll just say 7.0 / 4.

1:34
We can still use a fixnum for one of the values, as long as one of them is a float.

1:39
And now we get the proper result of 1.75.

1:43
We can use a float for the second value instead, if we prefer.

1:46
So 7 / 4.0, that also gives us 1.75.

1:51
Variables can be used in place of hardcoded numbers for

1:54
any part of the math operation.

1:56
So lets assign to a variable named number, let's assign the value 2 to that.

2:03
And then let's try using this number in math operations.

2:05
So we'll say, number + 3, we get the result 5,

2:09
because number contains 2, that + 3 is 5.

2:13
Let's try 4 * number, and we get the result 8, cuz 4 * 2 is 8.

2:20
Using a variable in a math operation leaves the value in that variable

2:23
unchanged, though.

2:24
If we take a look at the current value of number, we can see it's still 2,

2:28
despite the math operations we did on it, previously.

2:32
If you need to change the value that a variable holds, you can do

2:35
a math operation on the variable and then assign the result back to that variable.

2:40
So we could say, number = number + 1,

2:46
which gives us the result 3.

2:50
And then if we print out the value that number holds,

2:53
we can see that the number variable has been updated as well.

2:57
We can do the same thing again, number = number + 1.

3:01
And if we print out the value number holds,

3:03
we'll see that it's increased to 4 again.

3:05
If we say number = number 1,

3:08
that'll subtract 1 from the value that number holds.

3:12
We can double the value in number by saying number = number * 2.

3:18
And you can see that the result's been written back to the number variable.

3:22
And we can divide it in half by saying number = number / 2.0.

3:28
Remember, you should always use a float as part of your division operations in Ruby.

3:33
By the way, all the math operations we've shown you so

3:36
far work just like this in just about every programming language out there.

3:40
So you'll be able to apply what you've seen in almost any programming

3:43
language you want.

3:45
The next Ruby feature we're going to show you is something that not every

3:48
language has.

3:49
Abbreviated assignment operators let you take the value in a variable and

3:53
add to it, subtract from it, multiply it, or divide it.

3:57
Then reassign the result back to the same variable.

4:00
So we can say, number += 1, that's an abbreviated

4:05
assignment operator right there that adds one to whatever value is in number.

4:10
And we can see that we get the result 4, and

4:12
if we take a look at the value that number holds, we can see that's been updated.

4:17
If we call that again, we can increment the value in number again.

4:22
We can use a different abbreviated assignment operator, number =1,

4:26
to subtract 1 from the value that number holds.

4:31
And again, the value is permanently updated.

4:34
We can double it again by saying number *=2, and it's permanently updated to 8.

4:42
And we can permanently divide it in half by saying

4:46
number /= 2.0, and the value is halved.
You need to sign up for Treehouse in order to download course files.
Sign up