From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS4713 221.184.0.0/13 X-Spam-Status: No, score=-3.7 required=3.0 tests=AWL,BAYES_00, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.0 Received: from neon.ruby-lang.org (neon.ruby-lang.org [221.186.184.75]) by dcvr.yhbt.net (Postfix) with ESMTP id EC3621F51C for ; Thu, 31 May 2018 08:10:36 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id E1A14120A28; Thu, 31 May 2018 17:10:33 +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 83EE8120A27 for ; Thu, 31 May 2018 17:10:31 +0900 (JST) Received: by filter0053p3mdw1.sendgrid.net with SMTP id filter0053p3mdw1-15398-5B0FADF3-34 2018-05-31 08:10:27.620641095 +0000 UTC Received: from herokuapp.com (ec2-54-166-204-225.compute-1.amazonaws.com [54.166.204.225]) by ismtpd0041p1mdw1.sendgrid.net (SG) with ESMTP id 1NkMbspOQByXxodJqE_I0w for ; Thu, 31 May 2018 08:10:27.434 +0000 (UTC) Date: Thu, 31 May 2018 08:10:28 +0000 (UTC) From: shyouhei@ruby-lang.org To: ruby-core@ruby-lang.org Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 62678 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 14799 X-Redmine-Issue-Author: zverok X-Redmine-Sender: shyouhei 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: ync6xU2WACa70kv/Ymy4QrNMhiuLXJG8OTL2vJD1yS79EpYTnGaRiEypZ7AkXa4hbbnKgfISqzJPFL B6NjFQiGhSXQw/aVdosmcYvv8y08GPkbGJz9SayfZS3CWZNdMZ5of3gULrlSuEjNWF3+Op3aXpzCEG LU/1QPngPezK3GcMp6buHDwmCEAOgTIyDOEu4IcjYljohxbBqUzFJ+KGjQ== X-ML-Name: ruby-core X-Mail-Count: 87314 Subject: [ruby-core:87314] [Ruby trunk Feature#14799] Startless range 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 #14799 has been updated by shyouhei (Shyouhei Urabe). No strong opinion on this. However let me leave one question: how should Range#each work for this kind of ranges? ---------------------------------------- Feature #14799: Startless range https://bugs.ruby-lang.org/issues/14799#change-72307 * Author: zverok (Victor Shepelev) * Status: Open * Priority: Normal * Assignee: * Target version: ---------------------------------------- On introduction of endless range at #12912, "startless range" was discussed this way: > @sowieso: Not having the opposite (`..5` and `..-2`) feels like this is rather a hack than a thoroughly planned feature. > @duerst: I don't understand the need for a `..5` Range. The feature is called "endless range". Although mathematically, it's possible to think about startless ranges, they don't work in a program. Maybe some programming languages have `..5` as a shortcut for `0..5`, but that's in any way a usual, bounded, range with a start and an end. It's conceptually totally different from `5..`, which is a range with a start but no end, an unbound range. In the context of that ticket (ranges used mostly for slicing arrays) having `..5` was indeed hard to justify, but there are other cases when `..5` being `-Infinity..5` is absolutely reasonable: ```ruby case release_date when ..1.year.ago puts "ancient" when 1.year.ago..3.months.ago puts "old" when 3.months.ago..Date.today puts "recent" when Date.today.. puts "upcoming" end log.map(&:logged_at).grep(..Date.new(1980)) # => outliers due to bad log parsing... ``` E.g., whenever case equality operator is acting, having startless range to express "below this value" is the most concise and readable way. Also, for expressing constants (mostly decorative, but very readable): ```ruby # Celsius degrees WORK_RANGES = { ..-10 => :turn_off, -10..0 => :energy_saving, 0..20 => :main, 20..35 => :cooling, 35.. => :turn_off } ``` In addition, my related proposal #14784 suggests that this kind of ranges could be utilized by more powerful clamp too: ```ruby updated_at.clamp(..Date.today) ``` **Uncertainty points:** * Would it be hard to add to parser? I am not sure, I am not very good at it :( * Should `..` be a thing? I guess not, unless there would be convincing real-life examples, which for me it is hard to think of. -- https://bugs.ruby-lang.org/