All language subtitles for pragstudio-ruby-blocks-05-iterators (Transcribed on 27-Apr-2023 21-16-58)

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified) Download
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranî)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu

Original subtitles

So we've used the each method to iterate through basic Ruby arrays, but what happens when you write your own collection class?

Well, let's look at an example.

Let's say we have a simple genres class that contains an array of genres in the name instance variable.

For our purposes, it's simply a collection of genre names.

Action, comedy, sci-fi, and adventure.

What we want to be able to do is iterate through the names using each.

Sure, so we might think we could just say genres.each.

Give it the block.

This is going to give us the name of the genre, and then we could just print it out like so.

But that's not going to work because genres is an object of the genre's class,

and it doesn't define an each method.

The genre's class has a or contains an array.

That's this names instance variable, and it has an each method,

but we're one level removed from it out here.

But we can define our own each method.

Up inside of our genre's class, we'll just have a method called each.

One way to do this would be to yield each of those things, yield action, yield comedy,

and so on, but that's not going to scale very well.

We're going to have an individual line for each of those names.

So there's a better way to do that, and that's just a piggyback on the each method that the names array already has.

Names isn't array, we can call each on that array, and then it's going to yield to us the name,

and then we can just turn around and yield it to the associated block for the each method,

which is this block down here.

So again, this method is just going to piggyback on the each method that's defined on the names array.

And now when we run this, well it prints out all of our genres.

So when writing your own collection class,

the role of the each method is to yield items to the associated block.

So let's look at another example.

This one will have two classes.

Here we have a movie class with some fairly obvious attributes,

title, rating, and duration time.

When we watch the movie, we print out, watching the name of the movie,

and it's rating and duration in minutes.

For this example, we created four movie objects.

We also have a movie cue class, which serves as a collection of movie objects in an array.

So to add a movie to the cue, we call the add movie method.

For this example, we have a cue named Friday night with three of the four movies.

What we want to be able to do is iterate through the movies in the cue by calling each.

So given our cue object, we want to be able to call each.

We want that to yield a movie object to us, which then we can watch by calling movie.watch.

That's the thing that prints out the movie we're actually watching.

Now we know this won't work because the cue object isn't an array.

It doesn't find a each method, but we can define that each method up in the movie cue class.

All we needed to do is loop through all the movies.

We'll use the each method on the movies array and then yield them in turn.

So this is going to hand us a movie object, which we're just going to turn around and yield to the associated block.

So in the same way we can ask an array for each of the elements, we can now ask a movie cue for each of its movies.

If we run this, we see that we're watching the three movies that are in our cue.

Now what's cool about this is from outside of the class, when we're calling each down here,

we don't care how those movies are stored internally in the class.

I think we stored in the database or off on some web service somewhere. The each method inside of the class

encapsulates all those details, but outside of the class, we don't have to worry about how all those movies are being managed.

We just call each and we know that it's going to yield each movie to us, however they're stored.

Now the convention is to name the most obvious iterator method each, and there's a good reason for that, which we'll see in a minute.

But you can also have other iterator methods, like let's say I only want to watch PG movies.

In addition to our regular each method, we can have another method called each PG movie, for example.

How's that defined?

Each PG movie?

Okay, well what we need to do here is select all the movies that have a rating of PG.

We know how to do that. We can call select on the movies array.

Select all the movies we're rating equals PG.

That's going to return an array, remember, with all those select movies.

We can chain this together with an each, which will go through all those select movies, and then yield them to the associated block.

We run this. We see that we're only watching the PG movies now.

You know, we could make this more generic so that you could watch movies of any rating.

That's true. Instead of using each PG movie, we might call this something like each by rating.

And then just pass in a rating variable, which will substitute in here.

So it's not hard-coded. This would be rating.

And then down here, we'll just call each by rating.

This method takes a parameter, which is the rating we want.

This case will pass in PG, and we get those two movies we had before, or if we just wanted to watch the G movies.

We've got one G movie in our queue.

So Mike mentioned earlier that you want to name your most common iterator each, and there's really a good reason for that.

Yeah, it turns out that by defining an each method, you unlock all the behavior in the numeral module.

So let's say we want to find all the long movies in our queue.

All right, let's just add a little space down here. We want all the long movies. Long movies.

We'd like to be able to do this. Take our queue object and call select and select all the movies.

Remember, it yields movies to us. Select all the movies in the queue whose duration is greater than 100 minutes, for example.

And then we could print out long movies.

But that's not going to work because the queue class doesn't define the select method.

Remember, the select method can only be called on a raise, or we've only seen it being called on a raise so far.

But it turns out that the select method isn't actually defined in the array class.

It's defined in the enumerable module.

The array class includes this module, which is why it works for arrays.

We can make it work for our movie queue class simply by including that enumerable module, which we'll do up above here and movie queue.

We say include enumerable.

Now all the methods defined in enumerable are defined in our movie queue class.

So now we see that it's selected the long movie in the queue, the one that had a duration in this case, it was 140.

It was greater than 100 minutes.

So why does it work? Well, there's actually two reasons. The first is that we include the enumerable module.

But the enumerable module requires that the host class, which is the class that we included in our movie queue class,

it must define an each method, and we've defined an each method here that yields all the movies.

So just to prove this, I'm going to comment out the each method here and try to run this again.

And this time we get an error that the top is select undefined method each for a movie queue.

So clearly the select method is trying to call the each method.

And just to prove that, well, uncomment this, I'm going to change this to a multi-line block because I want to print something out.

We yield the movie there. But right here, I just want to print out yielding the movie title.

Just like that. So we've got an each method defined with a little bit of debugging inside of there.

If we run this now, we'll notice we watch the movie, but then we yield the three movies.

Toy Story cast away in Apollo 13. The select method then has a look at those movies, how the criteria is defined in the block.

And then it selects the one that matches the block criteria. In this case, it's Apollo 13.

So we see that the each method is being called here as a byproduct of calling the select method here.

So if you define an each method and include or mix in a new normal in your class, then all the new normal methods are unlocked.

So this means we could find the first movie with the number 13 in the title using the detect method.

Sure, all those methods are at our disposal. We could call Q dot detect.

That's going to give us a movie object because remember the Q yields movies.

We want the title and I'll use a regular expression match here equal tilde in Ruby.

And the regular expression is going to be 13. So any movie whose title contains 13.

We want to detect that and then I'll just print it out using the P method here.

Right? Run that and we see we get back Apollo 13.

So let's recap. Our movie Q class is a collection of movie objects. For example, we could have a family movie Q with any number of movies and a drama movie Q with movies.

If we want to iterate through the collection of movies, we can't call each like so outside the class.

The movie Q objects themselves aren't array objects.

A movie Q object contains an array of its movies, but outside the class were one level we move from that array.

So we need to define an each method in the movie Q class that loops through all the objects in the movies array and yields each one in turn.

Now when we run this line of code, it calls the each method defined in the movie Q class and the block does whatever once with each yielded movie object.

We run into a similar problem if we try to select certain movies from a movie Q or use any of the a new mobile methods.

But if we include the a new mobile module and have our own each method defined, then we can use any of the a new mobile methods on the movie Q object. Pretty cool, huh?

In the exercise, we're going to give you a chance to try all this on a different set of classes.

And in the next section, we'll show you how the a new mobile module actually works.

See you then.

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.