From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS4713 221.184.0.0/13 X-Spam-Status: No, score=-4.1 required=3.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from neon.ruby-lang.org (neon.ruby-lang.org [221.186.184.75]) by dcvr.yhbt.net (Postfix) with ESMTP id 6257B1F453 for ; Tue, 30 Apr 2019 15:07:48 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id B90DC120AFB; Wed, 1 May 2019 00:07:43 +0900 (JST) Received: from o1678916x28.outbound-mail.sendgrid.net (o1678916x28.outbound-mail.sendgrid.net [167.89.16.28]) by neon.ruby-lang.org (Postfix) with ESMTPS id A2FD5120887 for ; Wed, 1 May 2019 00:07:41 +0900 (JST) Received: by filter0138p3mdw1.sendgrid.net with SMTP id filter0138p3mdw1-28478-5CC86480-2 2019-04-30 15:06:40.002732592 +0000 UTC m=+411834.728619672 Received: from herokuapp.com (unknown [3.86.82.84]) by ismtpd0036p1mdw1.sendgrid.net (SG) with ESMTP id JZf0K9iGQEKi1T_w0JPb4w for ; Tue, 30 Apr 2019 15:06:40.011 +0000 (UTC) Date: Tue, 30 Apr 2019 15:06:40 +0000 (UTC) From: mame@ruby-lang.org Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 67980 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 15813 X-Redmine-Issue-Author: osyo X-Redmine-Sender: mame X-Mailer: Redmine X-Redmine-Host: bugs.ruby-lang.org X-Redmine-Site: Ruby Issue Tracking System X-Auto-Response-Suppress: All Auto-Submitted: auto-generated X-SG-EID: =?us-ascii?Q?EJh2gqwnyqXtd++xo=2FinyA1V0bXouTB4FkWnzNiKb4+nZCC8yZrNWDTIHhR5QE?= =?us-ascii?Q?580NN+ti7WuXoj8swVkw2p3es8v6+Naxdm5AmQ4?= =?us-ascii?Q?w6nf7624kHHNt1anGqtrSGxF5ILk+zTi0MAQ=2F0o?= =?us-ascii?Q?6lGLrdh0mWiEJjTl3AdNmHpf3XFcnT2Q7RRs1LA?= =?us-ascii?Q?eG=2FVL1X0SiUIyY2GJbMqMOQBXA7+hqJxcBA=3D=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 92497 Subject: [ruby-core:92497] [Ruby trunk Feature#15813] Proposal: Add exception support in `Range#first` X-BeenThere: ruby-core@ruby-lang.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: Ruby developers List-Id: Ruby developers List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ruby-core-bounces@ruby-lang.org Sender: "ruby-core" Issue #15813 has been updated by mame (Yusuke Endoh). Merged at 4e88e8692844a2a317bc19481f0f2601b6f00955. Thank you. ---------------------------------------- Feature #15813: Proposal: Add exception support in `Range#first` https://bugs.ruby-lang.org/issues/15813#change-77857 * Author: osyo (manga osyo) * Status: Open * 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/