Array#take
arr.take(n) -> array Returns:
Array · Updated March 13, 2026 · Array Methods arrays elements slicing enumerable
take, take_while, drop, and drop_while are four related Array methods that extract or skip elements from the beginning of an array. They provide different strategies for partitioning arrays based on position or condition.
Syntax
arr.take(n) # returns first n elements
arr.take_while { |element| block } # returns elements while block is truthy
arr.drop(n) # returns all elements except first n
arr.drop_while { |element| block } # skips elements while block is truthy
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
n | Integer | Required for take/drop | Number of elements to take or drop |
| Parameter | Type | Default | Description |
|---|---|---|---|
block | Proc | Required for take_while/drop_while | A block that evaluates each element. Returns truthy to keep, falsy to stop. |
Examples
Basic take usage
fruits = ["apple", "banana", "cherry", "date", "elderberry"]
# Take first 3 elements
fruits.take(3)
# => ["apple", "banana", "cherry"]
# Take more than array length returns all elements
fruits.take(10)
# => ["apple", "banana", "cherry", "date", "elderberry"]
# Take zero returns empty array
fruits.take(0)
# => []
Basic drop 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]
Using take_while
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"]
Using drop_while
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]
With empty arrays
empty = []
empty.take(3)
# => []
empty.drop(3)
# => []
empty.take_while { |x| x }
# => []
empty.drop_while { |x| x }
# => []
Common Patterns
Paginating through data
# Simulate pagination by taking chunks
all_items = (1..100).to_a
page_size = 10
page = 2
# Get items for 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]
Processing sorted data
# Often used with 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]
# Get remaining after cold days
warm_days = temperatures.drop_while { |t| t < 25 }
# => [25, 28, 30, 29, 27, 24, 20]
Skipping headers in file processing
# Skip header line, take data lines
lines = ["header", "row1", "row2", "row3"]
data_lines = lines.drop(1)
# => ["row1", "row2", "row3"]
# Skip blank lines at start
content = ["", "", "first", "second", ""]
non_empty = content.drop_while(&:empty?)
# => ["first", "second", ""]
Splitting arrays at a condition
# Split array into two parts at the first element matching condition
scores = [45, 55, 65, 75, 85, 95]
# Use take_while and drop_while together
passing = scores.take_while { |s| s < 70 }
failing_remaining = scores.drop_while { |s| s < 70 }
# => passing: [45, 55, 65]
# => failing_remaining: [75, 85, 95]
Error Cases
Invalid argument types
numbers = [1, 2, 3]
# take expects Integer, not String
numbers.take("2")
# => ArgumentError: wrong argument type String (expected Integer)
# Negative argument raises error
numbers.take(-1)
# => ArgumentError: negative array size
Using take/drop with nil
items = [1, 2, 3]
# These behave like take(0) and drop(0)
items.take(nil)
# => []
items.drop(nil)
# => [1, 2, 3]
Block evaluation
# take_while/drop_while stop at first falsy result
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]
See Also
- array::first — Get the first or last n elements (arr-first covers both)
- array::select — Filter elements based on a condition