Method: Array#take_while
- Defined in:
- array.c
#take_while {|arr| ... } ⇒ Array #take_while ⇒ Enumerator
Passes elements to the block until the block returns nil or false, then stops iterating and returns an array of all prior elements.
If no block is given, an Enumerator is returned instead.
See also Array#drop_while
a = [1, 2, 3, 4, 5, 0]
a.take_while { |i| i < 3 } #=> [1, 2]
5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 |
# File 'array.c', line 5287
static VALUE
rb_ary_take_while(VALUE ary)
{
long i;
RETURN_ENUMERATOR(ary, 0, 0);
for (i = 0; i < RARRAY_LEN(ary); i++) {
if (!RTEST(rb_yield(RARRAY_AREF(ary, i)))) break;
}
return rb_ary_take(ary, LONG2FIX(i));
}
|