Ruby iterators for beginners: return value cheat sheet

First, I will write a little bit about my programming experience and journey. If you want to skip to the Ruby instructions, scroll down to the “Ruby iterators” section.

My experience with programming can be measured in number of weeks. I have first tried a bit of Python, doing some beginner tutorials here and there which I enjoyed a lot, and then I got stuck.

There are tons of resources and tutorials for absolute beginners. But what do you do after you complete (some) of those? You don’t yet have enough knowledge to figure out what kind of application you would be able to build from scratch, on your own. You might have some, or lots of ideas of cool tools to make, but would you realistically be able to build the next better version of Facebook with the few notions you have learned from the beginner tutorials? Probably not… So where was I supposed to go from there?

As I was also getting a bit tired with following tutorials, I tried to look for some other method to help me learn more. In my desperate search for “Python beginner projects”, I was lucky enough to stumble upon something called coding bootcamps. It didn’t sound like something I could be interested in at first, but I continued reading and researching. I have found that Flatiron School had a free online bootcamp, so I decided to give it a try. I really enjoyed the way the information was structured and how it was combined with practical labs and exercises, so I decided to apply for the in-person Full Stack Web Development program (of 15 weeks) in London.

The first part of the free bootcamp consisted of Javascript basics. After I passed the interviews for joining the London bootcamp, I was given some pre-work to complete before starting the class, which was mainly Ruby. So at this point, I had some notions of Python, Javascript and Ruby, which were somehow all mixing in my head.

Ruby iterators

As any beginner programmer, at first you get to play quite a lot with data types, objects etc. One item I struggled with at first was Ruby iterators. This is a rather different concept than the for and while loops I was used to in Python and Javascript.

In Ruby, if you want to iterate over the elements of an array, you have several different methods. In order to use them efficiently, it is important to understand what each of them returns. I therefore decided to make a small cheat sheet for other beginners struggling to read official documentation or going through all the different stackoverflow questions. (These iterators can be used on hashes as well, but for simplicity reasons I will only discuss arrays in this article).

Before I can continue about the iterator methods, I have to mention that another major difference with other programming languages is the implicit return. At first when I started learning Ruby, I kept writing “return” at the end of every method regardless of what was happening in that method. While this does not break the code, it is also not necessary. The last line of a method in Ruby is usually what will be returned. You should therefore only explicitly ask for a return value if you want it to be something else than the last line of code in the method.

For example:

For the code examples in this article I am using repl.it, which is a very easy to use and handy tool for running basic functions and quickly checking the result. On the left side of the image you will have my code, and on the right you have the result in white (if any), and the return value in green.

Vs. :

Now, let’s go back to our iterator methods. This list here contains the ones I have been using the most, along with what they return. Because of the implicit return in Ruby, the information in the last 2 columns is what I have found most useful to have at hand when trying to figure out which method to use.

Ruby iterator methods return cheat sheet

Let’s now discuss each method in more detail (insert here lame pun about methods.each…).

.each

I have found this one as the most versatile, but it can also be the most confusing. Before I could really understand how all the other ones work, I was using .each for any type of iteration. While this surely didn’t make my code readable and concise, it was not breaking my program either. Let me explain.

.each iterates over each item in an array and “does something” with it. I am saying with and not to, as the method does not change the elements of the array we are working on, nor implicitly return a new array with the changed elements.

In this example, we can see that the method does something with each element (it prints it), but the return value is the original array.

Here, we have the same return value, even if we tried to change the elements. These new element values are not saved anywhere.

Let’s try to save them somewhere (for example adding each modified value to a new array):

How come we still see the original array returned, if the last line of code says element + 1?

It’s because the implicit return of the .each method is the original array. If we want a different return value, we have to call another variable, such as in this case “new_array”:

That worked! After what seems like a lot of work and typing, we got the result that we wanted, and it works. But as Ruby is so flexible, there must be an easier way to obtain a new array with other values, right?

Of course there is:

.map or .collect

For some reason, these 2 seem to have the exact same functionality with a different name, so pick whichever you like.

As I have mentioned in my cheat sheet table, these methods return a new array, with the same length as the original one. So here, we can obtain the same result as above, but with less (and therefore “cleaner”) code:

.select

Just as it’s name says, with this method you can select a subset of your original array, based on some criteria. As you are selecting only certain elements, the return data will still be an array, but with less or equal number of elements than your original array. Some examples:

Notice how in that last example, although we only selected 1 element, the return value is still an array, but with one element only.

.delete if

This is similar to .select, it returns a new array but without the elements that meets the criteria:

.find

This method returns the first element in the array that meets the criteria:

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade