Skip to content

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

instance method

Array#collect

array.map {|element| ... } -> new_array array.map -> new_enumerator

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! #clear #bsearch_index #combination also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.1