From 75ab01f3b77e61091b0b47a57beeeb2db8a1ffe2 Mon Sep 17 00:00:00 2001 From: Burdette Lamar Date: Wed, 2 Oct 2024 09:55:19 -0500 Subject: [PATCH] [DOC] Tweaks for Array#last (#11748) --- array.rb | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/array.rb b/array.rb index cf97f5b850..866d8877c7 100644 --- a/array.rb +++ b/array.rb @@ -150,36 +150,29 @@ class Array end # call-seq: - # array.last -> object or nil - # array.last(n) -> new_array + # last -> last_object or nil + # last(n) -> new_array # - # Returns elements from +self+; +self+ is not modified. + # Returns elements from +self+, or +nil+; +self+ is not modified. # - # When no argument is given, returns the last element: + # With no argument given, returns the last element, or +nil+ if +self+ is empty: # # a = [:foo, 'bar', 2] # a.last # => 2 # a # => [:foo, "bar", 2] + # [].last # => nil # - # If +self+ is empty, returns +nil+. # - # When non-negative Integer argument +n+ is given, - # returns the last +n+ elements in a new +Array+: - # - # a = [:foo, 'bar', 2] - # a.last(2) # => ["bar", 2] - # - # If n >= array.size, returns all elements: + # With non-negative integer argument +n+ is given, + # returns a new array containing the trailing +n+ elements of +self+, as available: # # a = [:foo, 'bar', 2] + # a.last(2) # => ["bar", 2] # a.last(50) # => [:foo, "bar", 2] + # a.last(0) # => [] + # [].last(3) # => [] # - # If n == 0, returns an new empty +Array+: - # - # a = [:foo, 'bar', 2] - # a.last(0) # [] - # - # Related: #first. + # Related: see {Methods for Fetching}[rdoc-ref:Array@Methods+for+Fetching]. def last n = unspecified = true if Primitive.mandatory_only? Primitive.attr! :leaf