From: shevegen@gmail.com
To: ruby-core@ruby-lang.org
Subject: [ruby-core:90852] [Ruby trunk Feature#15496] Extract between string as standard String api
Date: Wed, 02 Jan 2019 10:12:02 +0000 (UTC) [thread overview]
Message-ID: <redmine.journal-76039.20190102101201.3b04535a63f9fdcd@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-15496.20190102085159@ruby-lang.org
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/
next prev parent reply other threads:[~2019-01-02 10:12 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <redmine.issue-15496.20190102085159@ruby-lang.org>
2019-01-02 8:52 ` [ruby-core:90851] [Ruby trunk Feature#15496] Extract between string as standard String api macdevign
2019-01-02 10:12 ` shevegen [this message]
2019-01-04 6:46 ` [ruby-core:90884] " duerst
2019-01-05 7:23 ` [ruby-core:90894] " macdevign
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-list from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.ruby-lang.org/en/community/mailing-lists/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=redmine.journal-76039.20190102101201.3b04535a63f9fdcd@ruby-lang.org \
--to=ruby-core@ruby-lang.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).