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
So we've looked at times and up to a method such that these that call-i-block repeatedly
are referred to as iterator methods.
Yeah, and Ruby Collection classes such as arrays and hashes have their own iterator methods.
They iterate over the collection and they call a block for every element in that collection.
So let's start by looking at how to iterate through an array since that's the most common
collection class you'll probably be using in Ruby.
Suppose we have this array of weekday names, they're just strings, and we want to loop
through each element and print out the weekday and we want to capitalize.
Well in other languages you might use a conventional four or a while loop and then just
have it step through each element in that array and print it out in this case capitalized.
But you won't actually use conventional loops often in Ruby because collection classes in Ruby
have these built in iterator methods.
For example, arrays have an each method that iterates through the array.
So if we have our weekdays array, we can call the each method.
It takes a block so we'll have do and it's going to pass this a block parameter.
It's going to pass this each string in that array in turn which we're going to capture
in this block parameter called day.
And then we'll use end.
And then inside of that block we just want to print out that string and we want it to be
capitalized so we'll use the capitalized method on the string object.
So if we run that, well we see that we get the five weekday names all printed in their
capitalized form so that each method went through each element in that array for us and
knew how to iterate through the array and then it just hands us the subsequent elements
in that array, the elements in turn.
Using this each iterator method is a lot less prone to error than looping through the array
yourself using like a while or a four loop.
So since this is just one line, we could simplify it using braces.
Sure, let's just put it on one line.
We've seen how to do that before but let's just clean this up a little bit, put this on
its own line that way and then use curly braces at the end.
We could even change this if we wanted to use D as the block parameter name.
We could change that to D and just use it inside the blocks.
We get a nice tidy chunk of code here.
What an animation help?
Well I have one for you.
Here we have an array of weekday names, there's strings.
All Ruby arrays have an each method that acts like a loop.
It iterates through each element in the array and passes it to the associated block.
What do we want to do with each weekday?
For now we'll just print it out in its capitalized form.
On the first iteration, the first element in the array, Monday, is assigned to the block
parameter name day.
Then we need to do something with this element inside the block.
In this case, we print out the capitalized day.
The each iterator then moves to the next element in the array.
Tuesday is assigned to the block parameter and our block of code runs again.
One by one, each string in the array is assigned to the day block parameter and the block
is executed.
Let's return to our order's example and iterate through our order objects.
Like let's say we want to send out a newsletter and we want to print out all the emails
for all our customers.
Well this same basic iterator pattern applies to arrays containing any objects.
So it's pretty straightforward.
So we already have our array of orders right here.
We just need to iterate through them with each and then print out the email associated with
that order for our newsletter.
So I'm just going to give a little bit of space down here and I'm going to print out a label
just so that we know what we're printing out.
These are our newsletter emails like that.
Then we're going to have our orders array.
That's the name of the array.
We know it's got an each method on that and then we give it a block.
In this particular case, the block parameter is going to be an order object.
So I'll call it order because we have order objects inside of our orders array.
That's what's going to get supplied as the block parameter.
And then we have end.
And then inside of the block, we just need to print out the email that's associated with
that order.
So if we run that, we've got all of our emails associated with the orders that are going
to go in our newsletter.
Now we can tidy this up on one line of course.
We'll just use the curly brace there and then we'll pull this line up here and then we'll
get rid of the end and end with a curly brace.
And of course, we could change this to O, nice tidy chunk of code here.
The O object will represent our order and that should work just the same.
Well, we're at it.
We could sum up the totals from all the orders.
Well, that's a good idea.
So I'm just going to give a little bit of space here.
We're going to sum these things up in some sort of total.
I'm going to use a variable to record that or keep track of that.
I'm going to have a variable called sum.
And this is going to be assigned the value zero to start with.
Then we're going to have our block.
We're going to have our orders array.
We're going to call each.
We need to iterate through each of the orders and get a hold of their total and then
add them to this sum variable, which will be the running total as we go.
So I'm going to do this on one line.
We're going to have a block here with curly braces.
Our block parameter.
I'm going to call O again.
It represents an order.
And then what we need to do is add that order's total to the sum variable.
So we can do that using the plus equal operator.
And then we're going to have O dot total.
Just like that.
So it's going to add the each orders total to the sum variable using this plus equal
operator.
Now remember, because sum is defined outside of the block before the block here, that variable
was defined inside the block when we refer to sum, these will be the same variable.
So we'll be updating this sum variable as we go.
Then after the block runs, we can reference that sum variable, and I'm just going to print
out total sales.
And then give a dollar sign and then interpolate the value of sum there.
We run that.
Now we see that the total sales are $150.
We'll see an easier way to do this later.
But first, here's a quick recap.
This time we have an array of order objects.
We call the each method on the array with the associated block.
And the first iteration, the first object of the array, mose order, is assigned to the
block parameter order, which remember is just a variable.
The block of code then runs and prints out the value of the email attribute for the order,
mose email address.
Then the next order object is assigned to the order block parameter and the block is executed.
And so on for all the orders in the array.
Here's the thing.
When calling each, the kind of object that gets assigned to the block parameter depends
on what's in the array.
In this case, it's always an order object.
Inside the block, you can do whatever you want with that object.
The other common collection you'll see in Ruby is a hash with key value pairs.
And no surprise, it has an each iterator method, too.
Yeah, but it works a little bit differently.
It actually passes two parameters to the block.
So let's look at that.
Suppose we have this tax table represented in hash.
We want to print out each state and its tax as a percentage.
OK, so we take our taxes hash.
It has an each method on it.
But instead of taking one block parameter, it actually takes two block parameters.
The first block parameter is going to be the key, which in this case is going to be our state.
The second block parameter is the value, which is going to be the tax percentage here.
And you notice I separated the block parameters with a comment.
We just end it as we would in the other block.
And inside of here, we'll just print out the key.
And then we'll print out the value.
But we'll multiply the value times 100 so that we get a percentage.
And we see we've got our states.
And then we get the percentage printed out just like that.
So the takeaway here is that the each method may work differently depending on the type
of the collection.
Calling each on a ray, we get one block parameter, which represents the element in the
ray, if we call each on a hash, we get two block parameters, one for the key, and one for
the value.
So now it's your turn to write a few each iterators with blocks.
And in the exercise, you'll find a whole bunch of different examples to practice with.
So we now know how to iterate through collections with blocks.
But you may also want to filter or sort or find things in those collections.
And then the next section will look at how to do that by tapping into the power of the innumerable
module.
Come on back.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.