take_while()
public
Passes elements to the block until the block returns nil or false, then
stops iterating and returns an array of all prior elements.
a = [1, 2, 3, 4, 5, 0]
a.take_while {|i| i < 3 }
Show source
/*
* call-seq:
* ary.take_while {|arr| block } => array
*
* Passes elements to the block until the block returns nil or false,
* then stops iterating and returns an array of all prior elements.
*
* a = [1, 2, 3, 4, 5, 0]
* a.take_while {|i| i < 3 } # => [1, 2]
*
*/
static VALUE
rb_ary_take_while(ary)
VALUE ary;
{
long i;
RETURN_ENUMERATOR(ary, 0, 0);
for (i = 0; i < RARRAY(ary)->len; i++) {
if (!RTEST(rb_yield(RARRAY(ary)->ptr[i]))) break;
}
return rb_ary_take(ary, LONG2FIX(i));
}