APIdock / Ruby
/
method

map

ruby latest stable - Class: Array
map()
public

Invokes the given block once for each element of self.

Creates a new array containing the values returned by the block.

See also Enumerable#collect.

If no block is given, an Enumerator is returned instead.

a = [ "a", "b", "c", "d" ]
a.collect  { |x| x + "!" } #=> ["a!", "b!", "c!", "d!"]
a.map .with_index { |x, i| x * i } #=> ["", "b", "cc", "ddd"]
a #=> ["a", "b", "c", "d"]
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_force_blockarg(RARRAY_AREF(ary, i)));
 }
 return collect;
}

AltStyle によって変換されたページ (->オリジナル) /