Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,000 --> 00:00:07,960
So before we go any further, let's take a quick look at a few gotchas when it comes to blocks
2
00:00:07,960 --> 00:00:09,560
and they're surrounding variables.
3
00:00:09,560 --> 00:00:11,400
I just love gotchas.
4
00:00:11,400 --> 00:00:12,960
Here's the first one.
5
00:00:12,960 --> 00:00:16,320
We've seen this block with a number block parameter.
6
00:00:16,320 --> 00:00:18,520
It prints the number three times.
7
00:00:18,520 --> 00:00:21,280
Sure, if we run this, we get three lines of howdy.
8
00:00:21,280 --> 00:00:27,000
Now, what if we also wanted to print a name like we want to say howdy mo?
9
00:00:27,000 --> 00:00:30,560
We could define a name variable inside the block and use it.
10
00:00:30,560 --> 00:00:32,439
Sure, I'll just put it right up here.
11
00:00:32,439 --> 00:00:34,560
Name equals mo.
12
00:00:34,560 --> 00:00:39,720
And then we'll just say howdy mo.
13
00:00:39,720 --> 00:00:41,120
Just like that.
14
00:00:41,120 --> 00:00:44,800
And of course, if we run it, now we've got mo's name in the output.
15
00:00:44,800 --> 00:00:48,920
Now you might think you could access the variable after the block runs.
16
00:00:48,920 --> 00:00:52,560
Yeah, let's just down here, we'll just do a puts of name.
17
00:00:52,560 --> 00:00:55,240
That's the name of the variable.
18
00:00:55,240 --> 00:00:57,120
If we run this, we get an error.
19
00:00:57,120 --> 00:01:01,320
It says undefined local variable or method name.
20
00:01:01,320 --> 00:01:02,720
Well, why is that?
21
00:01:02,720 --> 00:01:07,960
Well, variables that show up inside of the block only here are local to the block.
22
00:01:07,960 --> 00:01:09,680
They're like local variables.
23
00:01:09,680 --> 00:01:15,080
They're only in scope while the block is running or within this doing end.
24
00:01:15,080 --> 00:01:20,520
So when we try to reference name outside of that block, it's not defined because once
25
00:01:20,520 --> 00:01:24,480
the block is run, this name variable has evaporated.
26
00:01:24,480 --> 00:01:29,680
So then what if the name was already a variable defined before the block?
27
00:01:29,680 --> 00:01:30,960
Well, let's give that a try.
28
00:01:30,960 --> 00:01:36,080
Just up here before the block, we'll set the name to something like curly.
29
00:01:36,080 --> 00:01:39,840
Now if we run it, notice it prints out mo at the bottom.
30
00:01:39,840 --> 00:01:42,720
That's that puts line here that's printing the name.
31
00:01:42,720 --> 00:01:44,440
So we don't get an undefined variable.
32
00:01:44,440 --> 00:01:45,720
It knows what name is.
33
00:01:45,720 --> 00:01:49,120
And in fact, the value in name is mo.
34
00:01:49,120 --> 00:01:51,480
Well, we set name to curly up here.
35
00:01:51,480 --> 00:01:55,480
But then when the block ran, it set the name variable to mo.
36
00:01:55,480 --> 00:01:58,960
And that's why we're seeing mo printed out outside of the block.
37
00:01:58,960 --> 00:02:03,400
So anytime you have a variable defined before the block and it has the same name.
38
00:02:03,400 --> 00:02:06,280
It's the same name variable inside of the block.
39
00:02:06,280 --> 00:02:08,320
They're the same variable.
40
00:02:08,320 --> 00:02:12,560
So if you change it inside of the block, well, it's changed when we reference it outside
41
00:02:12,560 --> 00:02:14,200
of the block as well.
42
00:02:14,200 --> 00:02:18,600
In other words, the block can change the value of local variables and it's surrounding scope.
43
00:02:18,600 --> 00:02:21,040
But there are two exceptions to this.
44
00:02:21,040 --> 00:02:23,679
We have a number block parameter.
45
00:02:23,679 --> 00:02:28,040
What happens if we have a number variable defined before the block?
46
00:02:28,040 --> 00:02:29,040
Yeah, let's just do that here.
47
00:02:29,040 --> 00:02:33,680
We'll have number equal to say 100.
48
00:02:33,680 --> 00:02:35,920
So we've got a variable here called number.
49
00:02:35,920 --> 00:02:38,480
And we've got a block parameter called number.
50
00:02:38,480 --> 00:02:41,000
And then we're printing out that number inside of the block.
51
00:02:41,000 --> 00:02:45,560
So in the same way that name was shared, you might expect a number to be shared as well.
52
00:02:45,560 --> 00:02:47,320
But let's go ahead and print that out down here.
53
00:02:47,320 --> 00:02:48,320
Puts number.
54
00:02:48,320 --> 00:02:54,840
If we run this, notice that the number that gets printed is 100 right here.
55
00:02:54,840 --> 00:02:57,440
Well, we set number to 100 at the top.
56
00:02:57,440 --> 00:03:02,120
But then remember when the block runs, this time's method is going to assign values to the
57
00:03:02,120 --> 00:03:03,120
number parameter.
58
00:03:03,120 --> 00:03:07,760
It's going to be 1, 2, and 3, which we saw printed inside of the block.
59
00:03:07,760 --> 00:03:11,880
Actually it's 0, 1, and 2 in this case because it's the time's method.
60
00:03:11,880 --> 00:03:15,920
But then when we print number out at the end, it's back to 100 again.
61
00:03:15,920 --> 00:03:20,600
So even though it seems like this would get overwritten right here, the number variable
62
00:03:20,600 --> 00:03:23,600
is preserved outside of the block.
63
00:03:23,600 --> 00:03:28,799
So the takeaway here is block parameters, or the names of block parameters here, aren't
64
00:03:28,799 --> 00:03:32,839
shared with variables of the same name in the surrounding scope.
65
00:03:32,839 --> 00:03:38,239
The number variable here and the number block parameter here are different variables.
66
00:03:38,239 --> 00:03:40,040
This is actually great news.
67
00:03:40,040 --> 00:03:44,880
This means that we can use any name we want for a block parameter without worrying that
68
00:03:44,880 --> 00:03:50,160
it will clobber or overwrite a variable of the same name in this surrounding scope.
69
00:03:50,160 --> 00:03:53,760
Yeah, you can imagine all sorts of variables being defined before this block.
70
00:03:53,760 --> 00:03:56,200
And number might be a very common one to use.
71
00:03:56,200 --> 00:03:59,840
So even though you called this block parameter number here, you don't have to worry about
72
00:03:59,840 --> 00:04:02,640
it clobbering any of those variables.
73
00:04:02,640 --> 00:04:04,359
So let's look at one last example.
74
00:04:04,359 --> 00:04:09,680
Suppose we're doing some calculations and we want to store it in a temp variable before
75
00:04:09,680 --> 00:04:10,680
the block.
76
00:04:10,680 --> 00:04:13,079
Like I don't know, pi divided by 4.
77
00:04:13,079 --> 00:04:14,840
Yeah, that sounds like a fun calculation.
78
00:04:14,840 --> 00:04:19,959
Then inside the block, we have a temp variable for storing the actual temperature.
79
00:04:19,959 --> 00:04:23,520
Temperature in this case, let's say 98.6.
80
00:04:23,520 --> 00:04:26,960
And then we can print out what the temperature is.
81
00:04:26,960 --> 00:04:30,160
So this would be like how do you know is 98.6 degrees.
82
00:04:30,160 --> 00:04:31,160
Right.
83
00:04:31,160 --> 00:04:35,240
Now what happens if we reference temp outside the block?
84
00:04:35,240 --> 00:04:36,520
So we print temp there.
85
00:04:36,520 --> 00:04:37,520
Let's run it.
86
00:04:37,520 --> 00:04:41,000
Well, temp is 98.6.
87
00:04:41,000 --> 00:04:45,880
So we know it was 98.6 inside of the block because we see the print outs here.
88
00:04:45,880 --> 00:04:49,440
Howdymo is 98.6.
89
00:04:49,440 --> 00:04:54,840
Up here it was set to a value other than 98.6.
90
00:04:54,840 --> 00:04:56,880
So we know that these are shared variables.
91
00:04:56,880 --> 00:04:59,480
It's got the same name here as it does here.
92
00:04:59,480 --> 00:05:01,320
And this isn't a block parameter.
93
00:05:01,320 --> 00:05:02,880
So it's the same variable.
94
00:05:02,880 --> 00:05:06,880
So we've overwritten the temp variable inside of our block.
95
00:05:06,880 --> 00:05:09,200
Now sometimes you don't want this to happen.
96
00:05:09,200 --> 00:05:13,360
Say for example, we want to keep this temp variable, but also keep this local variable
97
00:05:13,360 --> 00:05:16,960
inside of the block, but not have them be the same thing.
98
00:05:16,960 --> 00:05:19,680
And there's a really easy way to set that up.
99
00:05:19,680 --> 00:05:23,280
Inside of our block parameter list, we can use a semicolon.
100
00:05:23,280 --> 00:05:28,800
And then we just give it the name of any parameter or variable that we want to protect.
101
00:05:28,800 --> 00:05:30,400
In this case, we're going to say temp.
102
00:05:30,400 --> 00:05:34,039
And by doing this, we're telling Ruby, hey, I know there's a temp variable out here, or
103
00:05:34,039 --> 00:05:36,240
there's a variable called temp out here.
104
00:05:36,240 --> 00:05:39,640
And I'm going to have a variable called temp inside of my block as well.
105
00:05:39,640 --> 00:05:42,600
But I want this temp to be a block local variable.
106
00:05:42,600 --> 00:05:45,840
In other words, I want them to be different variables.
107
00:05:45,840 --> 00:05:49,000
I don't want this one to overwrite the one in this rounding scope.
108
00:05:49,000 --> 00:05:53,760
So now if I save that and print it out, you notice that temp is printed here.
109
00:05:53,760 --> 00:06:00,880
Is the value of that math pi divided by four rather than 98.6, which overwrote the variable
110
00:06:00,880 --> 00:06:01,880
there.
111
00:06:01,880 --> 00:06:03,760
So again, you just use a semicolon.
112
00:06:03,760 --> 00:06:08,520
And then you can list any parameters or any variable names, I should say, after that,
113
00:06:08,520 --> 00:06:11,599
that should be protected inside of the block.
114
00:06:11,599 --> 00:06:16,080
So the takeaway here is the variables listed after the semicolon aren't considered block
115
00:06:16,080 --> 00:06:17,080
parameters.
116
00:06:17,080 --> 00:06:20,320
They don't get assigned values when the block is executed.
117
00:06:20,320 --> 00:06:25,280
Think of them as reserved variable names for use inside the block.
118
00:06:25,280 --> 00:06:27,120
So here's a quick recap.
119
00:06:27,120 --> 00:06:31,320
Variables that appear only inside the block are local to the block.
120
00:06:31,320 --> 00:06:34,840
They are not accessible outside the block.
121
00:06:34,840 --> 00:06:38,760
Variables defined outside the block can be shared inside the block.
122
00:06:38,760 --> 00:06:43,680
The block can change the value of local variables in its surrounding scope.
123
00:06:43,680 --> 00:06:48,440
Block parameters are always local to the block, even if they have the same name as the local
124
00:06:48,440 --> 00:06:51,440
variable in the surrounding scope.
125
00:06:51,440 --> 00:06:56,840
Lastly, you can declare block local variables by putting them after a semicolon in the
126
00:06:56,840 --> 00:06:58,880
blocks parameter list.
127
00:06:58,880 --> 00:07:04,560
This way, they're variable outside the block with the same name is protected from change.
128
00:07:04,560 --> 00:07:06,200
Now don't let all this throw you.
129
00:07:06,200 --> 00:07:10,640
It's just good to keep in mind how blocks interact with their surrounding scope.
130
00:07:10,640 --> 00:07:14,600
And often when I get some weird behavior with the block, it's because I've got some variable
131
00:07:14,600 --> 00:07:16,159
mixed up somewhere.
132
00:07:16,159 --> 00:07:18,800
So feel free to experiment with these rules on your own.
133
00:07:18,800 --> 00:07:33,520
It's a great way to practice and learn.
11784
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.