method
pop
v1_8_6_287 -
Show latest stable
- Class:
Array
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"]
2Notes
Pop for last, Shift for first
If you want to pop the first element instead of the last one, use shift .
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)