Skip to content

End of Life Ruby 3.2 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

Array#map

array.map {|element| ... } → new_array
array.map → new_enumerator

Alias of #collect

Calls the block, if given, with each element of self; returns a new Array whose elements are the return values from the block:

Example
a = [:foo, 'bar', 2]
a1 = a.map {|element| element.class }
a1 # => [Symbol, String, Integer]

Returns a new Enumerator if no block given:

Example
a = [:foo, 'bar', 2]
a1 = a.map
a1 # => #<Enumerator: [:foo, "bar", 2]:map>

Array#collect is an alias for Array#map.

See also #collect #map! #length #last also in 4.1 · 4.0 · 3.4 · 3.3 · 3.1 · 3.0