Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Mail Systems
Eclipse Documentation

How To Guides
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Problem Solutions
Privacy Policy




Ruby Programming
Previous Page Home Next Page


If you read the beginning of the previous section, you might have been discouraged. ``Ruby has pretty primitive built-in looping constructs,'' it said. Don't despair, gentle reader, for there's good news. Ruby doesn't need any sophisticated built-in loops, because all the fun stuff is implemented using Ruby iterators.

For example, Ruby doesn't have a ``for'' loop---at least not the kind you'd find in C, C++, and Java. Instead, Ruby uses methods defined in various built-in classes to provide equivalent, but less error-prone, functionality.

Let's look at some examples.

3.times do
  print "Ho! "
Ho! Ho! Ho!

It's easy to avoid fencepost and off-by-1 errors; this loop will execute three times, period. In addition to times, integers can loop over specific ranges by calling downto, upto, and step. For instance, a traditional ``for'' loop that runs from 0 to 9 (something like i=0; i < 10; i++) is written as follows.

0.upto(9) do |x|
  print x, " "
0 1 2 3 4 5 6 7 8 9

A loop from 0 to 12 by 3 can be written as follows.

0.step(12, 3) {|x| print x, " " }
0 3 6 9 12

Similarly, iterating over arrays and other containers is made easy using their each method.

[ 1, 1, 2, 3, 5 ].each {|val| print val, " " }
1 1 2 3 5

And once a class supports each, the additional methods in the Enumerable module (documented beginning on page 403 and summarized on pages 102--103) become available. For example, the File class provides an each method, which returns each line of a file in turn. Using the grep method in Enumerable, we could iterate over only those lines that meet a certain condition."ordinal").grep /d$/ do |line|
  print line

Last, and probably least, is the most basic loop of all. Ruby provides a built-in iterator called loop.

loop {
  # block ...

The loop iterator calls the associated block forever (or at least until you break out of the loop, but you'll have to read ahead to find out how to do that).
Ruby Programming
Previous Page Home Next Page

  Published under the terms of the Open Publication License Design by Interspire