From: manga.osyo@gmail.com
To: ruby-core@ruby-lang.org
Subject: [ruby-core:92498] [Ruby trunk Feature#15813] Proposal: Add exception support in `Range#first`
Date: Tue, 30 Apr 2019 15:10:49 +0000 (UTC) [thread overview]
Message-ID: <redmine.journal-77859.20190430151048.5b5e4ef70d818d43@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-15813.20190430145248@ruby-lang.org
Issue #15813 has been updated by osyo (manga osyo).
Thanks!!!!!!!
----------------------------------------
Feature #15813: Proposal: Add exception support in `Range#first`
https://bugs.ruby-lang.org/issues/15813#change-77859
* Author: osyo (manga osyo)
* Status: Closed
* Priority: Normal
* Assignee:
* Target version:
----------------------------------------
## Current status
Calling `Range#last` in endless range(`(1..)`) raises an exception.
```ruby
# OK
p (1..Float::INFINITY).end # => Infinity
p (1..).end # => nil
p (1..Float::INFINITY).last # => Infinity
# NG: Raise error: in `last': cannot get the last element of endless range (RangeError)
p (1..).last
p (1..).last(1)
```
But, calling `Range#first` in beginless range(`(..1)`) does not raise an exception.
```ruby
# OK
p (-Float::INFINITY..1).begin # => -Infinity
p (..1).begin # => nil
p (-Float::INFINITY..1).first # => -Infinity
# OK: Does not raise
p (..1).first # => nil
# NG: Raise error: in `each': can't iterate from NilClass (TypeError)
p (..1).first(1)
```
I think the current situation is not consistent, so it is necessary to move the behavior to one side or the other.
Also, in the case of `Range#last`, an exception is explicitly raised.
see: https://github.com/ruby/ruby/blob/6a3165e19dfa21babfb2ef1f1c20c9930410b0ec/range.c#L1100-L1102
## Proposal
Added support to raise an exception for `Range#first` too.
### Before
```ruby
# OK
p (-Float::INFINITY..10).begin # => -Infinity
p (..10).begin # => nil
p (-Float::INFINITY..10).first # => -Infinity
p (..10).first # => nil
p Range.new(nil, 10).first # => nil
# NG: Raise error: in `each': can't iterate from NilClass (TypeError)
p (..10).first(1)
```
### After
```ruby
# OK
p (-Float::INFINITY..10).begin # => -Infinity
p (..10).begin # => nil
p (-Float::INFINITY..10).first # => -Infinity
# NG: Raise error: in `first': cannot get the first element of beginless range (RangeError)
p (..10).first
p Range.new(nil, 10).first
p (..10).first(1)
# in Ruby 2.6.1
p Range.new(nil, 10).first
# Error: in `initialize': bad value for range (ArgumentError)
```
Thank you.
pull request : https://github.com/ruby/ruby/pull/2163
--
https://bugs.ruby-lang.org/
prev parent reply other threads:[~2019-04-30 15:11 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <redmine.issue-15813.20190430145248@ruby-lang.org>
2019-04-30 14:52 ` [ruby-core:92495] [Ruby trunk Feature#15813] Proposal: Add exception support in `Range#first` manga.osyo
2019-04-30 15:06 ` [ruby-core:92497] " mame
2019-04-30 15:10 ` manga.osyo [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-list from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.ruby-lang.org/en/community/mailing-lists/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=redmine.journal-77859.20190430151048.5b5e4ef70d818d43@ruby-lang.org \
--to=ruby-core@ruby-lang.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).