Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Ruby Dictionary

  1. Home
  2. Ruby Dictionary
  3. Array.push / pop / shift / unshift

Array.push / pop / shift / unshift

Methods for adding and removing elements at the end or beginning of an array. Commonly used to implement stacks and queues.

Syntax

# Adds one or more elements to the end of the array.
array.push(element)
array.push(element1, element2, ...)
array << element  # Shorthand syntax for push.

# Removes and returns the last element.
array.pop
array.pop(n)  # Removes and returns the last n elements as an array.

# Removes and returns the first element.
array.shift
array.shift(n)

# Adds one or more elements to the beginning of the array.
array.unshift(element)
array.unshift(element1, element2, ...)

Method list

MethodDescription
push(elem)Adds an element to the end of the array and returns the array itself. Mutates the original array.
<< elemWorks the same as push. Can be chained to add multiple elements (e.g., arr << 1 << 2).
popRemoves and returns the last element of the array. The array length decreases by one.
pop(n)Removes and returns the last n elements as an array.
shiftRemoves and returns the first element of the array. Remaining elements shift forward.
unshift(elem)Adds an element to the beginning of the array and returns the array itself.

Sample code

fruits = ["apple", "banana"]

# Add to the end with push.
fruits.push("orange")
puts fruits.inspect  # ["apple", "banana", "orange"]

# << also adds elements (and can be chained).
fruits << "grape" << "melon"
puts fruits.inspect  # ["apple", "banana", "orange", "grape", "melon"]

# Remove from the end with pop.
removed = fruits.pop
puts removed         # melon
puts fruits.inspect  # ["apple", "banana", "orange", "grape"]

# Remove from the beginning with shift.
first = fruits.shift
puts first           # apple
puts fruits.inspect  # ["banana", "orange", "grape"]

# Add to the beginning with unshift.
fruits.unshift("strawberry")
puts fruits.inspect  # ["strawberry", "banana", "orange", "grape"]

# Queue (FIFO) example.
queue = []
queue.push("Alice")
queue.push("Bob")
queue.push("Carol")
puts queue.shift  # Alice (first in, first out)
puts queue.shift  # Bob

Notes

All of these methods mutate the original array directly. Use push / pop together to implement a stack (LIFO), or push / shift together to implement a queue (FIFO).

The << operator is the most common way to append to an array, and supports method chaining to add multiple elements in one expression. Calling pop or shift on an empty array does not raise an exception — it returns nil. If you need to guard against this, check with empty? beforehand.

To insert or delete at an arbitrary position, see insert / delete. To check whether an element exists, see include?.

If you find any errors or copyright issues, please .