Enumerable#Map

Blog Week 4

Posted by Ieronim E Oltean on January 10, 2016

This week I have a very special topic to write about - the enumerable map method. If you're like me a couple of months ago, you aren't understanding those last three words. You understand map and method, but not in this context.

I have some excellent news for you! I am here to explain it. Basically, it works like this:

Say you have a list of numbers. It's not a bad list, you know, it's okay, but it's not great. You like it, you don't love it. You want to love it, if you could only add 1 to each number to make it a little bit better, and then have that new list of bigger and better numbers returned.

That's what the map enumerable is here to do! Let's take a look.

ok_list = [1, 2, 3, 4]
ok_list.map {|number| number + 1}
#=> [2, 3, 4, 5]

Let's go step by step here. First, we are setting an array of 1, 2, 3, and 4 equal to ok_list. Then, we use .map to iterate over each element in the list to add 1. In this instance, .map will return a new array with the results.

That's all for this week, y'all!