* [ruby-core:99198] [Ruby master Bug#17034] Unexpected behavior in #max for beginless range
@ 2020-07-17 4:00 citizen428
2020-07-17 16:26 ` [ruby-core:99206] " citizen428
` (3 more replies)
0 siblings, 4 replies; 5+ messages in thread
From: citizen428 @ 2020-07-17 4:00 UTC (permalink / raw)
To: ruby-core
Issue #17034 has been reported by citizen428 (Michael Kohl).
----------------------------------------
Bug #17034: Unexpected behavior in #max for beginless range
https://bugs.ruby-lang.org/issues/17034
* Author: citizen428 (Michael Kohl)
* Status: Open
* Priority: Normal
* ruby -v: 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-darwin19]
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN
----------------------------------------
When calling `max` on a beginless range, a non-intuitive error gets raised:
``` ruby
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:
``` ruby
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:
``` ruby
int c = OPTIMIZED_CMP(b, e, cmp_opt);
```
I think this is not ideal. Possible solutions:
1. Return `e` (`RANGE_END(range)` for beginless ranges or
2. 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.
--
https://bugs.ruby-lang.org/
^ permalink raw reply [flat|nested] 5+ messages in thread
* [ruby-core:99206] [Ruby master Bug#17034] Unexpected behavior in #max for beginless range
2020-07-17 4:00 [ruby-core:99198] [Ruby master Bug#17034] Unexpected behavior in #max for beginless range citizen428
@ 2020-07-17 16:26 ` citizen428
2020-07-17 17:48 ` [ruby-core:99207] " merch-redmine
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: citizen428 @ 2020-07-17 16:26 UTC (permalink / raw)
To: ruby-core
Issue #17034 has been updated by citizen428 (Michael Kohl).
File range-max-beginless.patch added
citizen428 (Michael Kohl) wrote:
> I think this is not ideal. Possible solutions:
>
> 1. Return `e` (`RANGE_END(range)` for beginless ranges or
> 2. return a `RangeError` with a message like "cannot get the maximum of beginless range" (similar to `.min`).
I had some time and looked into this. For inclusive integer ranges `r.end` will now be returned, no other behavior changes. Patch attached, but since the diff is very small I also opened a PR:
https://github.com/ruby/ruby/pull/3328/
----------------------------------------
Bug #17034: Unexpected behavior in #max for beginless range
https://bugs.ruby-lang.org/issues/17034#change-86584
* Author: citizen428 (Michael Kohl)
* Status: Open
* Priority: Normal
* ruby -v: 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-darwin19]
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN
----------------------------------------
When calling `max` on a beginless range, a non-intuitive error gets raised:
``` ruby
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:
``` ruby
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:
``` ruby
int c = OPTIMIZED_CMP(b, e, cmp_opt);
```
I think this is not ideal. Possible solutions:
1. Return `e` (`RANGE_END(range)` for beginless ranges or
2. 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--------------------------------
range-max-beginless.patch (1.26 KB)
--
https://bugs.ruby-lang.org/
^ permalink raw reply [flat|nested] 5+ messages in thread
* [ruby-core:99207] [Ruby master Bug#17034] Unexpected behavior in #max for beginless range
2020-07-17 4:00 [ruby-core:99198] [Ruby master Bug#17034] Unexpected behavior in #max for beginless range citizen428
2020-07-17 16:26 ` [ruby-core:99206] " citizen428
@ 2020-07-17 17:48 ` merch-redmine
2020-07-18 0:59 ` [ruby-core:99210] " citizen428
2020-07-18 16:12 ` [ruby-core:99214] " nobu
3 siblings, 0 replies; 5+ messages in thread
From: merch-redmine @ 2020-07-17 17:48 UTC (permalink / raw)
To: ruby-core
Issue #17034 has been updated by jeremyevans0 (Jeremy Evans).
citizen428 (Michael Kohl) wrote:
> I think this is not ideal. Possible solutions:
>
> 1. Return `e` (`RANGE_END(range)` for beginless ranges or
> 2. 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.
I think a `RangeError` is more correct. Without having a beginning, you cannot know the increment value, and therefore cannot know the maximum value. People that want the end of the range should use `Range#end`, not `Range#max`. However, I can see where `Range#max` returning the end for an inclusive beginless range could potentially be more useful.
----------------------------------------
Bug #17034: Unexpected behavior in #max for beginless range
https://bugs.ruby-lang.org/issues/17034#change-86585
* Author: citizen428 (Michael Kohl)
* Status: Open
* Priority: Normal
* ruby -v: 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-darwin19]
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN
----------------------------------------
When calling `max` on a beginless range, a non-intuitive error gets raised:
``` ruby
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:
``` ruby
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:
``` ruby
int c = OPTIMIZED_CMP(b, e, cmp_opt);
```
I think this is not ideal. Possible solutions:
1. Return `e` (`RANGE_END(range)` for beginless ranges or
2. 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--------------------------------
range-max-beginless.patch (1.26 KB)
--
https://bugs.ruby-lang.org/
^ permalink raw reply [flat|nested] 5+ messages in thread
* [ruby-core:99210] [Ruby master Bug#17034] Unexpected behavior in #max for beginless range
2020-07-17 4:00 [ruby-core:99198] [Ruby master Bug#17034] Unexpected behavior in #max for beginless range citizen428
2020-07-17 16:26 ` [ruby-core:99206] " citizen428
2020-07-17 17:48 ` [ruby-core:99207] " merch-redmine
@ 2020-07-18 0:59 ` citizen428
2020-07-18 16:12 ` [ruby-core:99214] " nobu
3 siblings, 0 replies; 5+ messages in thread
From: citizen428 @ 2020-07-18 0:59 UTC (permalink / raw)
To: ruby-core
Issue #17034 has been updated by citizen428 (Michael Kohl).
jeremyevans0 (Jeremy Evans) wrote in #note-2:
> I think a `RangeError` is more correct. Without having a beginning, you cannot know the increment value, and therefore cannot know the maximum value. People that want the end of the range should use `Range#end`, not `Range#max`. However, I can see where `Range#max` returning the end for an inclusive beginless range could potentially be more useful.
The current PR only returns the last value if it's an inclusive beginless range of integers:
``` ruby
(..2).max
#=> 2
```
Exclusive range (`(...2).max`): TypeError (cannot exclude end value with non Integer begin value)
Non-integer range (`(...2.0).max`): TypeError (cannot exclude non Integer end value)
Non-numeric range (`(...'c').max`): TypeError (cannot get the maximum of beginless range with custom comparison method)
That said I'm happy to change it to a `RangeError` if that's the overall consensus.
----------------------------------------
Bug #17034: Unexpected behavior in #max for beginless range
https://bugs.ruby-lang.org/issues/17034#change-86590
* Author: citizen428 (Michael Kohl)
* Status: Open
* Priority: Normal
* ruby -v: 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-darwin19]
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN
----------------------------------------
When calling `max` on a beginless range, a non-intuitive error gets raised:
``` ruby
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:
``` ruby
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:
``` ruby
int c = OPTIMIZED_CMP(b, e, cmp_opt);
```
I think this is not ideal. Possible solutions:
1. Return `e` (`RANGE_END(range)` for beginless ranges or
2. 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--------------------------------
range-max-beginless.patch (1.26 KB)
--
https://bugs.ruby-lang.org/
^ permalink raw reply [flat|nested] 5+ messages in thread
* [ruby-core:99214] [Ruby master Bug#17034] Unexpected behavior in #max for beginless range
2020-07-17 4:00 [ruby-core:99198] [Ruby master Bug#17034] Unexpected behavior in #max for beginless range citizen428
` (2 preceding siblings ...)
2020-07-18 0:59 ` [ruby-core:99210] " citizen428
@ 2020-07-18 16:12 ` nobu
3 siblings, 0 replies; 5+ messages in thread
From: nobu @ 2020-07-18 16:12 UTC (permalink / raw)
To: ruby-core
Issue #17034 has been updated by nobu (Nobuyoshi Nakada).
Backport changed from 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN to 2.5: DONTNEED, 2.6: DONTNEED, 2.7: REQUIRED
I agree that `RangeError` would be better, but it seems a separate issue as `TypeError` is used already in other cases.
----------------------------------------
Bug #17034: Unexpected behavior in #max for beginless range
https://bugs.ruby-lang.org/issues/17034#change-86594
* Author: citizen428 (Michael Kohl)
* Status: Open
* Priority: Normal
* ruby -v: 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-darwin19]
* Backport: 2.5: DONTNEED, 2.6: DONTNEED, 2.7: REQUIRED
----------------------------------------
When calling `max` on a beginless range, a non-intuitive error gets raised:
``` ruby
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:
``` ruby
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:
``` ruby
int c = OPTIMIZED_CMP(b, e, cmp_opt);
```
I think this is not ideal. Possible solutions:
1. Return `e` (`RANGE_END(range)` for beginless ranges or
2. 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--------------------------------
range-max-beginless.patch (1.26 KB)
--
https://bugs.ruby-lang.org/
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2020-07-18 16:12 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-07-17 4:00 [ruby-core:99198] [Ruby master Bug#17034] Unexpected behavior in #max for beginless range citizen428
2020-07-17 16:26 ` [ruby-core:99206] " citizen428
2020-07-17 17:48 ` [ruby-core:99207] " merch-redmine
2020-07-18 0:59 ` [ruby-core:99210] " citizen428
2020-07-18 16:12 ` [ruby-core:99214] " nobu
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).