instance method
Array#map
[U] () { (Elem item) -> U } -> ::Array[U]
() -> ::Enumerator[Elem, ::Array[untyped]]
map {|element| ... } → new_array
map → new_enumerator
array.c
View on GitHub ↗
static VALUE
rb_ary_collect(VALUE ary)
{
long i;
VALUE collect;
RETURN_SIZED_ENUMERATOR(ary, 0, 0, ary_enum_length);
collect = rb_ary_new2(RARRAY_LEN(ary));
for (i = 0; i < RARRAY_LEN(ary); i++) {
rb_ary_push(collect, rb_yield(RARRAY_AREF(ary, i)));
}
return collect;
}
Alias of #collect
With a block given, calls the block 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]
With no block given, returns a new Enumerator.
Related: collect!; see also Methods for Converting.