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.1 required=3.0 tests=AWL,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 EC71F1F770 for ; Wed, 2 Jan 2019 10:12:09 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 93C7E1210C6; Wed, 2 Jan 2019 19:12:06 +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 90C641210C6 for ; Wed, 2 Jan 2019 19:12:04 +0900 (JST) Received: by filter0048p3las1.sendgrid.net with SMTP id filter0048p3las1-14625-5C2C8E71-36 2019-01-02 10:12:01.909616304 +0000 UTC m=+1078612.775547558 Received: from herokuapp.com (ec2-54-224-14-133.compute-1.amazonaws.com [54.224.14.133]) by ismtpd0026p1iad2.sendgrid.net (SG) with ESMTP id PQzBsTptRXqfRDmvGCINAg Wed, 02 Jan 2019 10:12:01.690 +0000 (UTC) Date: Wed, 02 Jan 2019 10:12:02 +0000 (UTC) From: shevegen@gmail.com To: ruby-core@ruby-lang.org Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 66276 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 15496 X-Redmine-Issue-Author: macdevign X-Redmine-Sender: shevegen 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/Ymy4QrNMhiuLXJG8OTL2vJD1yS4B+puf2s+qMfzczA6Y+sx6yn1i0YjZlnOobK GTBP0nb+X6O8o97xmNuM9Ewac6Pc3iXGB0Wu3m15DeHOLewVfMFLtjgLxQr7NKg1xS1laoEAOPfU3J enQp/8KuU5/O8+FOigin1mbjQQlf/lWpBga8/u6gsRul3sH4bmCBhxIegg== X-ML-Name: ruby-core X-Mail-Count: 90852 Subject: [ruby-core:90852] [Ruby trunk Feature#15496] Extract between string as standard String api 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 #15496 has been updated by shevegen (Robert A. Heiler). Hmmm. I don't have a preference here since I can find arguments in favour or against this. It's fairly easily possible to use a regex so .between (or any other name to extract the substring between two boundaries) is not that necessary; on the other hand it may be simpler to use an "officially approved" method. If you feel strongly about your proposal you could propose it for mention during a developer meeting at: https://bugs.ruby-lang.org/issues/15462 (In ~a week or so from now on.) Then you may get matz' opinion about the proposal, both feature-wise and API/name wise. ---------------------------------------- Feature #15496: Extract between string as standard String api https://bugs.ruby-lang.org/issues/15496#change-76039 * Author: macdevign (Macdevign mac) * Status: Open * Priority: Normal * Assignee: * Target version: ---------------------------------------- I could not find the a simple String api to extract the string between two string, and I notice that many face the same issue and endup rolling their own solutions (eg https://stackoverflow.com/questions/9661478/how-to-return-the-substring-of-a-string-between-two-strings-in-ruby). Given that string "between" extraction is such a common operation, will adding a focused and simplified String method make coding pleasant ? This is my solution but probably someone can provide better and efficient implementation. ~~~ # self: String instance, # from: first 'from' String # to: first 'to' String found after 'from' # return string between from and to, which exclude the argument, nil otherwise. def between(from, to) from_idx = self.index(from)&.+(from.length) if from_idx to_idx = self.index(to, from_idx) return self[from_idx...to_idx] if to_idx end nil end ~~~ Test case "Hello world".between("ell", "ld") => "o wor" "Testing 123".between("Te", "123") => "sting " -- https://bugs.ruby-lang.org/