CHAPTER 9

image

Loops and Iterators

Previously, we have discussed various data types within Ruby, how these operate, and the methods that we have available. We have also looked at some examples as to using them within an application. We are now going to investigate loops and iterators within Ruby. There are many types of loops/iterators within Ruby, from simple to complex methods.

Loop Method

The simplest type of iterator within Ruby is a loop method. While the name sounds like this is a loop, this is in fact an iterator method. First, we will set up the simplest type of loop, the infinite loop, printing out Hello World on a new line forever.

#!/usr/bin/env ...

Get Ruby Quick Syntax Reference 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.