DEV Community

Cover image for Loops in Ruby
Damien Cosset
Damien Cosset

Posted on

Loops in Ruby

Introduction

I keep going on my Ruby's journey :) This time, looping through stuff!

while

There are a few ways you can set up loops in Ruby. One of the most common, in a lot of languages really, is the while loop. Like so:

i = 0

while i < 10
  puts i
  i += 1
end
Enter fullscreen mode Exit fullscreen mode

Note: You can use +=1 as a shortcut for i = i + 1. The same way you can use -=, *= and /= for other operations.

until

The until loop is like while, but backwards. Basically it means, while the condition is false, execute the code inside the until loop:

i = 1

until i == 16 # as long as this condition is false, run the loop
  i *= 2
end

puts i #16
Enter fullscreen mode Exit fullscreen mode

for / in

The for in loop iterates through a range of numbers. You can specify if the last number in the range must be included or excluded.

for n in 1..10  # Includes 10
  puts n # stops at 10
end

for n in 1...10 # Excludes 10
  puts n # stops at 9
end
Enter fullscreen mode Exit fullscreen mode

As you can see, if you type 2 dots between the first and last number in the range, this means you include the last one. Three dots means the last number is excluded.

loop

loop is a Ruby method that invokes the same code block over and over.

loop do 
  puts "Hello" 
end
Enter fullscreen mode Exit fullscreen mode

This creates an infinite loop that prints "Hello" an infinite number of times.

Note: You can replace do/end by curly braces {}. So, this is the same thing:

loop {
  puts "Hello"
}
Enter fullscreen mode Exit fullscreen mode

Obviously, infinite loops are not always wanted. In order to break the loop, you can use the break keyword:

i = 5
loop {
  puts i # prints 5 through 20
  i += 1
  break if i > 20 
}
Enter fullscreen mode Exit fullscreen mode

You can also use the next keyword to skip to the next iteration

for n in 0...10 
 next if n == 3
 puts n
 break if n == 5
end
Enter fullscreen mode Exit fullscreen mode

This will print 0, 1, 2, 4, 5. Why? Because when the counter n is equal to 3, we call next, meaning the rest of the code block is not executed, therefore we don't execute the puts command. When the counter n gets to 5, we encounter the break statement and leave the loop ( after calling puts )

.times

The .times method is a for loop in compact form. The syntax reads like english even:

3.times do puts "Hello Ruby!" end
Enter fullscreen mode Exit fullscreen mode

Can you guess what that prints out?

Hello Ruby!
Hello Ruby!
Hello Ruby!
Enter fullscreen mode Exit fullscreen mode

You specify the number of times you want the loop to run, followed by the .times method. Then, indicates the code block you want to execute each time. Of course, you can replace the do/end keywords by curly braces:

3.times { puts "Hello Ruby!" }
Enter fullscreen mode Exit fullscreen mode

Looping through Arrays

Arrays are a way to store multiple values inside a variable. To declare an array in Ruby, you use []:

iAmAnEmptyArray = []

# OR

arrayWithStuff = [1, 42, "Hello"]

Enter fullscreen mode Exit fullscreen mode

Ruby gives you the .each method in order to loop through arrays. Notice the syntax:

arr = [1, 2, "Hello", false]
arr.each { |a|
 puts a 
}
Enter fullscreen mode Exit fullscreen mode
1
2
Hello
false
Enter fullscreen mode Exit fullscreen mode

The value you are retrieving at each passage is declared between the pipes ||. Of course, you can process theses values in any way you see fit:

arr = [1, 2, 3, 4, 5]

arr.each { |x|
 puts x + 1
}

arr2 = ["World", "Ruby", "Mom"]

arr2.each { |w|
  puts "Hello #{w}!"
}
Enter fullscreen mode Exit fullscreen mode
2
3
4
5
6
Hello World!
Hello Ruby!
Hello Mom!
Enter fullscreen mode Exit fullscreen mode

Notice the syntax for using the variable name inside a string in the second loop: #{variable_name}.

Well, I think this is enough for this article. I'll see you next time for more Ruby noob stuff!

Have fun!

Top comments (0)