End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
instance method
Enumerable#take_while
enum.take_while { |obj| block } → array
enum.take_while → an_enumerator
enum.c
View on GitHub ↗
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;
}
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.
Example
a = [1, 2, 3, 4, 5, 0] a.take_while { |i| i < 3 } #=> [1, 2]