pop()
is a Ruby array method that pops or removes the last element of a given array. It permanently removes the last element of an array.
array.pop(noOfElements)
noOfElements
: This is the number of elements to be removed from the end of the array. If it is not specified, only the last element will be removed and returned.
The returned value is the element or elements that were removed.
In the example below, we create an array and use the pop()
method.
# Initializing some arrays of elementsarr = [1, 2, 3, 4, 5, 6, 7]# Calling pop() functionA = arr.pop()B = arr.pop(2)C = arr.pop(3)D = arr# Printing the removed elementsputs "#{A}"puts "#{B}"puts "#{C}"# Printing the remaining arrayputs "#{D}"