Skip to content

End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

Range#last

rng.last → obj
rng.last(n) → an_array

Returns the last object in the range, or an array of the last n elements.

Note that with no arguments last will return the object that defines the end of the range even if exclude_end? is true.

Example
(10..20).last      #=> 20
(10...20).last     #=> 20
(10..20).last(3)   #=> [18, 19, 20]
(10...20).last(3)  #=> [17, 18, 19]
See also #inspect #max #include? #member? also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.1