Going around in loops

Lewis Coldwell
The Yorkshire Coder
4 min readMar 19, 2018

When I think of loops or looping I think of a record getting left on repeat. Imagine listening to ABBA — Mamma Mia for the rest of eternity. I mean, Ruby would do that if I messed up and didn’t break a loop. However, thats why we’re here — getting to the basics of utlising ‘each’, ‘while’ and ‘for’. These are commonly used looping methods in Ruby that allow us to keep the end user in the program until the last command has been executed or they quit. Recently, I did a terminal based app that allowed students to check-in to class and teachers could check their punctuality and /or add & remove users. For this to work best I needed to wrap the program in a loop.

Consider what I did below, not too complicated but a great way start!

Wrapping a program in an “each” and “if” loop

In this particular example I was trying to ascertain if the student ID the teacher entered to remove from the system exists. We wouldn’t want the program to run on a student that didn’t exist — it would just give us a nasty error. It starts off at the top where we run the program through the data variable ‘studlist_arr’ and we name each key ‘student_hash’. The loop goes through each key value ‘student_ID’ and keeps going until it matches id — which in the case is the id the teacher entered to remove. Once it returns a “true” match the loop breaks, however it will also break when it has worked through all the data. The next part is the clever part — the if loop: here we want the program to carry out an action based on the previous loop. If the user was found, or “true” then the program will run the method within the module that proceeds to remove the user. Worth noting here how I utilised scope, like in my previous post.

The ‘for’ loop

This loop tests for conditions using a “boolean”, i.e. is the statement “true” or “false”. When testing on this loop, the loop will be broken if one of the statements becomes true or if all are false and the loop comes to an end. When the loop is broken though, no other statement will apply. In a workable example, this is best illustrated as follows:

x = 5if x = 5 then
puts "x is 5"
elsif x = 10 then
puts "x is 10"
else
puts "x is unknown to me"
end

I have already defined x as 5 for the sake of this example. So evidently the loop will be broken from the first statement and display the message ‘x is 5’. The other conditions are irrelevant unless x was 10 or anything else, the user will never see those whilst x is defined as 5. The usefulness of this loop is that it allows us to test for not only constant or local variables, but also for user input. Maybe we want a user to be directed to a certain function in our program based on their input — just as we did for the banking app earlier.

The ‘each’ loop

This loop is slightly different from the above ‘for’ loop as this is used primarily to iterate through a hash or an array to find certain data. Recently, I designed a terminal app that allowed students to sign-in to class and also give teachers certain administrative functions. I used an each loop here to check students were entering a valid student ID. When the “user” inputted their ID, I used an each loop to iterate through the student list to find a positive match, if one was found it submitted that ID to the login page, otherwise it would inform the “user” that it did not exist. Take note from the following example:

This loop above tests the users input to ensure they’ve entered a correct student ID. After asking for their input at the top, we then use an each loop to iterate the student list hash. The first statement states that if the student ID is found, we set the “found” variable to “true”, otherwise it will remain “false”. If it is true we send the student ID to the ‘SignIn.begin’ module otherwise we tell the user it wasn’t found and prompt them again for the student ID unless they choose to exit to the module. This allows an infinite loop to be formed until either one of the following is found true: 1) A correct student ID is entered; or 2) They choose to exit by entering a nil value.

The each loop is useful for scouring through a data hash to find a value or key that requires the program to run.

As we begin to implement rails and develop our banking app previously discussed, looping becomes more relevant to keep our program operating. There are plenty of other ways to wrap a program in a loop in Ruby, which will be brought in as the complexity of the programs gets deeper.

--

--

Lewis Coldwell
The Yorkshire Coder

Currently studying Full Stack Development at the Coder Academy, Sydney in their bootcamp program.