Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Math Operations4:52 with Jay McGavren
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.
+ - * /
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
Integerobjects, any fractional part of the result will be truncated. Ensure that at least one of the operands is a
Floatfor 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,
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.
[SOUND] 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 go back to our workspace console and launch irb.
So we can try out of bunch of math operations and
immediately see the results.
The plus sign does the addition, so 2 + 3 gives us the result 5.
The minus sign does subtraction, so 12-4 gives us the result 8.
The asterisk does multiplication, so 5 * 8 gives us the result 40.
And the forward slash does division.
Let's try 7 / 4.
Now the result for 7 divided by 4 should be 1.75.
But because we used two fixnum values, the result gets truncated.
The reason Ruby does this is that if you're using fixnum values,
it's usually a signal that you're expecting to work with whole numbers.
And if there's any decimal places in a result, they should be thrown away.
The fix for
this is to replace at least one of those fixnum values with a float value.
And you may remember that we do that by including a decimal point in the value.
So we can still use a whole number, but we'll just say 7.0 / 4.
We can still use a fixnum for one of the values, as long as one of them is a float.
And now we get the proper result of 1.75.
We can use a float for the second value instead, if we prefer.
So 7 / 4.0, that also gives us 1.75.
Variables can be used in place of hard-coded numbers for
any part of the math operation.
So lets assign to a variable named number, let's assign the value 2 to that.
And then let's try using this number in math operations.
So we'll say, number + 3, we get the result 5,
because number contains 2, that + 3 is 5.
Let's try 4 * number, and we get the result 8, cuz 4 * 2 is 8.
Using a variable in a math operation leaves the value in that variable
If we take a look at the current value of number, we can see it's still 2,
despite the math operations we did on it, previously.
If you need to change the value that a variable holds, you can do
a math operation on the variable and then assign the result back to that variable.
So we could say, number = number + 1,
which gives us the result 3.
And then if we print out the value that number holds,
we can see that the number variable has been updated as well.
We can do the same thing again, number = number + 1.
And if we print out the value number holds,
we'll see that it's increased to 4 again.
If we say number = number- 1,
that'll subtract 1 from the value that number holds.
We can double the value in number by saying number = number * 2.
And you can see that the result's been written back to the number variable.
And we can divide it in half by saying number = number / 2.0.
Remember, you should always use a float as part of your division operations in Ruby.
By the way, all the math operations we've shown you so
far work just like this in just about every programming language out there.
So you'll be able to apply what you've seen in almost any programming
language you want.
The next Ruby feature we're going to show you is something that not every
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.
So we can say, number += 1, that's an abbreviated
assignment operator right there that adds one to whatever value is in number.
And we can see that we get the result 4, and
if we take a look at the value that number holds, we can see that's been updated.
If we call that again, we can increment the value in number again.
We can use a different abbreviated assignment operator, number -=1,
to subtract 1 from the value that number holds.
And again, the value is permanently updated.
We can double it again by saying number *=2, and it's permanently updated to 8.
And we can permanently divide it in half by saying
number /= 2.0, and the value is halved.
You need to sign up for Treehouse in order to download course files.Sign up