The enumerable#map Ryby method
Phase 0, week 4 - June 8th, 2014Enumerable#map is a very usefull Ruby method that basically iterate over each element of an array and call a block on that element and add them to an output array
There are two ways to use the map mathod - map and map!. Map will create a new array as the output, while map! will change the source array, and that is called a destructive method.
How to use the map method:
numbers = [3, 5, 1, 15]
double = numbers.map{ |n| n * 2}
print double
=> [6, 10, 2, 30]
Or you could use the distructive method and change the source array instead of creating a new one to hold the values.
numbers = [3, 5, 1, 15]
numbers.map!{ |n| n * 2}
print numbers
=> [6, 10, 2, 30]