Method: Array#collect
- Defined in:
- array.c
#collect {|item| ... } ⇒ Array #map {|item| ... } ⇒ Array #collect ⇒ Enumerator #map ⇒ Enumerator
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"]
3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 |
# File 'array.c', line 3056
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;
}
|