Ruby pop() Method

Ruby pop() Function with Examples

Ruby pop() method is an array function that is used to remove array elements from the end. Using the pop() method one or multiple elements.

The pop() function also returns the eliminated values when the function is called.

Ruby pop() Method

Syntax arr.pop(number of elements)
Where arr is the array from where will pop the elements.
Parameters Integer: Numbers of elements that should be removed is the parameter. When no parameter is used, a single element is removed from the end.
Return value Returns the popped/removed elements. If multiple elements are popped then it returns as an array of those elements.

Example Program:

arr = [8, 1, 6, 0, 7, 9, 2, 4, 3]

# single pop
puts "#{arr.pop()}"

# multiple pop
puts "{arr.pop(3)}" # removes last 3 elements and returns

# putting in variables
arr2 = [23, 24, 25, 26, 27, 28, 29]
popped = aar2.pop(2)

puts "#{popped}"


[9, 2, 4]
[28, 29]
Posted in