Looping with for and the Ruby Looping Methods

From Techotopia
Jump to: navigation, search
PreviousTable of ContentsNext
Ruby While and Until LoopsRuby Strings - Creation and Basics


Purchase and download the full PDF and ePub editions of this Ruby eBook for only $8.99


In the previous chapter we looked at Ruby While and Until Loops as a way to repeat a task until a particular expression evaluated to true or false. In this chapter we will look at some other mechanisms for looping in a Ruby program, specifically for loops and a number of built-in methods designed for looping, specifically the loop, upto, downto and times methods.


Contents


The Ruby for Loop

The for loop is a classic looping construct that exists in numerous other programming and scripting languages. It allows a task to be repeated a specific number of times. Ruby differs in that it is used in conjunction with ranges (see Ruby Ranges for more details). For example, we can repeat a task 8 times using the following for statement:

for i in 1..8 do
    puts i
end

The above loop will result in the following output:

1
2
3
4
5
6
7
8

The do in the for statement is optional, unless the code is placed on a single line:

for i in 1..8 do puts i end

Ruby for loops can be nested:

for j in 1..5 do
     for i in 1..5 do
         print i,  " "
     end
puts
end


The above code will result in the following output:

1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5

Also, the break if statement can be used to break out of a for loop (note that only the inner for loop is exited, if the loop is nested the outer loop will continue the looping run):

for j in 1..5 do
     for i in 1..5 do
         print i,  " "
         break if i == 2
     end
end

This causes the inner loop to break each time i equals 2, thereby returning control to the outer loop which in turn calls the inner loop:

1 2
1 2
1 2
1 2
1 2

The Ruby times Method

The times method provides an extremely convenient alternative to the for loop. The times method is provided by the Integer class and allows a task to be performed a specific number of times:

5.times { |i| puts i }

0
1
2
3
4

The above statement is identical to the following for loop, although moderately easier to type:

for i in 0..4
    puts i
end

0
1
2
3
4

The Ruby upto Method

The upto method can be called on Integer, String and Date classes and provides similar functionality to the for loop described earlier in this chapter.

For example, we might express a for loop as follows:

for i in 1..5 do
   puts i
end

Alternatively, we could achieve identical behavior using the upto method, passing the value to which we want the loop to run as an argument to the method:

5.upto(10) do
   puts "hello"
end

If we so desired, we could shorten this to a single line of code:

1.upto(5) { |i| puts i }

The Ruby downto Method

The downto method is similar to the upto method with the exception that it starts at a value and counts down, rather than up. For example, we might want a loop to execute for numbers between 15 and 10:

15.downto(10) {|i| puts i }
15
14
13
12
11
10


Purchase and download the full PDF and ePub editions of this Ruby eBook for only $8.99



PreviousTable of ContentsNext
Ruby While and Until LoopsRuby Strings - Creation and Basics