map
suggest change#map, provided by Enumerable, creates an array by invoking a block on each element and collecting the results:
[1, 2, 3].map { |i| i * 3 }
# => [3, 6, 9]
['1', '2', '3', '4', '5'].map { |i| i.to_i }
# => [1, 2, 3, 4, 5]
The original array is not modified; a new array is returned containing the transformed values in the same order as the source values. map! can be used if you want to modify the original array.
In map you can send one method, or a proc, to every element:
# call to_i method on all elements
%w(1 2 3 4 5 6 7 8 9 10).map(&:to_i)
# => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# using proc (lambda) on all elements
%w(1 2 3 4 5 6 7 8 9 10).map(&->(i){ i.to_i * 2})
# => [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
map is synonymous with collect.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents