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:06,500
Okay, let's jump right in and write some basic blocks.
2
00:00:06,500 --> 00:00:09,100
Yeah, starting with some basic blocks syntax and style,
3
00:00:09,100 --> 00:00:11,300
we'll give us a good foundation to build on.
4
00:00:11,300 --> 00:00:13,500
So we're using this sublime text editor here,
5
00:00:13,500 --> 00:00:18,000
and I've got a new file open called block underscore basics.rb.
6
00:00:18,000 --> 00:00:20,100
We're just going to use this as a little scratch pad
7
00:00:20,100 --> 00:00:22,300
for playing around with some blocks here.
8
00:00:22,300 --> 00:00:24,600
So the first question is, what is a block?
9
00:00:24,600 --> 00:00:26,600
Well, the block is simply a chunk of code
10
00:00:26,600 --> 00:00:28,900
that goes between do and end.
11
00:00:28,900 --> 00:00:32,000
So we can put any ruby code we want between do and end here.
12
00:00:32,000 --> 00:00:35,000
I'm just going to print out echo just like that.
13
00:00:35,000 --> 00:00:39,200
So this is the block, but we can't run the block directly.
14
00:00:39,200 --> 00:00:41,400
Instead, to get this block of code to run,
15
00:00:41,400 --> 00:00:45,400
we need to attach or associate it with a method call.
16
00:00:45,400 --> 00:00:48,000
Now ruby has a number of methods that will take blocks
17
00:00:48,000 --> 00:00:48,800
and run them.
18
00:00:48,800 --> 00:00:51,099
The method we're going to use is the time method.
19
00:00:51,099 --> 00:00:52,300
And we call it on a number.
20
00:00:52,300 --> 00:00:54,800
I'm going to call it on the number three, three times.
21
00:00:54,800 --> 00:00:55,900
That's the method name.
22
00:00:55,900 --> 00:00:58,700
And then we attach the block by simply putting it
23
00:00:58,700 --> 00:01:00,200
after the method call there.
24
00:01:00,200 --> 00:01:02,900
Now to run the block inside a sublime text,
25
00:01:02,900 --> 00:01:05,000
I'm just going to use command to be on a Mac
26
00:01:05,000 --> 00:01:07,600
or you can use control be on a PC.
27
00:01:07,600 --> 00:01:08,900
And we get our output down here.
28
00:01:08,900 --> 00:01:12,200
We see it printed echo three times, no surprise.
29
00:01:12,200 --> 00:01:14,300
Don't worry about this little finished thing down here.
30
00:01:14,300 --> 00:01:15,800
That's just the blind text telling us
31
00:01:15,800 --> 00:01:17,800
that it finished running the code.
32
00:01:17,800 --> 00:01:20,400
So when the time's method runs, it turns around
33
00:01:20,400 --> 00:01:24,400
and runs the code in the block three times in this case.
34
00:01:24,400 --> 00:01:28,500
The block kind of looks like a parameter to the method.
35
00:01:28,500 --> 00:01:29,800
Well, it does look like a parameter,
36
00:01:29,800 --> 00:01:31,700
but it's actually not a method parameter.
37
00:01:31,700 --> 00:01:34,700
And we can make that more explicit after the time's method.
38
00:01:34,700 --> 00:01:37,400
We can use parentheses where we would normally put
39
00:01:37,400 --> 00:01:39,800
method parameters inside of those parentheses.
40
00:01:39,800 --> 00:01:43,200
And we see that the block actually comes after those parentheses.
41
00:01:43,200 --> 00:01:45,400
So it's not a parameter to the method.
42
00:01:45,400 --> 00:01:46,300
In fact, we run this.
43
00:01:46,300 --> 00:01:48,000
We get exactly the same thing.
44
00:01:48,000 --> 00:01:50,700
So instead, the block is simply associated with
45
00:01:50,700 --> 00:01:53,600
or attached to the method call in this case
46
00:01:53,600 --> 00:01:55,400
that time's method call.
47
00:01:55,400 --> 00:01:58,400
That don't worry about this. We're going to explore this throughout
48
00:01:58,400 --> 00:01:59,500
the entire course.
49
00:01:59,500 --> 00:02:03,500
Yeah, and we'll see a case where the method actually takes a parameter in just a minute.
50
00:02:03,500 --> 00:02:06,600
So the convention is to use this do-in-in style
51
00:02:06,600 --> 00:02:08,500
when you have a multi-line block.
52
00:02:08,500 --> 00:02:10,200
Now, we've just got a single line block here.
53
00:02:10,200 --> 00:02:13,000
We've just got a single line of Ruby code inside of this block.
54
00:02:13,000 --> 00:02:15,100
When you have a single line block like this,
55
00:02:15,100 --> 00:02:18,100
instead of using do-in-in, you can use curly braces.
56
00:02:18,100 --> 00:02:20,100
I'm just going to bring this line to code back up here,
57
00:02:20,100 --> 00:02:23,400
put it on the same line, get rid of the end,
58
00:02:23,400 --> 00:02:26,200
and just use curly braces to surround the block.
59
00:02:26,200 --> 00:02:28,600
So here we've got our method call,
60
00:02:28,600 --> 00:02:30,700
and then here's the block after the method call.
61
00:02:30,700 --> 00:02:33,900
Just like we did with doing in, this time it's just on a single line.
62
00:02:33,900 --> 00:02:36,900
If we run this, we get the same thing.
63
00:02:36,900 --> 00:02:39,400
So let's look at blocks in a different scenario.
64
00:02:39,400 --> 00:02:43,100
Let's say we have a really simple order class like this one.
65
00:02:43,100 --> 00:02:46,200
An order has a customer's email and total amount,
66
00:02:46,200 --> 00:02:49,200
which we initialize, and then we wrote our own 2S method,
67
00:02:49,200 --> 00:02:51,900
which just prints that out that information.
68
00:02:51,900 --> 00:02:55,100
The first thing we want to do is create five example orders
69
00:02:55,100 --> 00:02:56,600
and put them in an array,
70
00:02:56,600 --> 00:02:59,800
and we can use the Times method to do that fairly quickly.
71
00:02:59,800 --> 00:03:01,300
Sure, we'll just do it right down here.
72
00:03:01,300 --> 00:03:04,100
I'm just going to add a little bit of space to give us a little bit of room.
73
00:03:04,100 --> 00:03:05,500
You want to put them into an array,
74
00:03:05,500 --> 00:03:07,700
so let's start with an empty Ruby array.
75
00:03:07,700 --> 00:03:09,600
Just like that, we'll just call it orders.
76
00:03:09,600 --> 00:03:11,100
Let's do five orders.
77
00:03:11,100 --> 00:03:14,400
Okay, so instead of using three times where you use five times,
78
00:03:14,400 --> 00:03:15,900
and then we're going to give it a block.
79
00:03:15,900 --> 00:03:18,900
Block begins with do-ins with end and inside of there,
80
00:03:18,900 --> 00:03:20,900
we can put any Ruby code we want.
81
00:03:20,900 --> 00:03:23,000
In this case, we want to create an order
82
00:03:23,000 --> 00:03:25,100
and append it to this order's array.
83
00:03:25,100 --> 00:03:26,500
So we're going to take the orders array,
84
00:03:26,500 --> 00:03:28,500
use the Append operator like that,
85
00:03:28,500 --> 00:03:30,500
and then create a new order.
86
00:03:30,500 --> 00:03:32,500
After passing the customer's email,
87
00:03:32,500 --> 00:03:35,300
which I'll just use customer at example.
88
00:03:35,300 --> 00:03:39,500
Dot com, and then we'll give the order total of say $10.
89
00:03:39,500 --> 00:03:41,500
Didn't down below here, we want to print out these orders,
90
00:03:41,500 --> 00:03:43,900
and I'm just going to use puts on orders.
91
00:03:43,900 --> 00:03:46,100
If you call puts on an array,
92
00:03:46,100 --> 00:03:48,900
it just turns around, it loops through that array for you,
93
00:03:48,900 --> 00:03:51,300
and then it gets the string returned by 2S,
94
00:03:51,300 --> 00:03:52,500
which we have one of those,
95
00:03:52,500 --> 00:03:54,100
and then it will go ahead and print that out.
96
00:03:54,100 --> 00:03:55,900
So if we save that and run it,
97
00:03:55,900 --> 00:03:57,700
we see that we have five orders.
98
00:03:57,700 --> 00:04:00,700
They have the same customers' email and total.
99
00:04:00,700 --> 00:04:02,100
You know what we'd like instead,
100
00:04:02,100 --> 00:04:04,500
we'd like each order to be kind of unique.
101
00:04:04,500 --> 00:04:07,900
Maybe have a unique email and a unique total amount.
102
00:04:07,900 --> 00:04:10,300
Sure, let's just return to our block basics file here,
103
00:04:10,300 --> 00:04:12,300
and see how we can change that around.
104
00:04:12,300 --> 00:04:15,300
So I'm going to start by changing this back around to do an end.
105
00:04:15,300 --> 00:04:17,700
It's going to make it a little easier to demonstrate,
106
00:04:17,700 --> 00:04:18,500
like that.
107
00:04:18,500 --> 00:04:21,100
Now it turns out that the times method will pass
108
00:04:21,100 --> 00:04:23,100
the iteration number to the block,
109
00:04:23,100 --> 00:04:26,100
and we can pick that up in what's called a block parameter,
110
00:04:26,100 --> 00:04:28,300
and we put block parameters after do,
111
00:04:28,300 --> 00:04:30,300
and they go inside of vertical bars.
112
00:04:30,300 --> 00:04:32,500
So I'm going to call the parameter number.
113
00:04:32,500 --> 00:04:34,500
It's just a variable, so we can call it whatever we want,
114
00:04:34,500 --> 00:04:35,900
but I'll call it number here.
115
00:04:35,900 --> 00:04:37,300
Then inside of the block,
116
00:04:37,300 --> 00:04:39,700
we can reference that variable number
117
00:04:39,700 --> 00:04:41,900
to print out the number of the iteration
118
00:04:41,900 --> 00:04:43,500
as this block is run.
119
00:04:43,500 --> 00:04:45,100
Go ahead and run that.
120
00:04:45,100 --> 00:04:47,900
And we see that the time method starts with zero
121
00:04:47,900 --> 00:04:50,099
and counts zero, one and two.
122
00:04:50,099 --> 00:04:51,900
So each time it calls the block,
123
00:04:51,900 --> 00:04:55,099
it passes the number in that block parameter called number,
124
00:04:55,099 --> 00:04:56,500
and then we just print it out.
125
00:04:56,500 --> 00:04:58,700
So just to demonstrate that that's a variable,
126
00:04:58,700 --> 00:05:01,299
we can actually change number to just end.
127
00:05:01,299 --> 00:05:03,500
Sure, in fact, with really short blocks like this,
128
00:05:03,500 --> 00:05:06,500
it's very common just to use single letter block parameter.
129
00:05:06,500 --> 00:05:08,900
So as long as we call it end as a block parameter there,
130
00:05:08,900 --> 00:05:12,099
and reference it with the same name, it works just the same.
131
00:05:12,099 --> 00:05:14,299
Now we'll change it back to the single line form,
132
00:05:14,299 --> 00:05:15,500
and we'll just take the do,
133
00:05:15,500 --> 00:05:16,900
that'll be a brace there.
134
00:05:16,900 --> 00:05:19,099
We'll move this back up to that line,
135
00:05:19,099 --> 00:05:21,700
and we'll get rid of end, put our curly brace there.
136
00:05:21,700 --> 00:05:24,700
So you see that the block parameter just goes inside of the block
137
00:05:24,700 --> 00:05:27,099
to the right hand side of this opening curly brace.
138
00:05:27,099 --> 00:05:29,299
So we've got our method called here,
139
00:05:29,299 --> 00:05:32,099
and we've got our block here.
140
00:05:32,099 --> 00:05:34,000
All right, so let's go back to our orders,
141
00:05:34,000 --> 00:05:36,700
and we can now apply this to creating our orders
142
00:05:36,700 --> 00:05:39,099
to have unique emails and totals.
143
00:05:39,099 --> 00:05:42,299
Sure, I'll just jump back over to our order.rb file.
144
00:05:42,299 --> 00:05:43,900
And down where we're creating the orders
145
00:05:43,900 --> 00:05:44,700
with our time method,
146
00:05:44,700 --> 00:05:46,500
we know that it's going to give us the number of the order,
147
00:05:46,500 --> 00:05:48,500
in fact in this case, I'm going to go ahead and use N
148
00:05:48,500 --> 00:05:49,900
just like we did before.
149
00:05:49,900 --> 00:05:51,400
So there's our block parameter.
150
00:05:51,400 --> 00:05:53,500
Now when we're creating the customer,
151
00:05:53,500 --> 00:05:55,900
or the order in this case with the customers email,
152
00:05:55,900 --> 00:05:58,500
I'll just interpolate that number there,
153
00:05:58,500 --> 00:06:01,300
that way we'll get like customer 1, 2, 3, and so on.
154
00:06:01,300 --> 00:06:03,100
And in fact, we'll change the totals too.
155
00:06:03,100 --> 00:06:04,900
I'll take that number whatever it is,
156
00:06:04,900 --> 00:06:07,500
and we'll just multiply it by 10.
157
00:06:07,500 --> 00:06:09,200
We say that and run it.
158
00:06:09,200 --> 00:06:11,900
We see we now have different customer emails.
159
00:06:11,900 --> 00:06:14,100
They start with zero and go all the way up to four,
160
00:06:14,100 --> 00:06:17,300
and then total starting with zero going up to 40.
161
00:06:17,300 --> 00:06:18,900
So our orders are all different now.
162
00:06:18,900 --> 00:06:21,300
There's zero through 40, but it would be better
163
00:06:21,300 --> 00:06:25,100
if they were like 10 through 50 and customers 1 through 5.
164
00:06:25,100 --> 00:06:27,300
You don't really want to be customer zero.
165
00:06:27,300 --> 00:06:29,300
So maybe we could change that.
166
00:06:29,300 --> 00:06:31,500
Yeah, the time method's not going to work in this case.
167
00:06:31,500 --> 00:06:33,100
So we're going to need to use another method.
168
00:06:33,100 --> 00:06:36,300
So let's go back to our scratch pad and play around with it.
169
00:06:36,300 --> 00:06:37,900
So the time method won't work for us
170
00:06:37,900 --> 00:06:39,900
because it starts counting at zero,
171
00:06:39,900 --> 00:06:42,600
but there's another method we can use, the up to method.
172
00:06:42,600 --> 00:06:47,600
So we want to start with one, and we want to go up to a different number.
173
00:06:47,600 --> 00:06:49,800
In this case, we want to go 1 through 3.
174
00:06:49,800 --> 00:06:51,600
So 1 up to 3.
175
00:06:51,600 --> 00:06:54,100
Now notice that 3 is a method parameter.
176
00:06:54,100 --> 00:06:55,600
It's in parentheses here.
177
00:06:55,600 --> 00:06:57,800
It's part of this method call.
178
00:06:57,800 --> 00:07:00,900
The block comes after the method call,
179
00:07:00,900 --> 00:07:02,100
which includes the parameter.
180
00:07:02,100 --> 00:07:05,000
So there's the method call and there's the block.
181
00:07:05,000 --> 00:07:09,400
It's just important to remember that the parentheses go before the block,
182
00:07:09,400 --> 00:07:11,400
which is all the way on the right hand side.
183
00:07:11,400 --> 00:07:15,400
In other words, the code block is not a method parameter.
184
00:07:15,400 --> 00:07:16,900
So if we go ahead and run this,
185
00:07:16,900 --> 00:07:19,900
what we see that we iterate from 1 to 3,
186
00:07:19,900 --> 00:07:21,900
which is just what we want.
187
00:07:21,900 --> 00:07:24,400
All right, so now we know how to go back to our orders
188
00:07:24,400 --> 00:07:26,400
and create orders 1 through 5.
189
00:07:26,400 --> 00:07:28,400
Sure, we'll jump over there.
190
00:07:28,400 --> 00:07:30,400
Instead of using the time method here,
191
00:07:30,400 --> 00:07:32,400
we're just going to change this from 1.
192
00:07:32,400 --> 00:07:35,400
We're going to go up to 5 in this case.
193
00:07:35,400 --> 00:07:36,900
We've already got the block parameter.
194
00:07:36,900 --> 00:07:39,400
The block itself doesn't change at all.
195
00:07:39,400 --> 00:07:41,900
It's just like when we're using the time method,
196
00:07:41,900 --> 00:07:44,900
but we know that the up to method is going to give us the numbers
197
00:07:44,900 --> 00:07:46,400
sequentially as we want.
198
00:07:46,400 --> 00:07:49,700
If we run that, we get customer 1 through customer 5,
199
00:07:49,700 --> 00:07:51,900
and now our total is go from 10 to 50.
200
00:07:51,900 --> 00:07:52,900
Perfect.
201
00:07:52,900 --> 00:07:55,900
So so far, we've seen three forms of blocks.
202
00:07:55,900 --> 00:07:57,599
We started with a method call.
203
00:07:57,599 --> 00:08:00,599
In this case, the time is method on a number.
204
00:08:00,599 --> 00:08:02,299
Blocks cannot be run on their own.
205
00:08:02,299 --> 00:08:04,900
They must be associated with a method.
206
00:08:04,900 --> 00:08:09,200
Do marks the start of the block and in marks while the end.
207
00:08:09,200 --> 00:08:12,200
Inside the block, we can put any Ruby code we want.
208
00:08:12,200 --> 00:08:15,200
Here, the time method calls the block three times.
209
00:08:15,200 --> 00:08:17,200
Each time, printing out echo.
210
00:08:17,200 --> 00:08:21,200
For simple blocks like this, we saw how we can put them all on one line.
211
00:08:21,200 --> 00:08:25,200
When we do that, instead of do an end, we use curly braces.
212
00:08:25,200 --> 00:08:28,200
We also looked at how blocks can take parameters.
213
00:08:28,200 --> 00:08:33,200
A block parameter is simply a local variable that sits between two vertical bars,
214
00:08:33,200 --> 00:08:35,200
which some people call pipes or go posts.
215
00:08:35,200 --> 00:08:37,200
When the block is called,
216
00:08:37,200 --> 00:08:40,200
the value of the parameter is filled in by the method.
217
00:08:40,200 --> 00:08:44,200
In the case of the time's method, each time it calls the block,
218
00:08:44,200 --> 00:08:48,200
it passes the block the current iteration number zero through two,
219
00:08:48,200 --> 00:08:51,200
which then gets assigned to the number parameter.
220
00:08:51,200 --> 00:08:55,200
We then use the number parameter in the block to print the number.
221
00:08:55,200 --> 00:08:59,200
So you can think of a block as being like an anonymous method.
222
00:08:59,200 --> 00:09:03,200
It encapsulates a chunk of code and can take parameters.
223
00:09:03,200 --> 00:09:06,200
We can also write this block on one line again,
224
00:09:06,200 --> 00:09:08,200
curly braces in place of doing end.
225
00:09:08,200 --> 00:09:12,200
Finally, we call the method that itself takes a parameter.
226
00:09:12,200 --> 00:09:16,200
In this case, we called up to and passed it for as the limit,
227
00:09:16,200 --> 00:09:18,200
which is the method parameter.
228
00:09:18,200 --> 00:09:21,200
Then comes the associated block after the method parameters.
229
00:09:21,200 --> 00:09:24,200
The block parameter stays the same as with time,
230
00:09:24,200 --> 00:09:28,200
since up to also passes the current iteration number to the block.
231
00:09:28,200 --> 00:09:30,200
So we get four echoes.
232
00:09:30,200 --> 00:09:33,200
Again, we could write this block on a single line.
233
00:09:33,200 --> 00:09:39,200
Just make sure that due and end are curly braces come after the parentheses for the method parameters.
234
00:09:39,200 --> 00:09:43,200
Because remember, the associated code block is not a method parameter.
235
00:09:43,200 --> 00:09:48,200
You'll frequently see single line blocks simplified further by using a single character
236
00:09:48,200 --> 00:09:50,200
as the name of the block parameter.
237
00:09:50,200 --> 00:09:54,200
Remember the name is arbitrary, so we can replace number with n,
238
00:09:54,200 --> 00:09:57,200
and that gives us an elegant line of code.
239
00:09:57,200 --> 00:10:01,200
So now it's your turn to write some basic blocks in the exercise.
240
00:10:01,200 --> 00:10:06,200
And in the next module, we'll use blocks with another built in iterator method called each.
241
00:10:06,200 --> 00:10:10,200
Now you'll see each use all over the place in Ruby and Rails code.
242
00:10:10,200 --> 00:10:31,200
So come on back.
19673
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.