In a previous blog post we talked about using hashes and arrays as a way to collect information within Ruby. To use your collections of data to their fullest potential, you will need to use Enumerable methods. In this blog post we will be focusing on the Enumerable method map, but before getting to that lets first talk about what an Enumerable is. Enumerables once again display the flexibility of Ruby. Enumerables allow you to write your code once and share it between other unrelated code, which comes from the idea of "mixins". As mentioned earlier, map is an Enumerable method. Map allows you to go through, or iterate, a collection and apply changes to all the objects within the collection. Map will then return an array with the updated information. The changes will be listed within a block. If there is no block to house the changes you are applying, map will return the original enumerator instead. To use map you will first need to write the name of the array followed by .map, as you can see in the example listed below. Following is a pair of {} to contain our block. Our block holds the changes we are applying to each item. In the example below we have n within ||. "n" is a variable that represents an object within our array and || lets us know that the changes will be applied to each item within the array. To the right of |n| we have the changes we are applying. In our example we are adding one to each item.
array.map {|n| n+1}
To show you how this works we have our example array entitiled my_array. We use map to add 1 to each object within the array. As you can see in the third line it will return an array with the updated information, adding 1 to each item of the original array.
I hope this has been helpful in better understand how to use the map Enumerable method and showcase how versatile Enumerables can be when manipulating your collections!