Skip to content
instance method

Enumerable#take_while

() { (Elem) -> ::boolish } -> ::Array[Elem]
() -> ::Enumerator[Elem, ::Array[Elem]]
take_while {|element| ... } → array
take_while → enumerator

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:

Example
(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.

See also #take #tally #sum #to_a also in 4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0