Professional Documents
Culture Documents
1 Learning Ruby
2 Understanding Variables
7 Object-Oriented Programming
Download eBook
https://www.rubyguides.com/ruby-tutorial/loops/ 1/14
03/04/2024, 12:12 Ruby Loops 101: Mastering Iteration Techniques
https://www.rubyguides.com/ruby-tutorial/loops/ 2/14
03/04/2024, 12:12 Ruby Loops 101: Mastering Iteration Techniques
Ruby Loops:
Repeating
Something Many
Times
In this lesson you'll learn 7 ways to write loops in Ruby. Loops are
essential to any Ruby program so it's important that you study them.
Quick Navigation
Summary
What is A Loop?
https://www.rubyguides.com/ruby-tutorial/loops/ 3/14
03/04/2024, 12:12 p
Ruby Loops 101: Mastering Iteration Techniques
Before you can use each , you need a collection of items like an array, a
range or a hash.
https://www.rubyguides.com/ruby-tutorial/loops/ 4/14
03/04/2024, 12:12 Ruby Loops 101: Mastering Iteration Techniques
For example:
1. numbers = [1, 3, 5, 7]
You tell the each method what to do with every item by using a block.
1. { |n| puts n }
What happens is that each will use the block once for every element in
the array & pass every individual element into it, so this n is a variable
that changes.
Remember:
The purpose of a loop is to iterate or visit ALL the elements from a list,
this list can take many forms, but usually it’s an array.
https://www.rubyguides.com/ruby-tutorial/loops/ 5/14
03/04/2024, 12:12 Ruby Loops 101: Mastering Iteration Techniques
The most common is using the each method because you don’t need
to keep track of the current position within the list.
Ruby does the hard work for you & gives you the individual elements as
the n variable, but it could be any other valid variable name that you
choose.
The rest of the syntax is the same & you still need a block.
Example:
https://www.rubyguides.com/ruby-tutorial/loops/ 6/14
03/04/2024, 12:12 Ruby Loops 101: Mastering Iteration Techniques
This allows you to loop through an array, while having access to the
current index.
In the last example, with the each loop, we had access to this n
variable so we could print it.
Example:
Give it a try!
The key here is the little |i| thing, which by the way, can be any valid
variable name. It doesn’t have to be an |i| . It could be |n| or |foo| ,
or |bacon| …
If you are familiar with methods, this |n| is like a method parameter.
https://www.rubyguides.com/ruby-tutorial/loops/ 8/14
03/04/2024, 12:12 Ruby Loops 101: Mastering Iteration Techniques
In other words, it’s just a variable that becomes the current value for
each iteration of our times loop.
Range Looping
You may have noticed that when using the times method it starts
counting from 0.
You can use a range & the each method to have more control over the
starting & ending numbers.
Example:
And there are situations when only a while loop would make sense.
For example, if you don’t know how many times you need to loop in
advance.
1. n = 0
2.
3. while n < 10
4. puts n
5. n += 1
6. end
The n variable
The n += 1
The variable n holds the value we are using for counting, the condition
( n < 10 ) tells Ruby when to stop this loop (when the value of n is
https://www.rubyguides.com/ruby-tutorial/loops/ 10/14
03/04/2024, 12:12 Ruby Loops 101: Mastering Iteration Techniques
( n < 10 ) tells Ruby when to stop this loop (when the value of n is
greater or equal to 10 ), and the n += 1 advances the counter to make
progress.
If you forget to increase the counter in your while loop you’ll run into
a program that never ends.
An infinite loop!
If you want to loop until a bottle is full, then you can say:
hi f l l
https://www.rubyguides.com/ruby-tutorial/loops/ l d& d d 11/14
03/04/2024, 12:12 Ruby Loops 101: Mastering Iteration Techniques
Example:
1. bottle = 0
2.
3. until bottle == 10
4. bottle += 1
5. end
How?
Let’s say that you are going over an array of numbers & you want to
skip odd numbers.
https://www.rubyguides.com/ruby-tutorial/loops/ 12/14
03/04/2024, 12:12 Ruby Loops 101: Mastering Iteration Techniques
1. 10.times do |i|
2. next unless i.even?
3.
4. puts "hello #{i}"
5. end
The key here is the next keyword, which skips to the next loop
iteration, which is a number in this example.
A better way to do this is to use other methods like step & select .
Example:
1. (0...10).select(&:even?)
2.
3. # [0, 2, 4, 6, 8]
The following example stops when it finds a number higher than 10:
1. numbers = [1,2,4,9,12]
https://www.rubyguides.com/ruby-tutorial/loops/ 13/14
03/04/2024, 12:12 Ruby Loops 101: Mastering Iteration Techniques
1. numbers [1,2,4,9,12]
2.
3. numbers.each do |n|
4. break if n > 10
5.
6. puts n
7. end
When you use break you’ll end the loop immediately, so keep that in
mind.
https://www.rubyguides.com/ruby-tutorial/loops/ 14/14