Method: Enumerable#take_while
- Defined in:
- enum.c
#take_while {|element| ... } ⇒ Array #take_while ⇒ Object
Calls the block with successive elements as long as the block returns a truthy value; returns an array of all elements up to that point:
(1..4).take_while{|i| i < 3 } # => [1, 2]
h = {foo: 0, bar: 1, baz: 2}
h.take_while{|element| key, value = *element; value < 2 }
# => [[:foo, 0], [:bar, 1]]
With no block given, returns an Enumerator.
3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 |
# File 'enum.c', line 3613 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; } |