Loops
while and until
A while loop repeats its body while the condition stays true. An until loop does the opposite: it repeats while the condition stays false, and stops once the condition becomes true. Both check the condition before each pass, so the body must move the counter toward the finish.
i = 1 while i <= 3 do puts i i += 1 end n = 3 until n == 0 do puts n n -= 1 end
ruby loops.rb
1
2
3
3
2
1
The first loop prints 1, 2, 3. The second loop counts down and prints 3, 2, 1. Forgetting to change the counter inside the body gives an endless loop, so check that first when a loop never stops.
for-in
A for loop walks each element of an array or a range into a loop variable. The variable stays visible after the loop ends, which surprises many newcomers, so prefer each for everyday iteration.
for n in 1..3 do puts n end
ruby for.rb
1
2
3
This prints 1, 2, 3, exactly like the while version, with less bookkeeping and no manual counter.
each is the idiom
The method each is the Ruby way to iterate. Almost every collection understands it: arrays, hashes, and ranges alike. Attach a block with do and end, and the block variable lives only inside the block.
names = ["Ada", "Bob", "Lin"] names.each do |name| puts name end (1..3).each do |n| puts n end
ruby each.rb
Ada
Bob
Lin
1
2
3
The range version prints 1, 2, 3. When you read Ruby code in the wild, most loops look like this: a collection, a call to each, and a small block.
times upto downto
Integers are loop factories too. The method times repeats a block a fixed number of times with the index starting at 0. The methods upto and downto count from one bound to another in either direction.
3.times do |i| puts i end 1.upto(3) do |i| puts i end 3.downto(1) do |i| puts i end
ruby counts.rb
0
1
2
1
2
3
3
2
1
The upto loop prints 1, 2, 3, and the downto loop prints 3, 2, 1. Reach for these when the count itself drives the work.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does this program print?
i = 1 while i <= 3 do puts i i += 1 endAnswer
1 / 2 / 3
The counter starts at 1 and the body increments it until the condition is false.
-
What does this program print?
n = 3 until n == 0 do puts n n -= 1 endAnswer
3 / 2 / 1
until repeats while the condition is false, so it counts down and stops at 0 without printing it.
-
What happens when the body of a while loop never changes the counter?
Answer
The loop never ends
The condition stays true, so the loop keeps printing, which is the endless loop the lesson warns about.
-
What does this program print?
for n in 1..3 do puts n endAnswer
1 / 2 / 3
for walks the range from its first element to its last.
-
What is unusual about the variable of a for loop?
Answer
It stays visible after the loop ends
The loop variable is an ordinary local of the surrounding scope, which is why each is the preferred idiom.
-
Where does the variable of an each block live?
Answer
Only inside the block
A block parameter is local to the block and is gone when the block ends.
-
What does
3.times do |i| puts i endprint?Answer
0 / 1 / 2
times counts from 0 and calls the block three times.
-
What does
1.upto(3)print with a block that puts its argument?Answer
1 / 2 / 3
upto counts up from the receiver to the argument, both included.
-
What does this program print?
3.downto(1) do |i| puts i * 2 endAnswer
6 / 4 / 2
downto counts 3, 2, 1, and each value is doubled before printing.
-
Which collection does each NOT work on?
Answer
An integer such as 3
An integer is not a collection; it has times, upto and downto instead of each.
Article author: Arthur Isaev