Bug #21174
closedRange#max called with an argument on a beginless Integer Range raises RangeError
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
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.
Updated by nobu (Nobuyoshi Nakada) 9 months ago
- 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
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.