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 method you can call method or use proc to all elements in array.

# 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.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents