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,480
So now that we have a good foundation in place for the remainder of the course, we're going
2
00:00:07,480 --> 00:00:13,080
to examine a number of use cases where blocks can be used to solve problems elegantly.
3
00:00:13,080 --> 00:00:17,640
I'm going to look at some very zig-a-files and you'll see some reoccurring patterns that
4
00:00:17,640 --> 00:00:21,120
you can then hopefully apply to your own code.
5
00:00:21,120 --> 00:00:23,640
So here's our first use case.
6
00:00:23,640 --> 00:00:28,240
Programmers often need to measure how much time some arbitrary code takes to run.
7
00:00:28,240 --> 00:00:32,119
I can't tell you how many times I've written code just like this.
8
00:00:32,119 --> 00:00:36,519
You start by getting the current time and you set it to some variable like start time.
9
00:00:36,519 --> 00:00:38,680
Then you run some arbitrary amount of code.
10
00:00:38,680 --> 00:00:42,680
In this case, we've just simulated some code by sleeping for half a second.
11
00:00:42,680 --> 00:00:46,120
And then you calculate the elapsed time, which is the current time minus whatever the
12
00:00:46,120 --> 00:00:50,920
start time was, and then you print out, hey, this chunk of code took so many seconds.
13
00:00:50,920 --> 00:00:56,760
So you're basically wrapping or bracketing this code here with some boilerplate code
14
00:00:56,760 --> 00:01:00,480
getting the start time and calculating and printing the elapsed time.
15
00:01:00,480 --> 00:01:05,800
And you end up with lots of duplicated or boilerplate code around any code that you want
16
00:01:05,800 --> 00:01:07,120
to measure.
17
00:01:07,120 --> 00:01:09,400
So how can we clean up the duplication here?
18
00:01:09,400 --> 00:01:15,520
Well one way is to encapsulate it in a method that runs some chunk of code in the middle.
19
00:01:15,520 --> 00:01:18,560
Here's the chunk of code we want to run in the middle, and here's the code we want
20
00:01:18,560 --> 00:01:19,840
to bracket it with.
21
00:01:19,840 --> 00:01:21,960
So let's write a method to do that.
22
00:01:21,960 --> 00:01:23,200
Which is to find it up top here.
23
00:01:23,200 --> 00:01:25,880
I'm going to call the method time it.
24
00:01:25,880 --> 00:01:26,880
Which of the method do?
25
00:01:26,880 --> 00:01:31,600
Well, first it needs to do this step, calculate the start time.
26
00:01:31,600 --> 00:01:36,080
And then at the end it needs to do this step here, calculate the elapsed time and print
27
00:01:36,080 --> 00:01:37,080
it out.
28
00:01:37,080 --> 00:01:40,960
And then right in the middle here we needed to run our arbitrary code.
29
00:01:40,960 --> 00:01:42,120
We know how to do that.
30
00:01:42,120 --> 00:01:44,600
We just call yield.
31
00:01:44,600 --> 00:01:50,120
Now down here to run this chunk of code, we call time it, that's the new of our method.
32
00:01:50,120 --> 00:01:54,320
And we put the code inside of block, I'll use do end here.
33
00:01:54,320 --> 00:01:59,679
So we're basically bracketing the code that varies, which is this code, with the code
34
00:01:59,679 --> 00:02:01,000
that doesn't vary at all.
35
00:02:01,000 --> 00:02:02,440
It never changes.
36
00:02:02,440 --> 00:02:04,880
So we can use this with any block we want.
37
00:02:04,880 --> 00:02:09,840
If we print this out now, it says it took 0.5 seconds.
38
00:02:09,840 --> 00:02:13,040
And of course we can use this method with any block of code.
39
00:02:13,040 --> 00:02:16,959
So we could call time it anywhere else in our program or our application.
40
00:02:16,959 --> 00:02:21,720
And we know that it's going to be bracketed with the appropriate code here.
41
00:02:21,720 --> 00:02:25,120
Now it's often handy to have some sort of label or description about the thing that you're
42
00:02:25,120 --> 00:02:26,120
timing.
43
00:02:26,120 --> 00:02:31,400
So you might want to pass in a parameter here like, oh, sleepy code, for example.
44
00:02:31,400 --> 00:02:33,400
Well that's just a method parameter.
45
00:02:33,400 --> 00:02:35,480
We capture it in a method parameter appear.
46
00:02:35,480 --> 00:02:36,920
We'll just call it label.
47
00:02:36,920 --> 00:02:41,520
And then we'll just print out whatever the label is to a county seconds.
48
00:02:41,520 --> 00:02:42,520
We run that.
49
00:02:42,520 --> 00:02:45,160
It says sleepy code took half a second.
50
00:02:45,160 --> 00:02:49,800
This technique of running the code in the middle of some other code is a very common
51
00:02:49,800 --> 00:02:50,800
pattern.
52
00:02:50,800 --> 00:02:58,040
We're referred to as execute around because we're executing around the code in the block.
53
00:02:58,040 --> 00:03:02,680
Next up, let's suppose we have this sensor class that has methods for returning the water
54
00:03:02,680 --> 00:03:04,640
temperature and level.
55
00:03:04,640 --> 00:03:08,280
We just made them random numbers just to make it more interesting.
56
00:03:08,280 --> 00:03:12,960
And then let's say we are checking the sensor regularly and we need to generate an audit
57
00:03:12,960 --> 00:03:13,960
trail.
58
00:03:13,960 --> 00:03:15,600
Yes, we have this code down here.
59
00:03:15,600 --> 00:03:17,080
We're checking the water temperature.
60
00:03:17,080 --> 00:03:18,080
What do we do?
61
00:03:18,080 --> 00:03:21,920
Well we ask the sensor for its temperature if it's less than 150.
62
00:03:21,920 --> 00:03:23,040
We run that expression.
63
00:03:23,040 --> 00:03:25,760
We assign the result to a variable called result.
64
00:03:25,760 --> 00:03:27,680
If the result is true, then we print okay.
65
00:03:27,680 --> 00:03:29,080
The water temperature is fine.
66
00:03:29,080 --> 00:03:31,720
Otherwise, we print out failed.
67
00:03:31,720 --> 00:03:34,520
We also do something very similar with the water level.
68
00:03:34,520 --> 00:03:36,360
Yeah, we just ask the sensor for its level.
69
00:03:36,360 --> 00:03:37,920
If it's greater than three.
70
00:03:37,920 --> 00:03:39,640
So we assign that to the result.
71
00:03:39,640 --> 00:03:41,880
If it is greater than three, well then we print okay.
72
00:03:41,880 --> 00:03:43,680
That's a valid water level.
73
00:03:43,680 --> 00:03:45,760
Otherwise, we print fail.
74
00:03:45,760 --> 00:03:47,960
Now when we run it, depending on the random numbers we get,
75
00:03:47,960 --> 00:03:49,400
we'll get different output.
76
00:03:49,400 --> 00:03:53,000
So look like the water temperature was okay, but the water level failed.
77
00:03:53,000 --> 00:03:54,000
We run it again.
78
00:03:54,000 --> 00:03:57,040
We'll get different output depending on the random numbers.
79
00:03:57,040 --> 00:04:01,000
So what works, but the problem is we have a lot of code duplication here.
80
00:04:01,000 --> 00:04:05,320
The real meat of it is just calling sensor temperature here to check the temperature and
81
00:04:05,320 --> 00:04:07,840
calling sensor level here to check the level.
82
00:04:07,840 --> 00:04:11,360
And everything around these bits of code, well they're just checking code.
83
00:04:11,360 --> 00:04:15,280
And they don't change depending on what we're checking with the sensor.
84
00:04:15,280 --> 00:04:20,200
And we need to separate the boilerplate checking code from what is being checked.
85
00:04:20,200 --> 00:04:23,800
So let's write a method that encapsulates that checking stuff for us.
86
00:04:23,800 --> 00:04:29,440
I'm going to do it right up here, call the method with checking.
87
00:04:29,440 --> 00:04:31,600
And we're going to have this take a description.
88
00:04:31,600 --> 00:04:33,160
And then what's the method going to do?
89
00:04:33,160 --> 00:04:35,400
Well the first part is printing this out.
90
00:04:35,400 --> 00:04:37,320
So I'm just going to take this out of here.
91
00:04:37,320 --> 00:04:38,520
And we're going to say checking.
92
00:04:38,520 --> 00:04:41,920
And then we're going to interpolate in the description here.
93
00:04:41,920 --> 00:04:42,920
All right.
94
00:04:42,920 --> 00:04:45,080
We also need to do this if checking part.
95
00:04:45,080 --> 00:04:46,240
That's boilerplate code.
96
00:04:46,240 --> 00:04:47,840
So let's take that out of there.
97
00:04:47,840 --> 00:04:48,680
All right.
98
00:04:48,680 --> 00:04:53,440
Now in the middle of these two, we need to run the actual checking code.
99
00:04:53,440 --> 00:04:55,720
To do that, we're going to use a yield.
100
00:04:55,720 --> 00:04:57,320
That's going to yield to the block.
101
00:04:57,320 --> 00:04:59,720
But we need to make sure to capture the result.
102
00:04:59,720 --> 00:05:00,920
What the block returns to us.
103
00:05:00,920 --> 00:05:05,680
So I'm going to sign it to a variable here called result, which then we check right here.
104
00:05:05,680 --> 00:05:09,600
So now down here, we can remove this part.
105
00:05:09,600 --> 00:05:13,000
And we can just call with checking.
106
00:05:13,000 --> 00:05:14,560
We're checking the temperature.
107
00:05:14,560 --> 00:05:17,040
Passing the eastering we want there.
108
00:05:17,040 --> 00:05:19,360
Then give it the block.
109
00:05:19,360 --> 00:05:23,920
The block simply makes the comparison on temperature in the value of 150.
110
00:05:23,920 --> 00:05:28,620
And remember that the value of the last expression of the value in the block is passed
111
00:05:28,620 --> 00:05:31,360
back to the method as the value of yield.
112
00:05:31,360 --> 00:05:36,480
So whatever this returns, true or false, will be the value of result here, which then
113
00:05:36,480 --> 00:05:39,120
we can just go ahead and check as we did before.
114
00:05:39,120 --> 00:05:44,160
In the same way, we can go ahead and change this water level stuff to be with checking.
115
00:05:44,160 --> 00:05:46,800
This is going to be the level.
116
00:05:46,800 --> 00:05:49,440
We take all this boilerplate code out here.
117
00:05:49,440 --> 00:05:54,840
Just check the sensor level is greater than 3 and get rid of all this duplicated code.
118
00:05:54,840 --> 00:05:59,320
So now if we run it, where we get the same output, depending on the random numbers there.
119
00:05:59,320 --> 00:06:03,760
But the really cool part of this is this is nice, tidy, checking code here.
120
00:06:03,760 --> 00:06:08,880
And we've got all this boilerplate output stuff up in this with checking method.
121
00:06:08,880 --> 00:06:11,640
So again, we've separated concerns here.
122
00:06:11,640 --> 00:06:14,440
This method isn't concerned with what is being checked.
123
00:06:14,440 --> 00:06:19,080
It just knows to look at the result and print out a pass or a fail status.
124
00:06:19,080 --> 00:06:23,680
The block itself has the role of actually doing the checking.
125
00:06:23,680 --> 00:06:26,800
This makes it really easy to change the checking code.
126
00:06:26,800 --> 00:06:29,000
We could put the results in a database.
127
00:06:29,000 --> 00:06:31,040
We could email somebody at midnight.
128
00:06:31,040 --> 00:06:34,320
If the sensor fails, we could post it to a web server.
129
00:06:34,320 --> 00:06:36,200
Sure, it's all encapsulated in this method.
130
00:06:36,200 --> 00:06:38,480
We would just change what happens right here.
131
00:06:38,480 --> 00:06:41,240
And method all can also be used with an arbitrary block.
132
00:06:41,240 --> 00:06:44,560
Like we could check to see if we're happy if it's Saturday.
133
00:06:44,560 --> 00:06:47,720
Sure, this is our happiness level.
134
00:06:47,720 --> 00:06:49,200
And we just give it a block.
135
00:06:49,200 --> 00:06:53,440
We could just say time dot now to see if it's Saturday and Ruby would just say time dot
136
00:06:53,440 --> 00:06:57,000
now dot Saturday, question mark which returns to her false.
137
00:06:57,000 --> 00:06:58,200
So we run that.
138
00:06:58,200 --> 00:07:01,840
Well, unfortunately it's not Saturday, but we're still pretty happy.
139
00:07:01,840 --> 00:07:03,760
You'll use this same basic pattern.
140
00:07:03,760 --> 00:07:08,560
And each time you want to run a chunk of code before and after some code in the middle
141
00:07:08,560 --> 00:07:09,560
that varies.
142
00:07:09,560 --> 00:07:12,680
So basically you're executing around a block of code.
143
00:07:12,680 --> 00:07:20,080
We're going to riff on this pattern with some more examples in the next module.
144
00:07:20,080 --> 00:07:36,000
And before and after some code.
12663
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.