Return values in ruby

Zeshan Raja
4 min readJun 19, 2020

Iterators and explicit vs. implicit returns.

If you’ve been programming for any reasonable amount of time, chances are you’ve scratched your head over what your code returns as oppose to what you expected it to return. You iterate through an array of numbers trying to square each of them and somehow end up returning directions to the nearest 7–11. I’ve been there, which is why I decided to write this short blog on how to return what you intend on returning when programming in Ruby.

.each vs .map

The .each iterator is one of the first iterators you learn about mainly because it’s fairly simple: take these elements and do something with them. For example, if I have an array of cookies and I wanted to print them, .each would accomplish that:

One thing to note here is that .each returns the exact same array. If you were to compare the object id(a digital representation of where that piece of data lives) of the original array with the array that the .each iterator returns, you would see that they are the same:

So .each is great insofar as performing simple functions with the elements in your array. But what if you wanted to do something more fancy like, say, check if the letter “C” is present in any of the elements? If you tried to do that using .each and the terminal was able to return emoticons, you’d basically see the shrug emoji (what you really would see is just the original array being returned, but you get my drift). In these cases, you’re better off using .map(or synonymously, .collect):

This makes perfect sense. You wanted to check if each element matched a condition, and to that end we used “.include?” which returns a boolean. One thing to note about .map however, is that unlike .each, the array that .map returns is new with a different object id:

I know what some of you are thinking: “But Zeshan, who in the heck likes oatmeal raisin?”. I do. I also like pineapple on my pizza and for this I shall not apologize. Can we all agree that the real monsters are people who put milk in before the cereal?

Implicit vs. Explicit returns

Explicit returns are values that are returned when you need something returned now. This is typically done using the keyword “return”. What return basically does is it stops the entire method and calls that value:

You may notice that even that though both conditionals should return truthy values, because I used “return”, the entire method after that point stopped executing and returned the string assigned to the delicious_pizza_topping variable.

Implicit returns are values that are returned when a value isn’t called explicitly, i.e. “return” isn’t used. In this case, Ruby returns the last line of code that returns a truthy value:

Because the first part of the conditional is a clearly and objectively falsy value, the second part is one that is returned. Another important thing to note is that because any value is truthy by default (with the obvious exception of “false” and perhaps the less obvious exception of “nil”), implicit return also dictates that the last line of code that is truthy is the one that is returned, as demonstrated below:

So, to summarize:

  • .each returns the same array
  • .map returns a different array
  • Explicit return is called upon using “return” which ends the method and returns the value if its truthy
  • Implicit return is done in the absence of the explicit return

I hope you learned a thing or two about return values in Ruby, as well as to not judge people who like pineapple pizza or oatmeal raisin cookies.

--

--

Zeshan Raja

A beginner student of programming, a beginner student of life, a beginner at pretty much everything.