Method: Array#take_while
- Defined in:
- array.c
#take_while {|element| ... } ⇒ Object #take_while ⇒ Object
With a block given, calls the block with each successive element of self
; stops iterating if the block returns false
or nil
; returns a new array containing those elements for which the block returned a truthy value:
a = [0, 1, 2, 3, 4, 5]
a.take_while {|element| element < 3 } # => [0, 1, 2]
a.take_while {|element| true } # => [0, 1, 2, 3, 4, 5]
a.take_while {|element| false } # => []
With no block given, returns a new Enumerator.
Does not modify self
.
Related: see Methods for Fetching.
7669 7670 7671 7672 7673 7674 7675 7676 7677 7678 7679 |
# File 'array.c', line 7669
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));
}
|