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,920
So we've looked at times and up to a method such that these that call-i-block repeatedly
2
00:00:07,920 --> 00:00:09,920
are referred to as iterator methods.
3
00:00:09,920 --> 00:00:15,640
Yeah, and Ruby Collection classes such as arrays and hashes have their own iterator methods.
4
00:00:15,640 --> 00:00:20,560
They iterate over the collection and they call a block for every element in that collection.
5
00:00:20,560 --> 00:00:24,320
So let's start by looking at how to iterate through an array since that's the most common
6
00:00:24,320 --> 00:00:27,400
collection class you'll probably be using in Ruby.
7
00:00:27,400 --> 00:00:32,159
Suppose we have this array of weekday names, they're just strings, and we want to loop
8
00:00:32,159 --> 00:00:37,560
through each element and print out the weekday and we want to capitalize.
9
00:00:37,560 --> 00:00:42,160
Well in other languages you might use a conventional four or a while loop and then just
10
00:00:42,160 --> 00:00:47,720
have it step through each element in that array and print it out in this case capitalized.
11
00:00:47,720 --> 00:00:53,360
But you won't actually use conventional loops often in Ruby because collection classes in Ruby
12
00:00:53,360 --> 00:00:55,800
have these built in iterator methods.
13
00:00:55,800 --> 00:00:59,800
For example, arrays have an each method that iterates through the array.
14
00:00:59,800 --> 00:01:03,680
So if we have our weekdays array, we can call the each method.
15
00:01:03,680 --> 00:01:07,920
It takes a block so we'll have do and it's going to pass this a block parameter.
16
00:01:07,920 --> 00:01:11,960
It's going to pass this each string in that array in turn which we're going to capture
17
00:01:11,960 --> 00:01:13,840
in this block parameter called day.
18
00:01:13,840 --> 00:01:15,200
And then we'll use end.
19
00:01:15,200 --> 00:01:19,200
And then inside of that block we just want to print out that string and we want it to be
20
00:01:19,200 --> 00:01:24,400
capitalized so we'll use the capitalized method on the string object.
21
00:01:24,400 --> 00:01:29,360
So if we run that, well we see that we get the five weekday names all printed in their
22
00:01:29,360 --> 00:01:34,480
capitalized form so that each method went through each element in that array for us and
23
00:01:34,480 --> 00:01:39,200
knew how to iterate through the array and then it just hands us the subsequent elements
24
00:01:39,200 --> 00:01:41,960
in that array, the elements in turn.
25
00:01:41,960 --> 00:01:46,320
Using this each iterator method is a lot less prone to error than looping through the array
26
00:01:46,320 --> 00:01:49,360
yourself using like a while or a four loop.
27
00:01:49,360 --> 00:01:53,200
So since this is just one line, we could simplify it using braces.
28
00:01:53,200 --> 00:01:54,680
Sure, let's just put it on one line.
29
00:01:54,680 --> 00:01:58,080
We've seen how to do that before but let's just clean this up a little bit, put this on
30
00:01:58,080 --> 00:02:02,800
its own line that way and then use curly braces at the end.
31
00:02:02,800 --> 00:02:07,560
We could even change this if we wanted to use D as the block parameter name.
32
00:02:07,560 --> 00:02:09,720
We could change that to D and just use it inside the blocks.
33
00:02:09,720 --> 00:02:12,560
We get a nice tidy chunk of code here.
34
00:02:12,560 --> 00:02:14,120
What an animation help?
35
00:02:14,120 --> 00:02:15,920
Well I have one for you.
36
00:02:15,920 --> 00:02:18,960
Here we have an array of weekday names, there's strings.
37
00:02:18,960 --> 00:02:22,320
All Ruby arrays have an each method that acts like a loop.
38
00:02:22,320 --> 00:02:27,799
It iterates through each element in the array and passes it to the associated block.
39
00:02:27,799 --> 00:02:29,920
What do we want to do with each weekday?
40
00:02:29,920 --> 00:02:32,880
For now we'll just print it out in its capitalized form.
41
00:02:32,880 --> 00:02:37,680
On the first iteration, the first element in the array, Monday, is assigned to the block
42
00:02:37,680 --> 00:02:39,720
parameter name day.
43
00:02:39,720 --> 00:02:42,880
Then we need to do something with this element inside the block.
44
00:02:42,880 --> 00:02:46,040
In this case, we print out the capitalized day.
45
00:02:46,040 --> 00:02:49,720
The each iterator then moves to the next element in the array.
46
00:02:49,720 --> 00:02:54,080
Tuesday is assigned to the block parameter and our block of code runs again.
47
00:02:54,080 --> 00:02:58,920
One by one, each string in the array is assigned to the day block parameter and the block
48
00:02:58,920 --> 00:03:00,920
is executed.
49
00:03:00,920 --> 00:03:05,640
Let's return to our order's example and iterate through our order objects.
50
00:03:05,640 --> 00:03:09,880
Like let's say we want to send out a newsletter and we want to print out all the emails
51
00:03:09,880 --> 00:03:11,280
for all our customers.
52
00:03:11,280 --> 00:03:15,880
Well this same basic iterator pattern applies to arrays containing any objects.
53
00:03:15,880 --> 00:03:17,760
So it's pretty straightforward.
54
00:03:17,760 --> 00:03:20,160
So we already have our array of orders right here.
55
00:03:20,160 --> 00:03:24,480
We just need to iterate through them with each and then print out the email associated with
56
00:03:24,480 --> 00:03:26,679
that order for our newsletter.
57
00:03:26,679 --> 00:03:30,079
So I'm just going to give a little bit of space down here and I'm going to print out a label
58
00:03:30,079 --> 00:03:31,799
just so that we know what we're printing out.
59
00:03:31,799 --> 00:03:35,640
These are our newsletter emails like that.
60
00:03:35,640 --> 00:03:38,120
Then we're going to have our orders array.
61
00:03:38,120 --> 00:03:39,320
That's the name of the array.
62
00:03:39,320 --> 00:03:42,880
We know it's got an each method on that and then we give it a block.
63
00:03:42,880 --> 00:03:47,160
In this particular case, the block parameter is going to be an order object.
64
00:03:47,160 --> 00:03:51,799
So I'll call it order because we have order objects inside of our orders array.
65
00:03:51,799 --> 00:03:54,440
That's what's going to get supplied as the block parameter.
66
00:03:54,440 --> 00:03:55,840
And then we have end.
67
00:03:55,840 --> 00:04:00,320
And then inside of the block, we just need to print out the email that's associated with
68
00:04:00,320 --> 00:04:01,680
that order.
69
00:04:01,680 --> 00:04:05,920
So if we run that, we've got all of our emails associated with the orders that are going
70
00:04:05,920 --> 00:04:07,920
to go in our newsletter.
71
00:04:07,920 --> 00:04:10,240
Now we can tidy this up on one line of course.
72
00:04:10,240 --> 00:04:15,120
We'll just use the curly brace there and then we'll pull this line up here and then we'll
73
00:04:15,120 --> 00:04:17,959
get rid of the end and end with a curly brace.
74
00:04:17,959 --> 00:04:23,320
And of course, we could change this to O, nice tidy chunk of code here.
75
00:04:23,320 --> 00:04:27,560
The O object will represent our order and that should work just the same.
76
00:04:27,560 --> 00:04:28,720
Well, we're at it.
77
00:04:28,720 --> 00:04:31,040
We could sum up the totals from all the orders.
78
00:04:31,040 --> 00:04:32,960
Well, that's a good idea.
79
00:04:32,960 --> 00:04:34,960
So I'm just going to give a little bit of space here.
80
00:04:34,960 --> 00:04:37,880
We're going to sum these things up in some sort of total.
81
00:04:37,880 --> 00:04:40,920
I'm going to use a variable to record that or keep track of that.
82
00:04:40,920 --> 00:04:42,320
I'm going to have a variable called sum.
83
00:04:42,320 --> 00:04:45,440
And this is going to be assigned the value zero to start with.
84
00:04:45,440 --> 00:04:46,760
Then we're going to have our block.
85
00:04:46,760 --> 00:04:48,719
We're going to have our orders array.
86
00:04:48,719 --> 00:04:49,719
We're going to call each.
87
00:04:49,719 --> 00:04:54,400
We need to iterate through each of the orders and get a hold of their total and then
88
00:04:54,400 --> 00:04:58,320
add them to this sum variable, which will be the running total as we go.
89
00:04:58,320 --> 00:04:59,400
So I'm going to do this on one line.
90
00:04:59,400 --> 00:05:02,159
We're going to have a block here with curly braces.
91
00:05:02,159 --> 00:05:03,159
Our block parameter.
92
00:05:03,159 --> 00:05:04,000
I'm going to call O again.
93
00:05:04,000 --> 00:05:05,880
It represents an order.
94
00:05:05,880 --> 00:05:10,000
And then what we need to do is add that order's total to the sum variable.
95
00:05:10,000 --> 00:05:13,120
So we can do that using the plus equal operator.
96
00:05:13,120 --> 00:05:16,120
And then we're going to have O dot total.
97
00:05:16,120 --> 00:05:17,120
Just like that.
98
00:05:17,120 --> 00:05:22,240
So it's going to add the each orders total to the sum variable using this plus equal
99
00:05:22,240 --> 00:05:23,240
operator.
100
00:05:23,240 --> 00:05:27,760
Now remember, because sum is defined outside of the block before the block here, that variable
101
00:05:27,760 --> 00:05:33,120
was defined inside the block when we refer to sum, these will be the same variable.
102
00:05:33,120 --> 00:05:36,560
So we'll be updating this sum variable as we go.
103
00:05:36,560 --> 00:05:40,320
Then after the block runs, we can reference that sum variable, and I'm just going to print
104
00:05:40,320 --> 00:05:43,680
out total sales.
105
00:05:43,680 --> 00:05:48,560
And then give a dollar sign and then interpolate the value of sum there.
106
00:05:48,560 --> 00:05:49,800
We run that.
107
00:05:49,800 --> 00:05:52,600
Now we see that the total sales are $150.
108
00:05:52,600 --> 00:05:55,720
We'll see an easier way to do this later.
109
00:05:55,720 --> 00:05:58,280
But first, here's a quick recap.
110
00:05:58,280 --> 00:06:00,680
This time we have an array of order objects.
111
00:06:00,680 --> 00:06:04,680
We call the each method on the array with the associated block.
112
00:06:04,680 --> 00:06:09,440
And the first iteration, the first object of the array, mose order, is assigned to the
113
00:06:09,440 --> 00:06:13,200
block parameter order, which remember is just a variable.
114
00:06:13,200 --> 00:06:17,840
The block of code then runs and prints out the value of the email attribute for the order,
115
00:06:17,840 --> 00:06:19,560
mose email address.
116
00:06:19,560 --> 00:06:24,560
Then the next order object is assigned to the order block parameter and the block is executed.
117
00:06:24,560 --> 00:06:27,320
And so on for all the orders in the array.
118
00:06:27,320 --> 00:06:28,440
Here's the thing.
119
00:06:28,440 --> 00:06:32,840
When calling each, the kind of object that gets assigned to the block parameter depends
120
00:06:32,840 --> 00:06:34,679
on what's in the array.
121
00:06:34,679 --> 00:06:37,640
In this case, it's always an order object.
122
00:06:37,640 --> 00:06:41,359
Inside the block, you can do whatever you want with that object.
123
00:06:41,359 --> 00:06:45,280
The other common collection you'll see in Ruby is a hash with key value pairs.
124
00:06:45,280 --> 00:06:48,359
And no surprise, it has an each iterator method, too.
125
00:06:48,359 --> 00:06:50,080
Yeah, but it works a little bit differently.
126
00:06:50,080 --> 00:06:52,760
It actually passes two parameters to the block.
127
00:06:52,760 --> 00:06:54,599
So let's look at that.
128
00:06:54,599 --> 00:06:58,719
Suppose we have this tax table represented in hash.
129
00:06:58,720 --> 00:07:03,640
We want to print out each state and its tax as a percentage.
130
00:07:03,640 --> 00:07:06,280
OK, so we take our taxes hash.
131
00:07:06,280 --> 00:07:08,440
It has an each method on it.
132
00:07:08,440 --> 00:07:12,640
But instead of taking one block parameter, it actually takes two block parameters.
133
00:07:12,640 --> 00:07:16,440
The first block parameter is going to be the key, which in this case is going to be our state.
134
00:07:16,440 --> 00:07:21,040
The second block parameter is the value, which is going to be the tax percentage here.
135
00:07:21,040 --> 00:07:24,640
And you notice I separated the block parameters with a comment.
136
00:07:24,640 --> 00:07:26,760
We just end it as we would in the other block.
137
00:07:26,760 --> 00:07:30,920
And inside of here, we'll just print out the key.
138
00:07:30,920 --> 00:07:33,039
And then we'll print out the value.
139
00:07:33,039 --> 00:07:38,480
But we'll multiply the value times 100 so that we get a percentage.
140
00:07:38,480 --> 00:07:40,120
And we see we've got our states.
141
00:07:40,120 --> 00:07:42,880
And then we get the percentage printed out just like that.
142
00:07:42,880 --> 00:07:47,599
So the takeaway here is that the each method may work differently depending on the type
143
00:07:47,599 --> 00:07:48,719
of the collection.
144
00:07:48,719 --> 00:07:52,560
Calling each on a ray, we get one block parameter, which represents the element in the
145
00:07:52,560 --> 00:07:57,120
ray, if we call each on a hash, we get two block parameters, one for the key, and one for
146
00:07:57,120 --> 00:07:58,120
the value.
147
00:07:58,120 --> 00:08:01,520
So now it's your turn to write a few each iterators with blocks.
148
00:08:01,520 --> 00:08:05,200
And in the exercise, you'll find a whole bunch of different examples to practice with.
149
00:08:05,200 --> 00:08:08,400
So we now know how to iterate through collections with blocks.
150
00:08:08,400 --> 00:08:13,200
But you may also want to filter or sort or find things in those collections.
151
00:08:13,200 --> 00:08:18,200
And then the next section will look at how to do that by tapping into the power of the innumerable
152
00:08:18,200 --> 00:08:19,200
module.
153
00:08:19,200 --> 00:08:23,080
Come on back.
14210
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.