1
00:00:00,260 --> 00:00:04,450
If we're going to multiply the quantity
of widgets by the price to get the total,
2
00:00:04,450 --> 00:00:07,600
we're going to need to know
how to do math operations.
3
00:00:07,600 --> 00:00:11,560
Really, math operations like addition,
subtraction, multiplication, and
4
00:00:11,560 --> 00:00:14,910
division are central to almost
any programming language.
5
00:00:14,910 --> 00:00:17,390
So most languages make them easy to do.
6
00:00:17,390 --> 00:00:19,182
Ruby is no exception.
7
00:00:19,182 --> 00:00:22,773
[SOUND] Math operators take the values
to their left and right and
8
00:00:22,773 --> 00:00:24,747
perform a math operation on them.
9
00:00:24,747 --> 00:00:30,230
The four most common operators, add,
subtract, multiply, or divide values.
10
00:00:30,230 --> 00:00:33,110
Let's go back to our workspace console and
launch irb.
11
00:00:33,110 --> 00:00:35,630
So we can try out of bunch
of math operations and
12
00:00:35,630 --> 00:00:37,770
immediately see the results.
13
00:00:37,770 --> 00:00:42,500
The plus sign does the addition,
so 2 + 3 gives us the result 5.
14
00:00:42,500 --> 00:00:47,548
The minus sign does subtraction,
so 12-4 gives us the result 8.
15
00:00:47,548 --> 00:00:53,889
The asterisk does multiplication,
so 5 * 8 gives us the result 40.
16
00:00:53,889 --> 00:00:56,320
And the forward slash does division.
17
00:00:56,320 --> 00:00:59,690
Let's try 7 / 4.
18
00:00:59,690 --> 00:01:03,770
Now the result for
7 divided by 4 should be 1.75.
19
00:01:03,770 --> 00:01:08,760
But because we used two fixnum values,
the result gets truncated.
20
00:01:08,760 --> 00:01:12,170
The reason Ruby does this is that
if you're using fixnum values,
21
00:01:12,170 --> 00:01:16,088
it's usually a signal that you're
expecting to work with whole numbers.
22
00:01:16,088 --> 00:01:20,650
And if there's any decimal places in
a result, they should be thrown away.
23
00:01:20,650 --> 00:01:21,220
The fix for
24
00:01:21,220 --> 00:01:25,590
this is to replace at least one of
those fixnum values with a float value.
25
00:01:25,590 --> 00:01:29,880
And you may remember that we do that by
including a decimal point in the value.
26
00:01:29,880 --> 00:01:34,870
So we can still use a whole number,
but we'll just say 7.0 / 4.
27
00:01:34,870 --> 00:01:39,590
We can still use a fixnum for one of the
values, as long as one of them is a float.
28
00:01:39,590 --> 00:01:43,252
And now we get the proper result of 1.75.
29
00:01:43,252 --> 00:01:46,250
We can use a float for
the second value instead, if we prefer.
30
00:01:46,250 --> 00:01:51,740
So 7 / 4.0, that also gives us 1.75.
31
00:01:51,740 --> 00:01:54,820
Variables can be used in place
of hard-coded numbers for
32
00:01:54,820 --> 00:01:56,750
any part of the math operation.
33
00:01:56,750 --> 00:02:03,029
So lets assign to a variable named number,
let's assign the value 2 to that.
34
00:02:03,029 --> 00:02:05,577
And then let's try using this
number in math operations.
35
00:02:05,577 --> 00:02:09,682
So we'll say, number + 3,
we get the result 5,
36
00:02:09,682 --> 00:02:13,418
because number contains 2, that + 3 is 5.
37
00:02:13,418 --> 00:02:20,300
Let's try 4 * number, and
we get the result 8, cuz 4 * 2 is 8.
38
00:02:20,300 --> 00:02:23,870
Using a variable in a math operation
leaves the value in that variable
39
00:02:23,870 --> 00:02:24,890
unchanged, though.
40
00:02:24,890 --> 00:02:28,820
If we take a look at the current value
of number, we can see it's still 2,
41
00:02:28,820 --> 00:02:32,490
despite the math operations we did on it,
previously.
42
00:02:32,490 --> 00:02:35,740
If you need to change the value
that a variable holds, you can do
43
00:02:35,740 --> 00:02:40,720
a math operation on the variable and then
assign the result back to that variable.
44
00:02:40,720 --> 00:02:46,483
So we could say, number = number + 1,
45
00:02:46,483 --> 00:02:50,727
which gives us the result 3.
46
00:02:50,727 --> 00:02:53,763
And then if we print out
the value that number holds,
47
00:02:53,763 --> 00:02:57,930
we can see that the number
variable has been updated as well.
48
00:02:57,930 --> 00:03:01,140
We can do the same thing again,
number = number + 1.
49
00:03:01,140 --> 00:03:03,300
And if we print out
the value number holds,
50
00:03:03,300 --> 00:03:05,800
we'll see that it's increased to 4 again.
51
00:03:05,800 --> 00:03:08,149
If we say number = number- 1,
52
00:03:08,149 --> 00:03:12,830
that'll subtract 1 from
the value that number holds.
53
00:03:12,830 --> 00:03:16,910
We can double the value in number
by saying number = number * 2.
54
00:03:18,850 --> 00:03:22,470
And you can see that the result's been
written back to the number variable.
55
00:03:22,470 --> 00:03:28,090
And we can divide it in half by
saying number = number / 2.0.
56
00:03:28,090 --> 00:03:32,150
Remember, you should always use a float as
part of your division operations in Ruby.
57
00:03:33,500 --> 00:03:36,130
By the way,
all the math operations we've shown you so
58
00:03:36,130 --> 00:03:40,790
far work just like this in just about
every programming language out there.
59
00:03:40,790 --> 00:03:43,970
So you'll be able to apply what
you've seen in almost any programming
60
00:03:43,970 --> 00:03:45,490
language you want.
61
00:03:45,490 --> 00:03:48,400
The next Ruby feature we're going to
show you is something that not every
62
00:03:48,400 --> 00:03:49,580
language has.
63
00:03:49,580 --> 00:03:53,840
Abbreviated assignment operators let
you take the value in a variable and
64
00:03:53,840 --> 00:03:57,740
add to it, subtract from it,
multiply it, or divide it.
65
00:03:57,740 --> 00:04:00,780
Then reassign the result
back to the same variable.
66
00:04:00,780 --> 00:04:05,550
So we can say, number += 1,
that's an abbreviated
67
00:04:05,550 --> 00:04:10,310
assignment operator right there that
adds one to whatever value is in number.
68
00:04:10,310 --> 00:04:12,380
And we can see that we get the result 4,
and
69
00:04:12,380 --> 00:04:17,190
if we take a look at the value that number
holds, we can see that's been updated.
70
00:04:17,190 --> 00:04:21,320
If we call that again, we can
increment the value in number again.
71
00:04:22,570 --> 00:04:26,070
We can use a different abbreviated
assignment operator, number -=1,
72
00:04:26,070 --> 00:04:29,620
to subtract 1 from the value
that number holds.
73
00:04:31,590 --> 00:04:34,280
And again,
the value is permanently updated.
74
00:04:34,280 --> 00:04:42,060
We can double it again by saying number
*=2, and it's permanently updated to 8.
75
00:04:42,060 --> 00:04:46,328
And we can permanently
divide it in half by saying
76
00:04:46,328 --> 00:04:50,399
number /= 2.0, and the value is halved.