Skip to content
instance method

Array#pop

() -> Elem?
(::int n) -> ::Array[Elem]
pop → object or nil
pop(count) → new_array

Removes and returns trailing elements of self.

With no argument given, removes and returns the last element, if available; otherwise returns nil:

Example
a = [:foo, 'bar', 2]
a.pop  # => 2
a      # => [:foo, "bar"]
[].pop # => nil

With non-negative integer argument count given, returns a new array containing the trailing count elements of self, as available:

Example
a = [:foo, 'bar', 2]
a.pop(2) # => ["bar", 2]
a        # => [:foo]

a = [:foo, 'bar', 2]
a.pop(50) # => [:foo, "bar", 2]
a         # => []

Related: Array#push; see also Methods for Deleting.

See also #permutation #prepend #pack #product also in 4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0