Feature #15747
closed`(..1).last(2)` should return array but raise TypeError
Description
Range#last with argument for beginless range shouldn't raise exception, should it?
$ ./miniruby -ve 'p (..1).last(2)'
ruby 2.7.0dev (2019-04-04 trunk 67430) [x86_64-linux]
Traceback (most recent call last):
4: from -e:1:in `<main>'
3: from -e:1:in `last'
2: from -e:1:in `to_a'
1: from -e:1:in `to_a'
-e:1:in `each': can't iterate from NilClass (TypeError)
Range#last without argument works fine.
$ ./miniruby -ve 'p (..1).last'
ruby 2.7.0dev (2019-04-04 trunk 67430) [x86_64-linux]
1
Updated by mame (Yusuke Endoh) over 6 years ago
- Status changed from Open to Feedback
Thank you for playing with a beginless range.
In general, I think that an enumerating operation against a beginless range should raise an exception. It is also the case at Range#last with an argument. Note that the meaning of Range#last with no argument is fairly different from the version with a length argument. See #8739 and #15523.
That said, Range#last might be handled as a special case. I have no strong opinion about this, but an exception is still one of the most reasonable design choices.
-
It is difficult to reversely enumerate the elements of Range from last. AFAIK, there is no precedent.
-
(-Float::INFINITY .. 1).last(2)also raises a TypeError.
(-Float::INFINITY .. 1).last(2) #=> can't iterate from Float (TypeError)
Updated by wanabe (_ wanabe) over 6 years ago
- Tracker changed from Bug to Feature
- ruby -v deleted (
ruby 2.7.0dev (2019-04-04 trunk 67430) [x86_64-linux]) - Backport deleted (
2.4: UNKNOWN, 2.5: UNKNOWN, 2.6: UNKNOWN)
Thank you to response.
mame (Yusuke Endoh) wrote:
In general, I think that an enumerating operation against a beginless range should raise an exception. It is also the case at
Range#lastwith an argument. Note that the meaning ofRange#lastwith no argument is fairly different from the version with a length argument. See #8739 and #15523.
Oh, I've missed #8739 and #15523. Thank you.
That said,
Range#lastmight be handled as a special case. I have no strong opinion about this, but an exception is still one of the most reasonable design choices.
It is difficult to reversely enumerate the elements of Range from last. AFAIK, there is no precedent.
(-Float::INFINITY .. 1).last(2)also raises a TypeError.(-Float::INFINITY .. 1).last(2) #=> can't iterate from Float (TypeError)
OK. I have no strong opinion, too.
I have realized this is a not "Bug". I change the tracker "Feature".
Updated by wanabe (_ wanabe) about 6 years ago
- Status changed from Feedback to Rejected
Now, I think this proposal doesn't make sense.
So I reject it. thank you.