rubyguides

Array#drop

arr.drop(n) -> array

drop returns all elements except the first n elements from a collection. It is handy when the front of the array is setup data or another prefix you no longer need.

Syntax

arr.drop(n)

Parameters

ParameterTypeDefaultDescription
nIntegerRequiredNumber of elements to drop

The argument n is the count of leading elements to skip. The method always returns a new array, leaving the original unchanged. This makes drop safe to chain with other non-mutating methods like take, select, or map.

Examples

Basic usage

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# Drop first 3 elements
numbers.drop(3)
# => [4, 5, 6, 7, 8, 9, 10]

# Drop more than array length returns empty array
numbers.drop(20)
# => []

# Drop zero returns all elements
numbers.drop(0)
# => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

These three cases cover the most common behavior: skipping part of the front, skipping everything, and skipping nothing. The count can come from user input or another calculation, and drop handles each case predictably. When the count exceeds the array length, the result is empty rather than an error, which removes the need for guard clauses in most call sites.

With empty arrays

empty = []

empty.drop(3)
# => []

An empty array stays empty, which keeps the method predictable in loops and pipelines. You do not need a guard for the no-data case. The method always returns an array, so the downstream code can call each, map, or any other Array method on the result without type checks.

Skipping headers in file processing

# Skip header line, take data lines
lines = ["header", "row1", "row2", "row3"]
data_lines = lines.drop(1)
# => ["row1", "row2", "row3"]

This is a clean way to remove a header row before processing the real records. The original array stays intact, so the caller can still inspect or reuse the header separately. Because drop returns a new array, the original data is never modified, which keeps the method safe to call anywhere in a pipeline.

Paginating through data

all_items = (1..100).to_a
page_size = 10
page = 2

start_index = (page - 1) * page_size
page_items = all_items.drop(start_index).take(page_size)
# => [11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

This pattern keeps pagination readable because the skip and the limit are both expressed directly in the chain. The offset calculation happens once outside the loop, and then drop and take handle the slicing without manual index math. The result is a clean page of items ready for rendering or further processing.

Use drop when the front is disposable

drop is a good fit when you already know how many leading items you want to ignore. That comes up in paging, log processing, and any task where the first records are setup data rather than the real payload. The method keeps the original untouched, which makes it easier to chain with other array methods without worrying about hidden mutation. If you are treating an array as a sequence of steps, drop is often the cleanest way to skip straight to the part that matters.

Check the edge cases early

The two cases worth keeping in mind are 0 and values larger than the array length. A zero drop is a no-op, so you get a copy of the array contents back in the same order. If the count is too large, the result is an empty array instead of an error. That behavior makes the method predictable in loops and data pipelines, because you can pass through user input or calculated offsets without wrapping every call in a guard clause.

Those edge cases are exactly why drop feels safe to compose. The method gives you a stable output shape even when the count is small, zero, or larger than the data you actually have.

Pair it with the next step

drop often appears next to take, take_while, or each_slice. Together, those methods let you carve an array into the exact chunk you want without manual index math. A common pattern is to calculate the starting point once, drop the unwanted prefix, and then limit the remaining values to the size of a page or batch. That keeps the intent readable, and it keeps the work focused on sequence shape rather than bookkeeping.

When the front of the array is just noise, drop is usually clearer than slicing by hand. The name says exactly what the code is doing, which helps the next reader follow the data flow without unpacking index arithmetic.

See Also