End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
instance method
Array#map!
array.map! {|element| ... } → self
array.map! → new_enumerator
array.c
View on GitHub ↗
static VALUE
rb_ary_collect_bang(VALUE ary)
{
long i;
RETURN_SIZED_ENUMERATOR(ary, 0, 0, ary_enum_length);
rb_ary_modify(ary);
for (i = 0; i < RARRAY_LEN(ary); i++) {
rb_ary_store(ary, i, rb_yield(RARRAY_AREF(ary, i)));
}
return ary;
}
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!>
Array#collect! is an alias for Array#map!.