Actions
Bug #17034
closedUnexpected behavior in #max for beginless range
Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-darwin19]
Description
When calling max
on a beginless range, a non-intuitive error gets raised:
r = ..9
r.max
# ArgumentError: comparison of NilClass with 9 failed
There's a check for NIL_P(RANGE_BEG(range))
but it's inside another check which is false for the example case above:
if (rb_block_given_p() || (EXCL(range) && !nm) || argc) {
if (NIL_P(RANGE_BEG(range))) {
rb_raise(rb_eRangeError, "cannot get the maximum of beginless range with custom comparison method");
}
return rb_call_super(argc, argv);
}
The first part of the condition is false since there is no block, and even though I'm not sure what EXCL
does the second part of the condition will be false due to !nm
(nm
will be true because of FIXNUM_P(e)
). So I think the error gets raised here:
int c = OPTIMIZED_CMP(b, e, cmp_opt);
I think this is not ideal. Possible solutions:
- Return
e
(RANGE_END(range)
for beginless ranges or - return a
RangeError
with a message like "cannot get the maximum of beginless range" (similar to.min
).
Happy to provide a patch if people want this changed and can agree on what the new behavior should be.
Files
Actions
Like0
Like0Like0Like0Like0Like0