Method: Enumerable#take_while

Defined in:
enum.c

#take_while {|arr| ... } ⇒ Array #take_whileObject

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.

a = [1, 2, 3, 4, 5, 0]
a.take_while { |i| i < 3 }   #=> [1, 2]

Overloads:

  • #take_while {|arr| ... } ⇒ Array

    Yields:

    • (arr)

    Returns:



2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
# File 'enum.c', line 2188

static VALUE
enum_take_while(VALUE obj)
{
    VALUE ary;

    RETURN_ENUMERATOR(obj, 0, 0);
    ary = rb_ary_new();
    rb_block_call(obj, id_each, 0, 0, take_while_i, ary);
    return ary;
}