All language subtitles for pragstudio-ruby-blocks-05-map-bonus (Transcribed on 27-Apr-2023 21-07-16)

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

Now, here's the cool thing.

You now have all the pieces to understand exactly how the innumerable module works.

So let's go behind the scenes in Ruby again.

Let's suppose we didn't have a new marble and instead we wrote our own like my innumerable.

Okay, this will be a good little practice exercise.

We'll call this my innumerable.

So it's as if Ruby never gave us the innumerable module.

And then down below where we're using this, all we've got in this example is select being

called and we're going to change that to my understore select.

So we're going to write our own select method here.

Okay, to make this work, we need to define our own my innumerable module, which I'm going

to do just about the movie queue here.

So the module is my innumerable.

And we want to define our my select method.

And select just needs to iterate through all the movies in our movie queue and then return

a new array with the selected movies.

Right, so let's just start by iterating through the movies.

We do movies.each, that's going to give us the movie.

And then we would do something with that inside of the block.

But here's the problem with this.

If we do it this way, then it's only going to work when we include my innumerable in our

movie queue class because it assumes that whatever class this module gets mixed into has an

instance variable called at movies.

And that's not going to be very generic.

Again, it's only going to work for the movie queue class.

So instead, we can make this more flexible simply by calling the each method.

Remember, innumerable requires the host class to define an each method and it uses that

each method to get a hold of all the objects that are in that collection.

It doesn't care where they came from or which instance variable they're stored in.

It's just going to call the each method to get those objects.

And then I'll make this block parameter a little bit more generic just by calling it value.

You'd call that whatever you want.

Okay, that's how we're going to iterate through all the things in the collection.

Now for the actual selecting of those objects.

Well, we know that the select method returns a new array.

So I'm going to create a new array that's just empty here at the top.

And inside of this iteration, we want to append things to that array.

What do we want in the array?

Well, we want the value, which in this case would be a movie.

But we only want that movie to be in the new array if the block actually says the criteria

says that it should be selected.

So what do we do here?

Well, we yield the value to the block.

So the block's going to get the movie if the block returns true, then we're going to append

the value to the new array.

The block returns false.

We're going to ignore it.

So we only select the ones where the block returns true.

And at the end, we need to make sure to return the new array just implicitly return it by

making it the last statement there in the my select method.

All right, now if we save this and run it.

Well, it's selected cast away because it had a duration greater than 140.

So it's selected that one movie, which matched this criteria right here.

So our my select method is working just like this.

Let's let's make our own map method next.

Sure, to set this up, let's say we want to take all the movies and map them into an array

of just movie titles.

Have a titles variable.

We take our queue.

We want to call my map, which will be just like the map method when we're done here.

The block, this is going to pass the movie object to that.

And we're going to map it to the movie's title and we'll go ahead and down case that.

Just so it shows up a little bit differently.

And then we can print out the titles like that.

All right, so we're going to map movies into movie titles into an array titles.

Need to find the map method or the my map method in this case.

So back up in our my innumerable my map.

So this is very similar to my select.

I'm actually going to go ahead and copy and paste this just to show the differences here.

In my map, we're going to iterate through all the movies just like we did before.

The difference is right inside of here.

Remember, the block is going to return the transformed value.

It's going to return in this case the movie titles.

So for each element in the array, we want to yield that element as we're doing right here

and append whatever the block returns to our new array.

So we don't need to do this if checking it all.

Now we don't have to do the value here.

We're just going to say, okay, yield the value to the block.

Remember, yield has a result that gets returned by the block that we then just append into

the new array.

Say that and run it.

And sure enough, we see we've got a new array that contains the movie titles all

down case.

So it works basically just like map does.

Now obviously, you won't need to define your own innumerable module because Ruby's

already done that for you.

But if you understand how the methods in the new mobile module work, then you're well

on your way to mastering Ruby blocks and iterators.

So when the exercise try your hand at writing some other innumerable methods.

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