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 EE7D51F453 for ; Tue, 30 Apr 2019 14:52:57 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id A679F120A6B; Tue, 30 Apr 2019 23:52:52 +0900 (JST) Received: from o1678948x4.outbound-mail.sendgrid.net (o1678948x4.outbound-mail.sendgrid.net [167.89.48.4]) by neon.ruby-lang.org (Postfix) with ESMTPS id 5EB5D120941 for ; Tue, 30 Apr 2019 23:52:49 +0900 (JST) Received: by filter0058p3mdw1.sendgrid.net with SMTP id filter0058p3mdw1-5018-5CC86140-1C 2019-04-30 14:52:48.735841928 +0000 UTC m=+411923.449808087 Received: from herokuapp.com (unknown [3.86.82.84]) by ismtpd0036p1iad2.sendgrid.net (SG) with ESMTP id WGJa8aY1QBW31OSYzE2tEA for ; Tue, 30 Apr 2019 14:52:48.649 +0000 (UTC) Date: Tue, 30 Apr 2019 14:52:48 +0000 (UTC) From: manga.osyo@gmail.com Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 67978 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+U6TJuuQMCAZ3OX2ybarlx6ke4XdACuql1VWWP64G4Rh7?= =?us-ascii?Q?suxs79avnwCyg7ZPukh8+yFWEe0+NEtpzt4x=2FQ0?= =?us-ascii?Q?2ao6K8xi3ZNz0nLRjUPCuB2pzl9Ez2cXMQSsp9t?= =?us-ascii?Q?IaQlzkFSXLs=2FqCjS1Try=2FjHaGnSYjao0+hAWmlS?= =?us-ascii?Q?8Msyo2TcsCbtQ6bVPgGTFKGctSu31sJ0RsQ=3D=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 92495 Subject: [ruby-core:92495] [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 reported by osyo (manga osyo). ---------------------------------------- Feature #15813: Proposal: Add exception support in `Range#first` https://bugs.ruby-lang.org/issues/15813 * 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/