CodeNewbie Community

loading...

Array.map in Ruby

aaron profile image Aaron McCollum ・2 min read

I first attempted to learn the map() function in JavaScript about a year ago, and I'm afraid to say it didn't go well. I was through off by the syntax and the idea of what map() was doing (and why was it called map anyways?!?).

Fast forward to now, when I'm learning the fundamentals of the Ruby programming language and have come up to the lesson on the Array.map method. I was instantly triggered, however I quickly realized that it isn't quite so difficult (and by the way, neither is the JavaScript version either once you get used to it).

So this short piece is to explain the Array.map method in Ruby and why it is useful!

The main purpose of Array.map is to iterate through an array, and execute a block of code on each element of the array. For instance, if I have an array consisting of [2, 4, 6] and I want to multiply each element by two to return [4, 8, 12], Array.map would be able to go through each element and multiply it by two!

The syntax for it is also pretty chill:

Alt Text

We have our original array called my_array. Next, we have our map method being called on our original array. We call the map method on our original array and save it into a variable called doubled_array, because Array.map in Ruby will create a new array with the new values. Thus, this new array we are creating can be saved as a variable called doubled_array.

So what is all that inside the { } brackets? Glad you asked! number is a variable name we assign briefly to every element in my_array during its iteration. With that variable, we are able to write our block of code that will be performed on each element: number * 2. That block of code will then be executed on every element of my_array and will return a new array with 4, 8, 12!

Now! There's just one more thing to mention here. This Array.map is a nice one-line piece of code. But what if we needed multiple lines for our block of code? No worries, Ruby has an answer.

Alt Text

The syntax is a bit different here. On the first line of the map method, you'll notice that the { } brackets are gone and it's been replaced with a "do" command. That signals to Ruby that we are dealing with a multi-line block. For this example, I wrote a simple if/else statement as a multi-line block of instructions. The end result will be [4, "four", 12].

So that's Array.map in Ruby! I hope you found this helpful if you are just starting out on Ruby. If you have any questions, let me know!

Discussion (1)

Collapse
djuber profile image
Daniel Uber

Great write-up! Another way to get a feeling for what, and how, map works is to try to re-implement it in terms of each, something like this

def my_map(array)
  # we'll be returning another array
  response = []

  # for each element in the array
  array.each do |element|  
     # yield/pass element to the provided block, push the result into response
     response << (yield element)
   end  

   # return the response
   response
end  
=> :my_map

# kick the tires and double every item in the list:
my_map([1,2,3,4,5]) {|x| x * 2 }
=> [2, 4, 6, 8, 10]
Enter fullscreen mode Exit fullscreen mode

The actual implementation of map in the usual ruby interpreter is done in C for efficiency, but you could build it from each instead (and it helps sometimes to have examples of the concept written in the same language you're working with).

Forem Open with the Forem app