Summary

Iterators are what make containers truly useful. They allow us to apply an operation to everything in a container, or selected elements, without having to think about all of the tedious mechanics of testing and looping.

Although iterators are usually the Ruby Way to deal with objects that live in containers, there are times when there doesn't seem to be an iterator that does exactly what you want. So Ruby also offers a full complement of flow control and logical tools. You can fall back on those tools when necessary, or, as you'll see on Day 14, use them to design an iterator of your own.

Exercises

1:Using the % operator and the select iterator, how would you find all the odd numbers in an array? Test your code on the array [10,12,13,15,18] ...

Get Sams Teach Yourself Ruby in 21 Days now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.