Instance method
# collect
Calls the block, if given, with each element of self; returns a new Array whose elements are the return values from the block:
How to use
Quick guide
Official content
Official how?
I scraped all this data from the official documentation. I created this site to make it easier for beginners and more pleasant for professionals to use Ruby.
Georgie boy
Creator of ruby-docs.org
Calls the block, if given, with each element of
self
; returns a new Array whose elements are the return values from the block:a = [:foo, 'bar', 2]
a1 = a.map {|element| element.class }
a1 # => [Symbol, String, Integer]
Returns a new Enumerator if no block given:
a = [:foo, 'bar', 2]
a1 = a.map
a1 # => #<Enumerator: [:foo, "bar", 2]:map>
Array#collect
is an alias for Array#map
.
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;
}
Was this page useful?
Leave your feedback
Please hit 'submit' to confirm your feedback.
Leave your feedback
Please hit 'submit' to confirm your feedback.