From: kimmo.lehto@gmail.com
To: ruby-core@ruby-lang.org
Subject: [ruby-core:92995] [Ruby trunk Feature#15899] String#before and String#after
Date: Thu, 06 Jun 2019 07:00:11 +0000 (UTC) [thread overview]
Message-ID: <redmine.journal-78373.20190606070010.a64cba157ea6a6a8@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-15899.20190605072723@ruby-lang.org
Issue #15899 has been updated by kke (Kimmo Lehto).
> Using partition looks reasonable, and it can accept regexes.
It also has the problem of creating extra objects that you need to discard with `_` or assign and just leave unused.
> I am not a big fan of the names, though. I somehow associate #before and #after
more with time-based operations; and rack/sinatra middleware (route) filters.
How about `str.preceding(';')` and `str.following(';')`?
Perhaps `str.prior_to(';')` and `str.behind(';')`?
Possibility of opposite reading direction can make these problematic.
`str.left_from(';')`, `str.right_from(';')`? Sounds a bit clunky.
Head and tail could be the unixy choice and more versatile for other use cases.
```ruby
class String
def head(count = 10, separator = "\n")
...
end
def tail(count = 10, separator = "\n")
...
end
end
```
For my example use case, it would become:
```ruby
str = "application/json; charset=utf-8"
mime = str.head(1, ';')
labels = str.tail(1, ';')
```
And to emulate something like `$ curl xttp://x.example.com | head` you would use `response.body.head`
----------------------------------------
Feature #15899: String#before and String#after
https://bugs.ruby-lang.org/issues/15899#change-78373
* Author: kke (Kimmo Lehto)
* Status: Open
* Priority: Normal
* Assignee:
* Target version:
----------------------------------------
There seems to be no methods for getting a substring before or after a marker.
Too often I see and have to resort to variations of:
``` ruby
str[/(.+?);/, 1]
str.split(';').first
substr, _ = str.split(';', 2)
str.sub(/.*;/, '')
str[0...str.index(';')]
```
These create intermediate objects or/and are ugly.
The `String#delete_suffix` and `String#delete_prefix` do not accept regexps and thus only can be used if you first figure out the full prefix or suffix.
For this reason, I suggest something like:
``` ruby
> str = 'application/json; charset=utf-8'
> str.before(';')
=> "application/json"
> str.after(';')
=> " charset=utf-8"
```
What should happen if the marker isn't found? In my opinion, `before` should return the full string and `after` an empty string.
--
https://bugs.ruby-lang.org/
next prev parent reply other threads:[~2019-06-06 7:00 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <redmine.issue-15899.20190605072723@ruby-lang.org>
2019-06-05 7:27 ` [ruby-core:92972] [Ruby trunk Feature#15899] String#before and String#after kimmo.lehto
2019-06-05 8:02 ` [ruby-core:92973] " sawadatsuyoshi
2019-06-05 8:06 ` [ruby-core:92974] " sawadatsuyoshi
2019-06-05 9:12 ` [ruby-core:92976] " shevegen
2019-06-06 7:00 ` kimmo.lehto [this message]
2019-06-14 7:30 ` [ruby-core:93132] " kimmo.lehto
2019-06-14 14:54 ` [ruby-core:93143] " ruby-core
2019-07-09 18:34 ` [ruby-core:93645] [Ruby master " eddm
2019-11-04 20:57 ` [ruby-core:95677] " jonathan
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-78373.20190606070010.a64cba157ea6a6a8@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).