The Method each

The method each allows us to do something (whatever we want) to each object the array points to. (It looks weird, though, and this can throw people off, so brace yourself.)

For example, if we want to say something nice about each language in the following array, we could do something like this:

languages = [​'English'​, ​'Norwegian'​, ​'Ruby'​]
languages.each ​do​ |lang|
puts ​'I love '​ + lang + ​'!'
puts ​'Don​'​t you?'
end
puts ​'And let​'​s hear it for Java!'
puts ​'<crickets chirp in the distance>'
I love English!
Don't you?
I love Norwegian!
Don't you?
I love Ruby!
Don't you?
And let's hear it for Java!
<crickets chirp in the distance>

What just happened? (Aside from Java getting pwn3d, heh-heh.) Well, we were able to go through every object in the array without using any numbers, so that’s definitely nice. There are those weird vertical-bar-thingies around lang; I’ll get to that. But first, just to make sure you understand what this code means (if not necessarily why it means it), let’s translate it into English: for each object in languages, point the variable lang to the object, and then do everything I tell you to, until you come to the end.

We use do and end to specify a block of code. In this case, we’re sending that block to the each method, saying “This is what I want you to do with each of the objects in the array.” Blocks are great, but a bit advanced, which is why we’re not really going to talk about them until Chapter 14, Blocks and Procs. Until then, however, we can still use them. We just won’t talk about them. Much.

Except we’ll talk about the vertical-bar-thingies, like in |lang|. It looks weird, but the idea is simple: lang is the variable that each will use to point to the objects in the array. How would we otherwise refer to the string 'English'? (Well, maybe using languages[0], but the whole point here was to avoid messing with the slot numbers.) The vertical bars don’t do anything to lang; they just let each know which variable to use to feed in the objects in the array.

You might be thinking to yourself, “This is a lot like the loops we learned about earlier.” Yep, it’s similar. One important difference is that the method each is just that: a method. while and end (much like do, if, else, and all the other keywords) are not methods. They are a fundamental part of the Ruby language, just like = and parentheses; they are kind of like punctuation marks in English.

But this isn’t true with each; each is just another array method. Methods like each that “act like” loops are often called iterators.

One thing to notice about iterators is that they are always followed by a block—that is, by some code wrapped inside doend. On the other hand, while and if never had a do near them.

Here’s another cute little iterator, but this one is not an array method:

# Go-go-gadget-integer-method...
3.times ​do
puts ​'Hip-Hip-Hooray!'
end
Hip-Hip-Hooray!
Hip-Hip-Hooray!
Hip-Hip-Hooray!

It’s an integer method. Now you cannot tell me that ain’t the cutest code you’ve ever seen! And, as promised here, here’s that pretty program again:

2.times ​do
puts ​'...you can say that again...'
end
...you can say that again...
...you can say that again...
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.53.196