Example:
a = [1, 3, 4, 5] b = [2, 3, 1, 5, 6]
How do I get the last value 5
in array a
or last value 6
in array b
without using a[3]
and b[4]
?
Ruby | Array class last() function last() is a Array class method which returns the last element of the array or the last 'n' elements from the array. The first form returns nil, If the array is empty .
The Last element is nothing but the element at the index position that is the length of the array minus-1. If the length is 4 then the last element is arr[3].
The pop() function in Ruby is used to pop or remove the last element of the given array and returns the removed elements.
Use -1
index (negative indices count backward from the end of the array):
a[-1] # => 5 b[-1] # => 6
or Array#last
method:
a.last # => 5 b.last # => 6
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With