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:04,840
Now, here's the cool thing.
2
00:00:04,840 --> 00:00:10,640
You now have all the pieces to understand exactly how the innumerable module works.
3
00:00:10,640 --> 00:00:13,520
So let's go behind the scenes in Ruby again.
4
00:00:13,520 --> 00:00:19,200
Let's suppose we didn't have a new marble and instead we wrote our own like my innumerable.
5
00:00:19,200 --> 00:00:21,880
Okay, this will be a good little practice exercise.
6
00:00:21,880 --> 00:00:24,200
We'll call this my innumerable.
7
00:00:24,200 --> 00:00:27,880
So it's as if Ruby never gave us the innumerable module.
8
00:00:27,880 --> 00:00:32,519
And then down below where we're using this, all we've got in this example is select being
9
00:00:32,519 --> 00:00:35,720
called and we're going to change that to my understore select.
10
00:00:35,720 --> 00:00:38,200
So we're going to write our own select method here.
11
00:00:38,200 --> 00:00:42,760
Okay, to make this work, we need to define our own my innumerable module, which I'm going
12
00:00:42,760 --> 00:00:46,040
to do just about the movie queue here.
13
00:00:46,040 --> 00:00:52,480
So the module is my innumerable.
14
00:00:52,480 --> 00:00:55,800
And we want to define our my select method.
15
00:00:55,800 --> 00:01:00,440
And select just needs to iterate through all the movies in our movie queue and then return
16
00:01:00,440 --> 00:01:03,519
a new array with the selected movies.
17
00:01:03,519 --> 00:01:05,519
Right, so let's just start by iterating through the movies.
18
00:01:05,519 --> 00:01:09,759
We do movies.each, that's going to give us the movie.
19
00:01:09,759 --> 00:01:13,039
And then we would do something with that inside of the block.
20
00:01:13,039 --> 00:01:14,720
But here's the problem with this.
21
00:01:14,720 --> 00:01:19,880
If we do it this way, then it's only going to work when we include my innumerable in our
22
00:01:19,880 --> 00:01:26,600
movie queue class because it assumes that whatever class this module gets mixed into has an
23
00:01:26,600 --> 00:01:29,039
instance variable called at movies.
24
00:01:29,039 --> 00:01:30,640
And that's not going to be very generic.
25
00:01:30,640 --> 00:01:34,039
Again, it's only going to work for the movie queue class.
26
00:01:34,039 --> 00:01:39,240
So instead, we can make this more flexible simply by calling the each method.
27
00:01:39,240 --> 00:01:44,360
Remember, innumerable requires the host class to define an each method and it uses that
28
00:01:44,360 --> 00:01:48,320
each method to get a hold of all the objects that are in that collection.
29
00:01:48,320 --> 00:01:51,639
It doesn't care where they came from or which instance variable they're stored in.
30
00:01:51,639 --> 00:01:54,440
It's just going to call the each method to get those objects.
31
00:01:54,440 --> 00:01:58,479
And then I'll make this block parameter a little bit more generic just by calling it value.
32
00:01:58,479 --> 00:02:00,039
You'd call that whatever you want.
33
00:02:00,039 --> 00:02:03,440
Okay, that's how we're going to iterate through all the things in the collection.
34
00:02:03,440 --> 00:02:06,199
Now for the actual selecting of those objects.
35
00:02:06,199 --> 00:02:08,919
Well, we know that the select method returns a new array.
36
00:02:08,919 --> 00:02:13,040
So I'm going to create a new array that's just empty here at the top.
37
00:02:13,040 --> 00:02:19,600
And inside of this iteration, we want to append things to that array.
38
00:02:19,600 --> 00:02:21,200
What do we want in the array?
39
00:02:21,200 --> 00:02:24,440
Well, we want the value, which in this case would be a movie.
40
00:02:24,440 --> 00:02:31,840
But we only want that movie to be in the new array if the block actually says the criteria
41
00:02:31,840 --> 00:02:33,840
says that it should be selected.
42
00:02:33,840 --> 00:02:35,120
So what do we do here?
43
00:02:35,120 --> 00:02:38,280
Well, we yield the value to the block.
44
00:02:38,280 --> 00:02:45,120
So the block's going to get the movie if the block returns true, then we're going to append
45
00:02:45,120 --> 00:02:46,560
the value to the new array.
46
00:02:46,560 --> 00:02:47,880
The block returns false.
47
00:02:47,880 --> 00:02:48,880
We're going to ignore it.
48
00:02:48,880 --> 00:02:51,680
So we only select the ones where the block returns true.
49
00:02:51,680 --> 00:02:56,280
And at the end, we need to make sure to return the new array just implicitly return it by
50
00:02:56,280 --> 00:02:59,680
making it the last statement there in the my select method.
51
00:02:59,680 --> 00:03:02,080
All right, now if we save this and run it.
52
00:03:02,080 --> 00:03:06,600
Well, it's selected cast away because it had a duration greater than 140.
53
00:03:06,600 --> 00:03:10,920
So it's selected that one movie, which matched this criteria right here.
54
00:03:10,920 --> 00:03:13,760
So our my select method is working just like this.
55
00:03:13,760 --> 00:03:18,000
Let's let's make our own map method next.
56
00:03:18,000 --> 00:03:21,920
Sure, to set this up, let's say we want to take all the movies and map them into an array
57
00:03:21,920 --> 00:03:23,720
of just movie titles.
58
00:03:23,720 --> 00:03:25,440
Have a titles variable.
59
00:03:25,440 --> 00:03:26,440
We take our queue.
60
00:03:26,440 --> 00:03:30,720
We want to call my map, which will be just like the map method when we're done here.
61
00:03:30,720 --> 00:03:33,760
The block, this is going to pass the movie object to that.
62
00:03:33,760 --> 00:03:39,000
And we're going to map it to the movie's title and we'll go ahead and down case that.
63
00:03:39,000 --> 00:03:40,720
Just so it shows up a little bit differently.
64
00:03:40,720 --> 00:03:43,320
And then we can print out the titles like that.
65
00:03:43,320 --> 00:03:48,880
All right, so we're going to map movies into movie titles into an array titles.
66
00:03:48,880 --> 00:03:52,239
Need to find the map method or the my map method in this case.
67
00:03:52,239 --> 00:03:57,480
So back up in our my innumerable my map.
68
00:03:57,480 --> 00:03:59,519
So this is very similar to my select.
69
00:03:59,519 --> 00:04:03,600
I'm actually going to go ahead and copy and paste this just to show the differences here.
70
00:04:03,600 --> 00:04:08,560
In my map, we're going to iterate through all the movies just like we did before.
71
00:04:08,560 --> 00:04:11,079
The difference is right inside of here.
72
00:04:11,079 --> 00:04:15,720
Remember, the block is going to return the transformed value.
73
00:04:15,720 --> 00:04:18,560
It's going to return in this case the movie titles.
74
00:04:18,560 --> 00:04:23,840
So for each element in the array, we want to yield that element as we're doing right here
75
00:04:23,840 --> 00:04:28,480
and append whatever the block returns to our new array.
76
00:04:28,480 --> 00:04:30,840
So we don't need to do this if checking it all.
77
00:04:30,840 --> 00:04:33,599
Now we don't have to do the value here.
78
00:04:33,599 --> 00:04:36,919
We're just going to say, okay, yield the value to the block.
79
00:04:36,919 --> 00:04:42,280
Remember, yield has a result that gets returned by the block that we then just append into
80
00:04:42,280 --> 00:04:44,239
the new array.
81
00:04:44,239 --> 00:04:45,719
Say that and run it.
82
00:04:45,719 --> 00:04:49,479
And sure enough, we see we've got a new array that contains the movie titles all
83
00:04:49,479 --> 00:04:50,479
down case.
84
00:04:50,479 --> 00:04:53,799
So it works basically just like map does.
85
00:04:53,799 --> 00:04:57,520
Now obviously, you won't need to define your own innumerable module because Ruby's
86
00:04:57,520 --> 00:04:58,919
already done that for you.
87
00:04:58,920 --> 00:05:02,920
But if you understand how the methods in the new mobile module work, then you're well
88
00:05:02,920 --> 00:05:06,040
on your way to mastering Ruby blocks and iterators.
89
00:05:06,040 --> 00:05:29,920
So when the exercise try your hand at writing some other innumerable methods.
8249
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.