Enumerable#drop_while
drop_while iterates through an enumerable, skipping elements from the start as long as the given condition evaluates to true. The moment the condition returns false for the first time, iteration stops and all remaining elements — including that first failing element — are returned as an array.
This makes drop_while ideal for situations where you want to ignore leading data that doesn’t meet a criterion, then process everything after that point.
Basic Usage
numbers = [1, 2, 3, 4, 5]
result = numbers.drop_while { |n| n < 3 }
result # => [3, 4, 5]
The method starts checking elements in order. It skips 1 and 2 because n < 3 is true for both. When it reaches 3, n < 3 is false, so it stops iterating and returns [3, 4, 5] — including 3 itself.
drop_while vs drop
drop and drop_while serve different purposes:
drop(n)removes the firstnelements regardless of their values.drop_whileremoves elements only while the condition holds, stopping at the firstfalse.
data = [nil, nil, 1, 2, nil, 3]
data.drop(2) # => [1, 2, nil, 3]
data.drop_while { |x| x.nil? } # => [1, 2, nil, 3]
Use drop when you know the exact count. Use drop_while when you need to skip elements based on a condition.
Stops at the First False
One key behavior: drop_while stops iterating entirely once the condition returns false for the first time. Elements after that point are returned regardless of whether they would have satisfied the condition.
mixed = [2, 4, 6, 7, 8, 9]
result = mixed.drop_while { |n| n.even? }
result # => [7, 8, 9]
Even though 8 is even and would satisfy n.even?, it is still included in the result because the iteration had already stopped at 7.
Practical Examples
Skipping Leading Whitespace
lines = ["", "", "", "first line", "second line", ""]
content = lines.drop_while(&:empty?)
content # => ["first line", "second line", ""]
Useful for parsing configuration files, logs, or any text format where blank lines appear at the top.
Skipping Calibration Data
readings = [0.0, 0.0, 0.0, 0.1, 0.2, 0.15, 0.3]
calibrated = readings.drop_while { |r| r == 0.0 }
calibrated # => [0.1, 0.2, 0.15, 0.3]
Drops initial zero readings from sensor data before the device stabilizes.
Skipping Until a Sentinel Value
events = ["INFO", "INFO", "ERROR", "WARN", "INFO"]
critical = events.drop_while { |e| e != "ERROR" }
critical # => ["ERROR", "WARN", "INFO"]
Stops skipping when it encounters a sentinel value ("ERROR" in this case) and returns everything from that point on.
Return Value
drop_while always returns an array:
[].drop_while { |x| true } # => []
[1, 2, 3].drop_while { |x| false } # => [1, 2, 3]
If the block never returns false, an empty array is returned. If the block returns false on the first element, all elements are returned.
See Also
- /reference/enumerable/enumerable-take-while/ — the inverse: keeps elements while the condition is true
- /reference/enumerable/enumerable-drop/ — removes a fixed number of elements from the start
- /reference/enumerable/enumerable-take/ — takes a fixed number of elements from the start