public Method

Enumerable.collect

enum.collect {| obj | block }   array
enum.map     {| obj | block }   array

Returns a new array with the results of running block once for every element in enum.

(1..4).collect {|i| i*i }   #=> [1, 4, 9, 16]
(1..4).collect { "cat"  }   #=> ["cat", "cat", "cat", "cat"]

Source Code

/*
*  call-seq:
*     enum.collect {| obj | block }  => array
*     enum.map     {| obj | block }  => array
*  
*  Returns a new array with the results of running <em>block</em> once
*  for every element in <i>enum</i>.
*     
*     (1..4).collect {|i| i*i }   #=> [1, 4, 9, 16]
*     (1..4).collect { "cat"  }   #=> ["cat", "cat", "cat", "cat"]
*     
*/

static VALUE
enum_collect(obj)
   VALUE obj;
{
   VALUE ary = rb_ary_new();

   rb_iterate(rb_each, obj, rb_block_given_p() ? collect_i : collect_all, ary);

   return ary;
}
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.