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 ...
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.