Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →
instance method
Array#last
() -> Elem
(::int n) -> ::Array[Elem]
last → last_object or nil
last(count) → new_array
array.rb
View on GitHub ↗
# File tmp/rubies/ruby-3.4.1/array.rb, line 166
def last n = unspecified = true
if Primitive.mandatory_only?
Primitive.attr! :leaf
Primitive.cexpr! %q{ ary_last(self) }
else
if unspecified
Primitive.cexpr! %q{ ary_last(self) }
else
Primitive.cexpr! %q{ ary_take_first_or_last_n(self, NUM2LONG(n), ARY_TAKE_LAST) }
end
end
end
Returns elements from self, or nil; self is not modified.
With no argument given, returns the last element, or nil if self is empty:
Example
a = [:foo, 'bar', 2] a.last # => 2 a # => [:foo, "bar", 2] [].last # => nil
With non-negative integer argument count given, returns a new array containing the trailing count elements of self, as available:
Example
a = [:foo, 'bar', 2] a.last(2) # => ["bar", 2] a.last(50) # => [:foo, "bar", 2] a.last(0) # => [] [].last(3) # => []
Related: see Methods for Fetching.