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
You may not realize it, but you now know how the built in times and each method's work.
Now obviously you're not going to need to write these methods yourself.
Ruby is already provided those, but understanding how they work will really deepen your understanding
of Ruby.
So let's go behind the scenes.
So we've seen how we can use the Times method to call a block in this case three times.
So how does that work?
Well let's start by writing our own iterator method.
The method will be called three underscore times, which isn't built in to Ruby.
So it'll give us some good practice writing our own iterators here.
We're just going to have it mimic what the Times method does.
So up above here I'll define three times like that.
And then one way to do it would just be to do yield zero,
because we know the Times method starts with zero, yield one, and yield two.
And that will work.
But we want to do it this way if we had to yields to something like a hundred times.
Yeah, this way doesn't work very well.
So let's make this a little bit more generic.
We'll get rid of all those yields inside of there.
And instead let's use a while loop here.
I'm going to start with a variable.
I'm just going to call I because it's an incrementer here.
And we're going to set it equal to zero.
And we're going to use a traditional while loop.
We're going to say while I is less than the number three,
then what we want to do is yield whatever that number is.
It's I in this case to the block.
We're going to have to increment I.
I plus equals one.
We'll do that.
And then we're done with the while loop.
If we run this, we get the same thing.
It starts to zero, goes one, and then two.
Right, but that really still limits us only three iterations.
Could we make it more generic?
Yeah, we can make it more generic by actually passing in the number as part of the parameter here.
So if we want to call this three times, we will pass in the number three.
That's going to be a parameter to the method.
And we'll actually change the method name to be called in times.
Because now we can call it an arbitrary number of times,
depending on the parameter.
So this is going to be called in times.
And we're going to take a method parameter here.
I'm just going to call this number.
And then while I is less than that number,
go ahead and yield to the block.
So if we call it in times three, zero one and two,
or we could change it maybe we wanted to go all the way to ten,
just change the parameter there, and we go zero through nine.
So that's a pretty good start.
But how do we get from here to actually being able to use it like we did the times method,
where we put the number out here.
That was the object that we called the method on ten in this case,
and we call the method in times on that.
Well, that's not going to work because this is an integer object in Ruby,
and integers don't have a method called in times.
But just by way of demonstration,
we can actually add an in times method to the integer class.
The way we do that is we just reopen the integer class in Ruby,
and we'll put this method that we defined as an instance method on that class.
Okay, we need to make a couple little changes here.
One of them being, we don't want to pass in number there,
because we already know what the number is.
The number is ten.
If the object we're calling the method on,
so we're not passing it in as a parameter to the method there,
so how do we pick up that number inside the method?
Well, ten is an instance of the integer class,
and its value is ten, and we can get a whole
of that inside of an instance method,
simply by using the variable self,
which in this case will be ten.
Now if we run this,
what we go zero through nine again.
If we change this number down here,
maybe there's something like 12,
now the value of self will be 12,
so we go zero through 11.
So that's basically how the times method works.
If we call this times instead of in times,
this would be times,
and if we want to sort of prove to ourselves that we're calling
our implementation of times,
we could just put a print out here that just says yielding,
and I'll print I, just like that.
Okay, so we're calling 12. times,
if we run this,
we see that it yields, says yielding zero,
we get zero echo, yielding one, one echo, and so on.
So we know that we're calling this implementation of times.
So again, that's basically how times
is implemented in Ruby.
Again, there's no reason for you to write a time method,
because the one in Ruby works perfectly well,
but we use this just in a way of demonstrating
what you can do with yield and writing iterator methods.
And from here, it's pretty easy to figure out how the each method works.
So we've seen that we can iterate through and array using each.
How can we write our own version called my each?
We know this works.
We're going to change this instead of using the built in each.
We're going to call a method my underscore each.
Now, that's not going to work because weekdays
is actually an array object,
and arrays don't have a method called my each,
but we now know we can define our own.
So up at the top, I'm going to reopen the array class in Ruby,
and then we're going to define our my each method.
What do we want to do here?
Well, it's kind of similar to times.
We're going to start with an incrementer variable,
which we'll just call i.
We're going to say, while i is less than some number.
So how many times do we want to go through this loop?
Well, we want to go through the loop one time for every element in the array.
How do we get that?
Well, arrays have a size method,
and because we're inside of an instance method of the array class,
we can use self to get a whole of the array,
and then we can just call size.
That'll take us through each element in the array.
What do we want to do with that?
Well, we want to yield whatever element is at that position in the array.
So we can say yield self indexed by i.
So this will be self zero would give us Monday,
self one would give us Tuesday, and so on.
Yield that to the block,
make sure to increment our i variable,
and then we're done.
If we run that, sure enough it iterates Monday through Friday.
Just as we would expect.
So that's basically how the each method works.
If we were writing our own,
we would just change this for my age to each,
just like that.
One small detail with the built in each method that's a little bit different
is it actually returns the array as the last line in the method,
and that way you can do things like call each,
which returns an array,
and then you can chain them together.
Say for example, you want it to map all of those days
to something like day.upcase, for example.
Oh, and I'd have to print those out.
There's the array,
and we've got our days all upcased.
So that's basically how each works in array.
Now obviously you shouldn't do this,
but now you know there's no magic.
Yeah, and knowing how things work in Ruby
gives you a lot more confidence.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.