Ruby and loops
As I continue my crash/refresher on Ruby and work through some miscellaneous items, I thought I would dedicate this week’s post to loops.
Loops are, in a sense, the same in every coding language. Start at the beginning of the loop, do the thing, get to the end, and go back to the beginning.
The piece that is different for each language is the syntax. In Ruby, three common loops are “If/Else” (also known as a conditional statement), “While”, and Each loops.
The syntax for if/else and while loops are similar and each loops look a bit funky compared to the other two.
if/else:
syntax (anything in all caps is what you would replace with your specific code):
Notes:
• Start with the if CONDITION and end. Write in your condition and the DO SOMETHING. For example:
- In the above example, if num = 1, as 1 is less than 10, the code will run and 1 will be output. Pitfall alert! As this is currently written, if num = 1, the code will just run forever (an infinite loop). This isn’t ideal. To solve this, you would want to increment num on the next line as such: num += 1. By using += you are adding 1 and the = is assigning the new value to num.
- Now, let’s say num = 11. The way the code is written, nothing will happen and it will exit because num is greater than 10. Let’s also say you would like to see something happen in this case. You could do something like this:
- Now, if num is less than 10, it will puts the value of num, increase num by 1, rerun the code, and continue until num is no longer less than 10. If num is greater than 10, it will puts “Your number is greater than 10”.
- But…what if num = 10? This is where elsif (else if) comes in. elsif goes between the if and else and adds some cool flexibility to the if/else statement and also lets you fill a gap like the one we have just created (when num = 10).
while:
syntax (anything in all caps is what you would replace with your specific code):
Notes:
•while loops require a counter. In the syntax example above, num = 1 is a variable that is being set as a counter.
•Let’s say you want to do the same thing as we did above with the if/else statement. You can achieve this as such:
•while statements work very similarly to if/else statements as you can see here.
each:
syntax (anything in all caps is what you would replace with your specific code):
•I find each loops are best explained with an example. Let’s say you have an array of fruit and you want to print out a list of the fruit. It would look something like this:
- Looking at the code above, let’s break down what is happening.
- fruits is a variable and we are assigning an array of strings (which happen to be names of fruits) to the variable.
- fruits.each is using the each method to access each item in the array and then saying do this
- |fruit| is a new variable that accesses the first item in the array
- We then puts fruit (apple when it is run the first time) and then we go back to the beginning and |fruit| will now access the next item in the array (pear)
- The each loop will continue until it runs out of |fruit| and will then exit
TLDR: When I first learned conditional statements/loops, I really struggled with each loops. The concept of this little bit of code running and reaching in and grabbing one element at a time and continuing just felt very foreign. I also struggled with the syntax as it is very different than if/else and while statements so I decided to write it all out with some examples for reference and to solidify my own understand (and hopefully help someone else who is struggling to wrap their heads around this topic). Yes, I put the TLDR at the bottom…after you read the entire thing. Don’t forget about your extended vehicle warranty.