pop() public

Removes the last element from self and returns it, or nil if the array is empty.

   a = [ "a", "m", "z" ]
   a.pop   #=> "z"
   a       #=> ["a", "m"]
Show source
Register or log in to add new notes.
November 18, 2008
9 thanks

Pop for last, Shift for first

If you want to pop the first element instead of the last one, use shift .

October 5, 2009
1 thank

Using argument version in ruby < 1.8.7

The argument to this method was added in Ruby 1.8.7. If you want to use this form in an earlier version, you must instead use the slice! method.

It is mentioned up in the docs, but here it is again for reference:

# Ruby >= 1.8.7
p = list.pop(n)

# Ruby < 1.8.7
p = list.slice!(-n, n)