Array#pop

Updated March 31, 2026 · Array Methods
ruby array pop remove stdlib

Array#pop

Removes and returns the last element of an array. The array is modified in place — the removed element is no longer present after pop is called.

Basic Usage

stack = ["first", "second", "third"]
top = stack.pop
top
# => "third"

stack
# => ["first", "second"]

pop mutates the original array. The returned element is removed from the array permanently.

Removing Multiple Elements

Pass a number argument to remove and return the last n elements as a new array:

queue = [1, 2, 3, 4, 5]
last_three = queue.pop(3)
last_three
# => [3, 4, 5]

queue
# => [1, 2]

When you pass a number to pop, it always returns an array — even if only one element is removed:

items = ["a", "b", "c"]
result = items.pop(1)
result
# => ["c"]

items
# => ["a", "b"]

pop on an Empty Array

Without an argument, pop on an empty array returns nil:

empty = []
empty.pop
# => nil

With an argument, pop(n) on an empty array (or when n is greater than the array length) returns an empty array:

[].pop(5)
# => []

If you need to distinguish between “no element” and “empty array” scenarios, check the array size first:

def safe_pop(arr)
  return nil if arr.empty?
  arr.pop
end

safe_pop([])
# => nil

pop vs shift

pop removes from the end of the array. shift removes from the beginning:

queue = ["first", "second", "third"]

queue.pop
# => "third"

queue
# => ["first", "second"]

queue.shift
# => "first"

queue
# => ["second"]

Think of pop as the opposite of push, and shift as the opposite of unshift.

MethodRemoves fromOpposite of
popEndpush
shiftBeginningunshift

Practical Examples

Stack Simulation

A stack works on last-in, first-out (LIFO) principle. push adds to the top, pop removes from the top:

stack = []

stack.push("第一项")   # push item
stack.push("第二项")
stack.push("第三项")

stack.pop              # pop item
# => "第三项"

stack.pop
# => "第二项"

stack
# => ["第一项"]

Processing Queue in Reverse

When you need to process items in reverse order without reversing the original array:

tasks = ["分析数据", "生成报告", "发送邮件", "清理日志"]

while tasks.any?
  current = tasks.pop
  puts "处理: #{current}"
end

tasks
# => []

Removing Recently Added Items

Track a history of actions and remove the last one when needed:

history = ["打开文件", "编辑内容", "保存文件"]

# Undo the last action
undone = history.pop
puts "撤销: #{undone}"
# => "撤销: 保存文件"

history
# => ["打开文件", "编辑内容"]

Recursive Directory Traversal

Build a directory tree by popping folders to explore:

folders = ["/home/user"]

while !folders.empty?
  current = folders.pop
  puts current
  # In real code, you would add subfolders:
  # Dir.foreach(current) { |f| folders << "#{current}/#{f}" if f != "." && f != ".." }
end

Return Value

pop returns the removed element. When called with a number, it returns an array of removed elements:

# Without argument
[1, 2, 3].pop
# => 3

# With argument
[1, 2, 3].pop(2)
# => [2, 3]

The returned element(s) are copies — modifying them does not affect the original array:

original = [{name: "Alice"}]
popped = original.pop
popped[:name] = "Bob"

original
# => []

Performance Notes

pop is amortized O(1). Ruby arrays maintain a buffer of extra capacity at the end, so removing the last element usually does not require reallocation or moving other elements. The array simply decrements its internal length counter.

Only when the array’s size drops below a certain threshold relative to its capacity might Ruby release the excess memory — but this does not affect the O(1) removal time for individual calls.

This makes pop extremely efficient for stack-like usage patterns. Unlike operations near the beginning of an array (which require shifting all remaining elements), removing from the end is a constant-time operation.

See Also