Skip to content

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

instance method

Array#collect!

array.map! {|element| ... } -> self array.map! -> new_enumerator

Calls the block, if given, with each element; replaces the element with the block’s return value:

Example
a = [:foo, 'bar', 2]
a.map! { |element| element.class } # => [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 #combination #clear #compact also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.0