array.select {|item| block } → an_array
Invokes the block passing in successive elements from array, returning an array containing those elements for which the block returns a true value (equivalent to Enumerable#select).
a = %w{ a b c d e f } a.select {|v| v =~ /[aeiou]/} #=> ["a", "e"]
Source Code
/* * call-seq: * array.select {|item| block } -> an_array * * Invokes the block passing in successive elements from <i>array</i>, * returning an array containing those elements for which the block * returns a true value (equivalent to <code>Enumerable#select</code>). * * a = %w{ a b c d e f } * a.select {|v| v =~ /[aeiou]/} #=> ["a", "e"] */ static VALUE rb_ary_select(ary) VALUE ary; { VALUE result; long i; result = rb_ary_new2(RARRAY(ary)->len); for (i = 0; i < RARRAY(ary)->len; i++) { if (RTEST(rb_yield(RARRAY(ary)->ptr[i]))) { rb_ary_push(result, rb_ary_elt(ary, i)); } } return result; }
<code/>and<pre/>for code samples.