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=-3.0 required=3.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, FORGED_GMAIL_RCVD,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,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 D027D1F453 for ; Tue, 30 Apr 2019 15:11:57 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 47E2F120B0A; Wed, 1 May 2019 00:11:53 +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 66347120B07 for ; Wed, 1 May 2019 00:11:51 +0900 (JST) Received: by filter0073p3iad2.sendgrid.net with SMTP id filter0073p3iad2-7964-5CC86579-E 2019-04-30 15:10:49.228991507 +0000 UTC m=+412724.234526529 Received: from herokuapp.com (unknown [3.86.82.84]) by ismtpd0040p1mdw1.sendgrid.net (SG) with ESMTP id TKDOCsK-SFinqXwAEWf0kg for ; Tue, 30 Apr 2019 15:10:49.004 +0000 (UTC) Date: Tue, 30 Apr 2019 15:10:49 +0000 (UTC) From: manga.osyo@gmail.com Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 67981 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 15813 X-Redmine-Issue-Author: osyo X-Redmine-Sender: osyo 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?EoIqruA4Er0CjiyK1+U6TJuuQMCAZ3OX2ybarlx6ke7+NSLNEbzq20k9v5gBiE?= =?us-ascii?Q?wgeqpnoakV=2Foupd=2Fp2eIDQY7nU5WnSADbpu1Eu0?= =?us-ascii?Q?5Lan9xBIyb=2FmRasFrGr=2FMv8KZ0kamwxy=2FAuXhvv?= =?us-ascii?Q?f3rS0ujmM3pNEOu2vLBfHEg0n0fYZVQWd7ooH5c?= =?us-ascii?Q?wyxl0stQl80+tc7l+CvI6fbQmfZqsEqtoAw=3D=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 92498 Subject: [ruby-core:92498] [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 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/