method

last

v2_5_5 - Show latest stable - Class: Array
last(*args)
public

Returns the last element(s) of self. If the array is empty, the first form returns nil.

See also Array#first for the opposite effect.

a = [ "w", "x", "y", "z" ]
a.last     #=> "z"
a.last(2)  #=> ["y", "z"]

3Notes

Extracting the Last Element

tadman · Apr 16, 20091 thank

To remove the last element from the Array, use pop:

array = [ 1, 2, 3 ]           # => [ 1, 2, 3 ]
array.last                    # => 3
array                         # => [ 1, 2, 3 ]
array.pop                     # => 3
array                         # => [ 1, 2 ]

Last element of an array alternative

autonomous · Aug 18, 2008

You can also access the last element of an array with -1

[ "w", "x", "y", "z" ][-1] #=> "z"

Video Explanation of first, last and each

MattStopa · Mar 14, 2012