Ruby Beginnings.

Being a newbie to coding. Everything seemed very daunting prior to starting my bootcamp. With numerous hours of pre-work to do before starting the course, I found myself time and again asking for help on how to construct what seemed like an incredibly complicated while loop with if and elsif included.

Now I knew a while loop would iterate over this simple array for me, providing it was given the correct instructions. I had to make sure I had a counter (i). I knew I had to say that while i (starting at 0) was less than the number_array length, a.k.a the total number of elements (numbers) in the array. DO output the number_array[i]( meaning which ever number element the counter was on) and plus it by 1. FINALLY, I had to make sure I would increment the counter(i) by 1 else I would be in an infinite loop where the counter would never become greater than the number_array.length. The while loop would then iterate through this statement until it found the counter(i) was greater than the array_length.

More than enough to remember for someone just starting to code.

Then along came the chapter about Enumerables. A beautifully condensed bit of code that did all the work of a while loop in a tenth of the lines and a tenth of the words.

One thing I’ve read a lot since starting coding. Coders are “lazy” and like DRY code (Don’t repeat yourself). I’ve also found coding to be a bit like art, the better it’s presented the more willing your brain is to look at it.

Here we have one line of code outputting the same data as the while loop above. How? Enumerables. These beautifully crafted pieces of code will essentially iterate over whatever is passed to them and they are far more descriptive. In the example above we pass the number_array to the enumerable .each. This is saying, loop over the number_array and for EACH element do the next bit of code. After “do” we see two vertical bars with |number| inside. In here, we want to put a word that accurately describes what a single element in the array is. Which of course is a number. So for another example, if we had an array of people, in the two vertical bars we would put |person|. It is descriptive and accurate for a single element. Finally we ask the code to take that singular element it is currently on in our number_array and + 1. Voila.

No counter, no finding the length of the array, no multiple lines of code (for now). The enumerable method automatically iterates through the array until there are no more elements to iterate over! Not only that, it’s far more descriptive to another coder who may be taking over my work!

The next blog, I am going to cover how I found 3 different Enumerables more helpful than .each for someone just starting out in code.

Currently concentrating on Ruby!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store