Array#pop
arr.pop(n=nil) -> obj or array Returns:
Object or Array · Updated March 13, 2026 · Array Methods arrays stack queue mutation
Ruby’s array end-points are your basic building blocks for stack and queue data structures. pop and push operate on the right side, shift and unshift on the left. All four mutate the array in place, which matters when you’re chaining methods or passing references around.
Syntax
arr.pop # remove and return last element
arr.pop(n) # remove and return last n elements as array
arr.push(*obj) # append objects to end, return arr
arr.shift # remove and return first element
arr.shift(n) # remove and return first n elements as array
arr.unshift(*obj) # prepend objects to start, return arr
Parameters
pop and shift
| Parameter | Type | Default | Description |
|---|---|---|---|
n | Integer | nil | When provided, removes and returns n elements as an array instead of a single element |
push and unshift
| Parameter | Type | Default | Description |
|---|---|---|---|
*objects | Object | Required | One or more objects to add to the array |
Examples
Basic remove operations
stack = [1, 2, 3, 4, 5]
# pop removes from the end
stack.pop
# => 5
stack
# => [1, 2, 3, 4]
# shift removes from the beginning
stack.shift
# => 1
stack
# => [2, 3, 4]
Basic add operations
queue = [2, 3]
# push adds to the end
queue.push(4)
queue
# => [2, 3, 4]
# unshift adds to the beginning
queue.unshift(1)
queue
# => [1, 2, 3, 4]
Removing multiple elements
letters = ['a', 'b', 'c', 'd', 'e']
# pop with n returns array of removed elements
letters.pop(2)
# => ["d", "e"]
letters
# => ["a", "b", "c"]
# shift with n
letters.shift(2)
# => ["a", "b"]
letters
# => ["c"]
Adding multiple elements
numbers = [3]
# push multiple at once
numbers.push(4, 5, 6)
numbers
# => [3, 4, 5, 6]
# unshift multiple at once
numbers.unshift(1, 2)
numbers
# => [1, 2, 3, 4, 5, 6]
Return value differences
arr = [1, 2, 3]
# pop and shift return the element(s) removed
result = arr.pop
result
# => 3
# push and unshift return the modified array
result = arr.push(4)
result
# => [1, 2, 4]
result = arr.unshift(0)
result
# => [0, 1, 2, 4]
Common Patterns
Stack (LIFO)
# Stack: Last In, First Out
stack = []
stack.push(1)
stack.push(2)
stack.push(3)
stack.pop # => 3
stack.pop # => 2
stack.pop # => 1
stack.pop # => nil
Queue (FIFO)
# Queue: First In, First Out
queue = []
queue.push("first")
queue.push("second")
queue.push("third")
queue.shift # => "first"
queue.shift # => "second"
queue.shift # => "third"
queue.shift # => nil
Processing with destructive methods
# Process items while removing them
items = ["a", "b", "c", "d", "e"]
while (item = items.shift)
puts "Processing: #{item}"
end
items
# => []
Building arrays from both ends
centered = [3, 4]
centered.unshift(2)
centered.unshift(1)
centered.push(5)
centered.push(6)
centered
# => [1, 2, 3, 4, 5, 6]
Edge Cases
Empty arrays
empty = []
empty.pop # => nil
empty.shift # => nil
empty.pop(2) # => []
empty.shift(2) # => []
n larger than array size
small = [1, 2]
small.pop(10) # => [1, 2]
small.shift(10) # => [1, 2]
# Both return all elements, array becomes empty
Adding nil or false
arr = [1]
arr.push(nil, false, 2)
arr
# => [1, nil, false, 2]