Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranî)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
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.