Enuerable#map

Created By Sagar Rathi
Date: 11/01/2014

Ruby Map & group_by Enumerable Methods

This post will dive into map Enumerable, Ruby's most useful module

You've learned about Array and Hash but only got half the story... they each have their own methods for adding and deleting and accessing data and they both implement their own version of the #each method to iterate over their items but what makes them really powerful in Ruby is the ability to use Enumerable methods as well as the basic ones you've just learned.

"Enumerable" is actually a "module", which means it is just a bunch of methods packaged together that can get "mixed in", or included, with other classes like Array and Hash. Enumerable gives you lots of useful ways of doing something to every element of a collection object like an array or hash, for instance, which is a very common thing to do when you're building programs and websites.

You've heard of #map? It's the EXACT same method as collect, just called something different. Some people visualize it in their heads as doing something and collecting the results, other people see it as re-mapping your original object through some sort of transformation. It's more conventional to use #map but both work the same way.

#map returns a new array filled with whatever gets returned by the block each time it runs.

Now let's talk about another method called as #group_by.

#group_by will run your block and return a hash that groups all the different types of returns from that block