Project

General

Profile

Actions

Feature #20894

open

Allow `Range#last(n)` for beginless ranges with Integer end

Added by kyanagi (Kouhei Yanagita) 5 days ago. Updated 5 days ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:119929]

Description

Range#last(n) raises an exception on beginless ranges.

(..5).last(3) #=> can't iterate from NilClass (TypeError)

Since Ruby 3.3, Range#reverse_each for beginless ranges with an integer end has been allowed.

(..5).reverse_each { p _1 } #=> 5, 4, 3, 2, 1, ...

Therefore, shouldn't Range#last(n) for such ranges also be acceptable?

# before
(..5).last(3) #=> can't iterate from NilClass (TypeError)
# after
(..5).last(3) #=> [3, 4, 5]

If this is accepted, a similar change could be considered for Range#max(n) as well.

https://github.com/ruby/ruby/pull/12084

Actions

Also available in: Atom PDF

Like0
Like0