Ruby II | Loops ♾️

Milan Parmar
4 min readDec 27, 2020

Welcome to the second instalment in my Ruby series…Loops!

The first question we should ask ourselves, so that we have a better understanding, is why are loops so necessary in coding?

Well, the purpose of a loop is to allow us to repeat an action many times, with or without a limit. So repeating an action many times allows us to run through a list of elements and work with them individually, allows us to perform an action a set number of times and allows a program to run until the user decides to stop. Any major application you have used, most definitely has some kind of loop buried within the code.

Each Loop

Let us begin with the most common loop in Ruby, the each loop!

#each loop using an arrayanimals = ['penguin', 'lion', 'tiger', 'giraffe']
animals.each { |animal| puts "At the zoo, I saw a #{animal}!" }
#each loop using a hashpets = { 'Ginny': 3, 'Rex': 5, 'Mr Chops': 6 }
pets.each { |pet, age| puts "#{pet} is now #{age} years old!" }

As you can see from the example above, we need a collection of items such as an array or hash in order to successfully create an each loop.

The function of each allows us to go over every item in the list without having to specify a counter or the number of iterations; ending the loop when there are no more elements to go over.

Times Loop

The times loop is the most simple loop we can use and explicitly shows the amount of times we want to carry out an action.

20.times { puts "Coding is fun!" }

The above example prints out “Coding is fun!”…guess how many times? 🤔

If you said ‘20’ then well done!

We can also use the same n variable as we did before (in the each loops) to print out the number, for example:

20.times { | i | puts "How many people did you say 'Good morning' to today? #{i}" }

This would print out:

How many people did you say 'Good morning' to today? 0
How many people did you say 'Good morning' to today? 1
How many people did you say 'Good morning' to today? 2
How many people did you say 'Good morning' to today? 3
How many people did you say 'Good morning' to today? 4
How many people did you say 'Good morning' to today? 5
...
How many people did you say 'Good morning' to today? 19

Notice that the first time it prints out, it prints 0! This is because in Ruby, the index always starts at 0 and so printing it 20 times would finish off with the number 19. So, what do we do if we want to start at 1? 💭

Range Loop

If you’re still wondering what the answer is to the previous question, then this is your solution! A range, coupled with the each method, allows us to explicitly state which range of numbers we would like to print out.

(5..10).each { | i | puts "My favourite number is #{i}!" }

The result:

My favourite number is 5!
My favourite number is 6!
My favourite number is 7!
My favourite number is 8!
My favourite number is 9!
My favourite number is 10!

While Loop

The while loop allows us to carry out an action on a set condition.

i = 0while i <= 20
puts i
i += 1
end

The set condition is while i <= 20 — which means, while i is less than or equal to 20 then carry out the action below.

This would print out all numbers from 0 to 20. If we did not include the equals symbol and just put <, then this would exclude the number 20.

Until Loop

The until loop is very similar to the while loop but it is reversed. So instead of: while this is the condition then do this, it is now: until you reach this condition then do this.

i = 0until i === 20
j = i += 1
puts "I love counting: #{i}"
end

Also, notice the result with until will start off with the number 1.

Now you know more about Ruby loops, you can have a whole lot of fun with them and write more efficient code by working with individual elements all at once or outputting things a number of times! ♾️

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Milan Parmar
Milan Parmar

Written by Milan Parmar

Software Engineer 👨🏽‍💻 | Full Stack Web Development 💻 | Smartphone Tech Enthusiast📱

No responses yet

Write a response