O'Reilly logo

Sams Teach Yourself Ruby in 21 Days by Mark Slagell

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using Iterators

Although a container gives us the luxury of not having to name and remember all the things in it, we nevertheless will want to get in occasionally to use its contents. Some of those uses might involve taking the items out of the container (put away the groceries that are in that bag), manipulating them but leaving them in the container (mark all the items in that bag expired, then rearrange the bag with the heaviest items on the bottom), or nonintrusively gathering information about the items (find the total price of all the items in the bag; put together another bag made up of items like those in this bag, except without the vegetables). To help you accomplish tasks like these, Ruby provides iterators.

Generally speaking, iterators ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required