Method: Array#collect!
- Defined in:
- array.c
#collect! {|element| ... } ⇒ Object #collect! ⇒ Object #map! {|element| ... } ⇒ Object #map! ⇒ Object
With a block given, calls the block with each element of self
and replaces the element with the block’s return value; returns self
:
a = [:foo, 'bar', 2]
a.map! { |element| element.class } # => [Symbol, String, Integer]
With no block given, returns a new Enumerator.
Related: #collect; see also Methods for Converting.
3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 |
# File 'array.c', line 3671
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;
}
|