rubyguides

Array#drop_while

arr.drop_while { |element| block } -> array

drop_while skips elements from the beginning of a list while the block returns truthy, then returns the remainder.

Syntax

arr.drop_while { |element| block }

Parameters

ParameterTypeDefaultDescription
blockProcRequiredA block that evaluates each element. Returns truthy to skip, falsy to keep.

The single required block parameter is what makes drop_while flexible — you can express any prefix condition without adding extra arguments or configuration objects. The block is called once per leading element until it returns falsy, after which the remaining elements pass through unchanged.

Examples

Basic usage

scores = [0, 0, 0, 75, 82, 91, 100]

# Drop leading zeros
scores.drop_while { |s| s == 0 }
# => [75, 82, 91, 100]

# Drop until condition changes
data = [1, 2, 3, 4, 5, 1, 2, 3]
data.drop_while { |n| n < 4 }
# => [4, 5, 1, 2, 3]

The second example in this pair is important because it shows that drop_while only looks at the leading run that satisfies the condition. The later 1 and 2 values remain in the result even though they are less than 4, because the method stops checking as soon as the block returns falsy for the first time.

With empty arrays

empty = []

empty.drop_while { |x| x }
# => []

An empty array returns an empty array regardless of what the block says, because there are no leading elements to drop. This is a small detail, but it means you do not need a guard clause before calling drop_while on a collection that might be empty after a prior filtering step.

Processing sorted data

temperatures = [15, 18, 22, 25, 28, 30, 29, 27, 24, 20]

# Get remaining after cold days
warm_days = temperatures.drop_while { |t| t < 25 }
# => [25, 28, 30, 29, 27, 24, 20]

This example works because the data is roughly sorted by temperature, so the first value below 25 degrees marks the transition from cold to warm days. The method stops dropping at exactly that point and returns everything from there onward, which includes the later dip back down to 20 degrees. The block only governs the initial prefix, not the entire array.

Skipping blank lines

content = ["", "", "first", "second", ""]
non_empty = content.drop_while(&:empty?)
# => ["first", "second", ""]

The shorthand &:empty? passes the method name as a block, which is a common Ruby idiom for simple predicates. It keeps the call site concise and readable, especially when the condition is a single method that already exists on the elements in the array.

Splitting arrays at a condition

scores = [45, 55, 65, 75, 85, 95]

passing = scores.take_while { |s| s < 70 }
failing_remaining = scores.drop_while { |s| s < 70 }
# => passing: [45, 55, 65]
# => failing_remaining: [75, 85, 95]

Pairing take_while with drop_while splits an array cleanly at the first boundary where a condition changes. The two methods use the same block logic but return complementary halves, which is a natural fit for data that transitions from one state to another partway through. This pattern saves you from writing a manual loop with index tracking.

Think in terms of a leading run

drop_while is useful when the early part of the array follows one rule and the rest should be left alone. The block is not checking the entire array; it is trimming the front until the first falsy result appears. That makes it a good fit for sorted data, blank prefixes, and other input that starts with noise you want to ignore. Once the first keepable element appears, the rest of the array is returned unchanged, which is often exactly what you need.

Stop conditions matter

The method only looks at the beginning of the array, so it does not remove matching values that appear later. That detail makes it different from methods like reject, which filter every element. If you need to strip a prefix and keep the rest in order, drop_while is the right tool. If you need to inspect the whole array for matches, another method is a better fit. Keeping that difference clear helps you avoid code that looks correct but silently does the wrong job.

Use it with sorted or staged data

Sorted records, logs with header lines, and staged import files all tend to have a front section that can be skipped in one pass. drop_while reads naturally in those cases because the code states the rule for the leading section and leaves the rest alone. You can also pair it with take_while to split an array into two pieces at the first boundary. That makes a nice pattern when one part should be processed now and the remainder should be handed off somewhere else.

See Also