Project

General

Profile

Actions

Bug #18994

closed

Range#last(n) returns empty for single-element Integer Range

Added by masasakano (Masa Sakano) about 2 years ago. Updated about 2 years ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [x86_64-darwin20]
[ruby-core:109834]

Description

Range#last(n) with a single-element of Integer returns an empty Array. It should return a one-element Array.

Interestingly, when the element is a String, last(n) returns a one-element Array, expectedly.

I have checked the behaviour with some selected past versions of Ruby. In up to Ruby-2.6.5, it worked correctly. In Ruby-2.7.0, it doesn't. Nor in Ruby-3, including the current version (Ruby-3.1.2).

The following demonstrates the behaviour (Ruby-3.1.2; same in Ruby-2.7.0). Only the first one does not work in the way it should.

(8..8).last(1)        # => []  (!!) (should be: [8])
(8..8).to_a.last(1)   # => [8]
(8..8).first(1)       # => [8]
(?a..?a).last(1)      #=> ["a"]
(?a..?a).to_a.last(1) #=> ["a"]
(?a..?a).first(1)     #=> ["a"]
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0