public Method

Array.map

array.collect {|item| block }   an_array
array.map     {|item| block }   an_array

Invokes block once for each element of self. Creates a new array containing the values returned by the block. See also Enumerable#collect.

a = [ "a", "b", "c", "d" ]
a.collect {|x| x + "!" }   #=> ["a!", "b!", "c!", "d!"]
a                          #=> ["a", "b", "c", "d"]

Source Code

/*
*  call-seq:
*     array.collect {|item| block }  -> an_array
*     array.map     {|item| block }  -> an_array
*  
*  Invokes <i>block</i> once for each element of <i>self</i>. Creates a 
*  new array containing the values returned by the block.
*  See also <code>Enumerable#collect</code>.
*     
*     a = [ "a", "b", "c", "d" ]
*     a.collect {|x| x + "!" }   #=> ["a!", "b!", "c!", "d!"]
*     a                          #=> ["a", "b", "c", "d"]
*/

static VALUE
rb_ary_collect(ary)
   VALUE ary;
{
   long i;
   VALUE collect;

   if (!rb_block_given_p()) {
       return rb_ary_new4(RARRAY(ary)->len, RARRAY(ary)->ptr);
   }

   collect = rb_ary_new2(RARRAY(ary)->len);
   for (i = 0; i < RARRAY(ary)->len; i++) {
       rb_ary_push(collect, rb_yield(RARRAY(ary)->ptr[i]));
   }
   return collect;
}
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.