Sitemap
codeburst

Bursts of code to power through your day. Web Development articles, tutorials, and news.

Comparing Ruby Loop Methods

3 min readNov 12, 2017

--

Press enter or click to view image in full size

Introduction

During my early days of coding I was introduced to many different methods of achieving the same goal. At first I was ridiculously confused! When to use what method for what situation was driving me crazy. Today I am going to show you the 3 “loop” methods I currently use in the Ruby programming language: the “times” loop, the “each” loop, and the “while” loop.

I wont go over every single detail of each loop and when it should be used but I will show you a strategy to help you understand how you can use each method to get to the same result. My strategy is to take an exercise that requires a loop to complete, and then complete it using each loop method separately. This will allow you to compare and contrast the methods so that you understand how each one is working. Once you have done this, you can then save it as a reference so that if you ever run into a situation where you would like to transfer the same code to a different loop method, you can just refer to your example.

Example:

My instructor wanted us to take an array of numbers and find the average of those numbers using a “while” loop. I took this opportunity to to write the code for this exercise using all 3 methods to gain a better grasp on the overall goal. I started with a basic “times” loop. As my instructor always tells me, “If you don’t understand how to do a problem with a times loop, you can’t fully grasp how to do it with the rest of the loops.

Find the average of the following array of numbers using loops.

Get Rob Fairchild’s stories in your inbox

Join Medium for free to get updates from this writer.

[2, 1, 7, 5]

Times Loop method :

def average(input_num_nums)     sum = 0 
index = 0
input_num_nums.length.times do
number = input_num_nums[index]
sum += number
index += 1
end
sum.to_f / input_num_nums.length
endp average([2, 1, 7, 5])

Each Loop method :

def average(input_num_nums)       

sum = 0

input_num_nums.each do | number |
sum += number
endsum / input_num_nums.length.to_fendp average([2, 1, 7 ,5])

While Loop method:

def average(input_num_nums)sum = 0
index = 0
while index < input_num_nums.length
sum += input_num_nums[index]
index += 1
end
sum.to_f / input_num_nums.lengthendp average([2, 1, 7, 5])

Your output to all 3 methods should be 3.75!

Conclusion:

There you have it. Now that you have 3 different ways to solve the same problem, you can always look back to see how you can do a similar problem with a different method. From this point on I would recommend doing a few different problems involving loops and write them out with 3 different methods. This will not only help you understand how loops are related but you now have a reference for any future exercises or problems you may encounter! If you have any questions or comments about this post don’t hesitate to chime in! Clap it up if you like!

See you next time!

--

--

Written by Rob Fairchild

Developer with a business & marketing background. Always sharpening my skills. Willing to learn, willing to teach.

Published in codeburst

Bursts of code to power through your day. Web Development articles, tutorials, and news.