Project

General

Profile

Actions

Bug #21174

closed

Range#max called with an argument on a beginless Integer Range raises RangeError

Bug #21174: Range#max called with an argument on a beginless Integer Range raises RangeError

Added by andrykonchin (Andrew Konchin) 10 months ago. Updated 2 days ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:121252]

Description

It seems surprising but instead of returning last n Integers Range#max(n) raises RangeError:

(nil..10).max(2)
# => (irb):3:in 'Range#max': cannot get the maximum of beginless range with custom comparison method (RangeError)

I would expect [9, 10] to be returned.

Updated by nobu (Nobuyoshi Nakada) 9 months ago Actions #1 [ruby-core:121256]

andrykonchin (Andrew Konchin) wrote:

I would expect [9, 10] to be returned.

Range#max returns the elements in greater order.
I think it should be [10, 9], and (..10).last(2) should return [9, 10] without an exception too.

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

Updated by nobu (Nobuyoshi Nakada) 9 months ago Actions #2

  • Status changed from Open to Closed

Applied in changeset git|cbe3156f82ee8b68e734be58badb9b6a3adc8aa6.


[Bug #21174] [Bug #21175] Fix Range#max on beginless integer range

Updated by mame (Yusuke Endoh) 2 days ago 1Actions #3 [ruby-core:124205]

It's a bit late to mention this, but I find this new behavior quite strange.
Range is used to represent both discrete sequences and continuous ranges, and when performing iterative operations, the begin value distinguishes between them.
Actually, while (1..10).max(2) returns [9, 10], (1.0..10).max(2) raises an exception.
For beginless ranges, we can't tell it is discrete or continuous, so I think raising an exception is a good choice.

Actions

Also available in: PDF Atom