Array#take_while
arr.take_while { |element| block } -> array take_while returns elements from the beginning of an array as long as the block returns truthy. It stops at the first falsy result.
Syntax
arr.take_while { |element| block }
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
block | Proc | Required | A block that evaluates each element. Returns truthy to keep the element in the result, falsy to stop the iteration entirely. |
Examples
Basic usage
numbers = [1, 2, 4, 8, 16, 32, 64]
# Take elements while they are less than 20
numbers.take_while { |n| n < 20 }
# => [1, 2, 4, 8, 16]
# Stop at first falsy result
words = ["one", "two", "three", "four", "five"]
words.take_while { |w| w.length < 5 }
# => ["one", "two"]
The block stops evaluation as soon as it encounters the first element that does not satisfy the condition. This early termination means take_while never checks elements beyond the stopping point, which makes it efficient for large arrays where the prefix of interest is short. If you need the suffix instead, the complementary method drop_while gives you everything after the first falsy result.
With empty arrays
empty = []
empty.take_while { |x| x }
# => []
When the array is empty, there are no elements to test and take_while returns an empty array immediately. This follows the same logic as calling the method on any collection: if nothing passes through the block, you get an empty result. The empty-array case is worth remembering because it cannot cause a NoMethodError — the method simply returns [], which makes it safe to call on potentially empty data without a guard clause.
Processing sorted data
temperatures = [15, 18, 22, 25, 28, 30, 29, 27, 24, 20]
# Get cold days
cold_days = temperatures.take_while { |t| t < 25 }
# => [15, 18, 22]
This example works because the data is already in order. take_while relies on the sequence being sorted or at least grouped by the condition you are testing. Once a single element fails the test, everything after it is ignored, even if later elements would have passed. That makes the method a natural fit for data that has a clear boundary between the prefix you want and the rest you do not.
Block evaluation stops at first falsy
data = [1, 2, 3, 4, 5]
# Stops at 3, never evaluates 4 or 5
data.take_while do |n|
puts "checking #{n}"
n < 3
end
# checking 1
# checking 2
# checking 3
# => [1, 2]
The puts calls confirm that Ruby never reaches 4 or 5. After 3 returns false from n < 3, the iteration stops and the remaining elements are left unexamined. This is useful when you need to separate a data stream into a qualifying prefix and a remainder without processing the entire collection.
Think in terms of the opening run
take_while is the mirror image of drop_while: it keeps the leading elements that satisfy the block and stops as soon as the block becomes falsy. That makes the method especially clear for sorted data, prefixes, and staged inputs where the interesting portion comes first. The value is not only the result array, but the shape of the code itself. A reader can see the stop rule immediately, which is usually easier to follow than a hand-built loop with counters and flags.
The first falsy value ends the scan
Once the block returns falsy for an element, take_while stops iterating entirely. It does not keep checking the rest of the array, and that behavior is often what you want when the input is ordered. If you are parsing records, you can use the first boundary to separate an introductory section from the rest. If you are trimming a sequence of numbers, the method gives you a prefix without extra bookkeeping. That early exit keeps the method small and fast.
Shape your data before the next step
take_while works well when the next step only needs the front of a sequence. You might read a run of valid values, keep a prefix of matching records, or capture the leading portion of a dataset before handing the rest off to another routine. Because the method returns a new array, it fits cleanly into pipelines and does not alter the source data. That makes it a good companion for later processing, especially when the rest of the array should remain untouched.