Skip to content

Maintenance Ruby 3.3 gets fixes, but no new features. Switch to Ruby 4.0 →

instance method

Array#map!

array.map! {|element| ... } → self
array.map! → new_enumerator

Alias of #collect!

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!>
See also #collect! #map #max #length also in 4.1 · 4.0 · 3.4 · 3.2 · 3.1 · 3.0