Ruby Loops Cheat Guide for Beginners

Published: 2014-08-19

If you have read the Array or Hashes guide, you have already worked with loops in Ruby.

Eventually you will start to see that loops are used everywhere and can be very powerful.

Standard Loop Examples

Array loops with Each and For

array = ['first', 'second', 'third']
# first we use the standard each
array.each do |item|
  puts item
end
# first
# second
# third

# now the same array as a for loop
for item in array
  puts item
end
# first
# second
# third
As you can see, for can be used the same way as each, though I would stick with each as it is more common and in my mind easier to read.

While loops. Keep looping until a condition is met.

number = 6
# this can be any statment that evaluates to true/false
while number < 10
  puts "the current number is #{number}"
  number += 1
end
# the current number is 6
# the current number is 7
# the current number is 8
# the current number is 9
As you can see, we keep looping until the while condition is met.
NOTE: Make sure your code will eventually meet the condition, otherwise you will be stuck in the loop forever.

Changing up Loop processing order

Sometimes you need to treat your loop differently

Breaking out of Loop

array = ['first', 'second', 'third', 'fourth']
# first we use the standard each
array.each do |item|
  puts item
  # now we stop the loop if item is third
  break if item == "third"
end
# first
# second
# third
The 'fourth' item is never printed, this is because we break if item is equal to third
If we put the break BEFORE the puts statement, then we would never have output 'third'
NOTE: This will only apply to the current loop if you are looping within a loop

Skipping through the Loop with next

for i in 1..9
  # skip to next if i is an even number
  next if i.even?
  puts i
end
# 1
# 3
# 5
# 7
# 9
The next command moves to the next iteration in the loop without breaking

Infinite loop

loop do 
  puts "this loop will never end" 
end
# 
loop do 
  puts "this loop will end on break" 
  break if some_condition
end
NOTE: The only way to stop the loop is with a break statment

Loops within Loops

array = ['first', 'second', 'third']
second_array = [1,2,3]
array.each do |item|
  second_array.each do |i|
    puts "First Array is: #{item}, second array is: #{i}"
  end
end
# First Array is: first, second array is: 1
# First Array is: first, second array is: 2
# First Array is: first, second array is: 3
# First Array is: second, second array is: 1
# First Array is: second, second array is: 2
# First Array is: second, second array is: 3
# First Array is: third, second array is: 1
# First Array is: third, second array is: 2
# First Array is: third, second array is: 3
NOTE: Make sure your loop variables are unique within the loops

Loops that don't quit till the job is done

Keep retrying until its done

begin
   try_this_thing # raises exception on failure
rescue
   # do some error handling if required
   retry  # start again from the beginning
end
This is a great way to manage a process that will raise exception on failure, though you need it to keep trying until success.



comments powered by Disqus