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:09,000
You may not realize it, but you now know how the built in times and each method's work.
2
00:00:09,000 --> 00:00:12,000
Now obviously you're not going to need to write these methods yourself.
3
00:00:12,000 --> 00:00:17,000
Ruby is already provided those, but understanding how they work will really deepen your understanding
4
00:00:17,000 --> 00:00:18,000
of Ruby.
5
00:00:18,000 --> 00:00:19,000
So let's go behind the scenes.
6
00:00:19,000 --> 00:00:24,000
So we've seen how we can use the Times method to call a block in this case three times.
7
00:00:24,000 --> 00:00:26,000
So how does that work?
8
00:00:26,000 --> 00:00:29,000
Well let's start by writing our own iterator method.
9
00:00:29,000 --> 00:00:33,000
The method will be called three underscore times, which isn't built in to Ruby.
10
00:00:33,000 --> 00:00:36,000
So it'll give us some good practice writing our own iterators here.
11
00:00:36,000 --> 00:00:39,000
We're just going to have it mimic what the Times method does.
12
00:00:39,000 --> 00:00:43,000
So up above here I'll define three times like that.
13
00:00:43,000 --> 00:00:47,000
And then one way to do it would just be to do yield zero,
14
00:00:47,000 --> 00:00:53,000
because we know the Times method starts with zero, yield one, and yield two.
15
00:00:53,000 --> 00:00:55,000
And that will work.
16
00:00:55,000 --> 00:01:00,000
But we want to do it this way if we had to yields to something like a hundred times.
17
00:01:00,000 --> 00:01:02,000
Yeah, this way doesn't work very well.
18
00:01:02,000 --> 00:01:05,000
So let's make this a little bit more generic.
19
00:01:05,000 --> 00:01:07,000
We'll get rid of all those yields inside of there.
20
00:01:07,000 --> 00:01:09,000
And instead let's use a while loop here.
21
00:01:09,000 --> 00:01:11,000
I'm going to start with a variable.
22
00:01:11,000 --> 00:01:14,000
I'm just going to call I because it's an incrementer here.
23
00:01:14,000 --> 00:01:16,000
And we're going to set it equal to zero.
24
00:01:16,000 --> 00:01:18,000
And we're going to use a traditional while loop.
25
00:01:18,000 --> 00:01:21,000
We're going to say while I is less than the number three,
26
00:01:21,000 --> 00:01:25,000
then what we want to do is yield whatever that number is.
27
00:01:25,000 --> 00:01:27,000
It's I in this case to the block.
28
00:01:27,000 --> 00:01:29,000
We're going to have to increment I.
29
00:01:29,000 --> 00:01:30,000
I plus equals one.
30
00:01:30,000 --> 00:01:31,000
We'll do that.
31
00:01:31,000 --> 00:01:33,000
And then we're done with the while loop.
32
00:01:33,000 --> 00:01:36,000
If we run this, we get the same thing.
33
00:01:36,000 --> 00:01:38,000
It starts to zero, goes one, and then two.
34
00:01:38,000 --> 00:01:42,000
Right, but that really still limits us only three iterations.
35
00:01:42,000 --> 00:01:44,000
Could we make it more generic?
36
00:01:44,000 --> 00:01:48,000
Yeah, we can make it more generic by actually passing in the number as part of the parameter here.
37
00:01:48,000 --> 00:01:52,000
So if we want to call this three times, we will pass in the number three.
38
00:01:52,000 --> 00:01:54,000
That's going to be a parameter to the method.
39
00:01:54,000 --> 00:01:57,000
And we'll actually change the method name to be called in times.
40
00:01:57,000 --> 00:02:00,000
Because now we can call it an arbitrary number of times,
41
00:02:00,000 --> 00:02:01,000
depending on the parameter.
42
00:02:01,000 --> 00:02:03,000
So this is going to be called in times.
43
00:02:03,000 --> 00:02:06,000
And we're going to take a method parameter here.
44
00:02:06,000 --> 00:02:07,000
I'm just going to call this number.
45
00:02:07,000 --> 00:02:10,000
And then while I is less than that number,
46
00:02:10,000 --> 00:02:12,000
go ahead and yield to the block.
47
00:02:12,000 --> 00:02:16,000
So if we call it in times three, zero one and two,
48
00:02:16,000 --> 00:02:19,000
or we could change it maybe we wanted to go all the way to ten,
49
00:02:19,000 --> 00:02:23,000
just change the parameter there, and we go zero through nine.
50
00:02:23,000 --> 00:02:25,000
So that's a pretty good start.
51
00:02:25,000 --> 00:02:29,000
But how do we get from here to actually being able to use it like we did the times method,
52
00:02:29,000 --> 00:02:31,000
where we put the number out here.
53
00:02:31,000 --> 00:02:35,000
That was the object that we called the method on ten in this case,
54
00:02:35,000 --> 00:02:38,000
and we call the method in times on that.
55
00:02:38,000 --> 00:02:41,000
Well, that's not going to work because this is an integer object in Ruby,
56
00:02:41,000 --> 00:02:45,000
and integers don't have a method called in times.
57
00:02:45,000 --> 00:02:47,000
But just by way of demonstration,
58
00:02:47,000 --> 00:02:50,000
we can actually add an in times method to the integer class.
59
00:02:50,000 --> 00:02:54,000
The way we do that is we just reopen the integer class in Ruby,
60
00:02:54,000 --> 00:03:00,000
and we'll put this method that we defined as an instance method on that class.
61
00:03:00,000 --> 00:03:03,000
Okay, we need to make a couple little changes here.
62
00:03:03,000 --> 00:03:07,000
One of them being, we don't want to pass in number there,
63
00:03:07,000 --> 00:03:09,000
because we already know what the number is.
64
00:03:09,000 --> 00:03:10,000
The number is ten.
65
00:03:10,000 --> 00:03:12,000
If the object we're calling the method on,
66
00:03:12,000 --> 00:03:15,000
so we're not passing it in as a parameter to the method there,
67
00:03:15,000 --> 00:03:18,000
so how do we pick up that number inside the method?
68
00:03:18,000 --> 00:03:22,000
Well, ten is an instance of the integer class,
69
00:03:22,000 --> 00:03:24,000
and its value is ten, and we can get a whole
70
00:03:24,000 --> 00:03:26,000
of that inside of an instance method,
71
00:03:26,000 --> 00:03:28,000
simply by using the variable self,
72
00:03:28,000 --> 00:03:31,000
which in this case will be ten.
73
00:03:31,000 --> 00:03:32,000
Now if we run this,
74
00:03:32,000 --> 00:03:35,000
what we go zero through nine again.
75
00:03:35,000 --> 00:03:37,000
If we change this number down here,
76
00:03:37,000 --> 00:03:38,000
maybe there's something like 12,
77
00:03:38,000 --> 00:03:40,000
now the value of self will be 12,
78
00:03:40,000 --> 00:03:43,000
so we go zero through 11.
79
00:03:43,000 --> 00:03:45,000
So that's basically how the times method works.
80
00:03:45,000 --> 00:03:48,000
If we call this times instead of in times,
81
00:03:48,000 --> 00:03:49,000
this would be times,
82
00:03:49,000 --> 00:03:51,000
and if we want to sort of prove to ourselves that we're calling
83
00:03:51,000 --> 00:03:53,000
our implementation of times,
84
00:03:53,000 --> 00:03:56,000
we could just put a print out here that just says yielding,
85
00:03:56,000 --> 00:03:59,000
and I'll print I, just like that.
86
00:03:59,000 --> 00:04:01,000
Okay, so we're calling 12. times,
87
00:04:01,000 --> 00:04:02,000
if we run this,
88
00:04:02,000 --> 00:04:05,000
we see that it yields, says yielding zero,
89
00:04:05,000 --> 00:04:08,000
we get zero echo, yielding one, one echo, and so on.
90
00:04:08,000 --> 00:04:11,000
So we know that we're calling this implementation of times.
91
00:04:11,000 --> 00:04:14,000
So again, that's basically how times
92
00:04:14,000 --> 00:04:16,000
is implemented in Ruby.
93
00:04:16,000 --> 00:04:19,000
Again, there's no reason for you to write a time method,
94
00:04:19,000 --> 00:04:22,000
because the one in Ruby works perfectly well,
95
00:04:22,000 --> 00:04:24,000
but we use this just in a way of demonstrating
96
00:04:24,000 --> 00:04:28,000
what you can do with yield and writing iterator methods.
97
00:04:28,000 --> 00:04:32,000
And from here, it's pretty easy to figure out how the each method works.
98
00:04:32,000 --> 00:04:36,000
So we've seen that we can iterate through and array using each.
99
00:04:36,000 --> 00:04:40,000
How can we write our own version called my each?
100
00:04:40,000 --> 00:04:41,000
We know this works.
101
00:04:41,000 --> 00:04:43,000
We're going to change this instead of using the built in each.
102
00:04:43,000 --> 00:04:46,000
We're going to call a method my underscore each.
103
00:04:46,000 --> 00:04:49,000
Now, that's not going to work because weekdays
104
00:04:49,000 --> 00:04:51,000
is actually an array object,
105
00:04:51,000 --> 00:04:53,000
and arrays don't have a method called my each,
106
00:04:53,000 --> 00:04:55,000
but we now know we can define our own.
107
00:04:55,000 --> 00:05:00,000
So up at the top, I'm going to reopen the array class in Ruby,
108
00:05:00,000 --> 00:05:05,000
and then we're going to define our my each method.
109
00:05:05,000 --> 00:05:07,000
What do we want to do here?
110
00:05:07,000 --> 00:05:09,000
Well, it's kind of similar to times.
111
00:05:09,000 --> 00:05:11,000
We're going to start with an incrementer variable,
112
00:05:11,000 --> 00:05:13,000
which we'll just call i.
113
00:05:13,000 --> 00:05:16,000
We're going to say, while i is less than some number.
114
00:05:16,000 --> 00:05:19,000
So how many times do we want to go through this loop?
115
00:05:19,000 --> 00:05:24,000
Well, we want to go through the loop one time for every element in the array.
116
00:05:24,000 --> 00:05:25,000
How do we get that?
117
00:05:25,000 --> 00:05:27,000
Well, arrays have a size method,
118
00:05:27,000 --> 00:05:30,000
and because we're inside of an instance method of the array class,
119
00:05:30,000 --> 00:05:32,000
we can use self to get a whole of the array,
120
00:05:32,000 --> 00:05:34,000
and then we can just call size.
121
00:05:34,000 --> 00:05:36,000
That'll take us through each element in the array.
122
00:05:36,000 --> 00:05:38,000
What do we want to do with that?
123
00:05:38,000 --> 00:05:43,000
Well, we want to yield whatever element is at that position in the array.
124
00:05:43,000 --> 00:05:48,000
So we can say yield self indexed by i.
125
00:05:48,000 --> 00:05:51,000
So this will be self zero would give us Monday,
126
00:05:51,000 --> 00:05:54,000
self one would give us Tuesday, and so on.
127
00:05:54,000 --> 00:05:55,000
Yield that to the block,
128
00:05:55,000 --> 00:05:58,000
make sure to increment our i variable,
129
00:05:58,000 --> 00:06:00,000
and then we're done.
130
00:06:00,000 --> 00:06:03,000
If we run that, sure enough it iterates Monday through Friday.
131
00:06:03,000 --> 00:06:05,000
Just as we would expect.
132
00:06:05,000 --> 00:06:07,000
So that's basically how the each method works.
133
00:06:07,000 --> 00:06:08,000
If we were writing our own,
134
00:06:08,000 --> 00:06:10,000
we would just change this for my age to each,
135
00:06:10,000 --> 00:06:12,000
just like that.
136
00:06:12,000 --> 00:06:15,000
One small detail with the built in each method that's a little bit different
137
00:06:15,000 --> 00:06:20,000
is it actually returns the array as the last line in the method,
138
00:06:20,000 --> 00:06:22,000
and that way you can do things like call each,
139
00:06:22,000 --> 00:06:23,000
which returns an array,
140
00:06:23,000 --> 00:06:25,000
and then you can chain them together.
141
00:06:25,000 --> 00:06:28,000
Say for example, you want it to map all of those days
142
00:06:28,000 --> 00:06:32,000
to something like day.upcase, for example.
143
00:06:32,000 --> 00:06:34,000
Oh, and I'd have to print those out.
144
00:06:34,000 --> 00:06:35,000
There's the array,
145
00:06:35,000 --> 00:06:38,000
and we've got our days all upcased.
146
00:06:38,000 --> 00:06:40,000
So that's basically how each works in array.
147
00:06:40,000 --> 00:06:43,000
Now obviously you shouldn't do this,
148
00:06:43,000 --> 00:06:45,000
but now you know there's no magic.
149
00:06:45,000 --> 00:06:48,000
Yeah, and knowing how things work in Ruby
150
00:06:48,000 --> 00:07:03,000
gives you a lot more confidence.
12265
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.