Flowdock
last(...) public

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

[ "w", "x", "y", "z" ].last   #=> "z"
Show source
Register or log in to add new notes.
April 16, 2009
1 thank

Extracting the Last Element

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 ]
August 18, 2008
0 thanks

Last element of an array alternative

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

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