[ruby-core:95677] [Ruby master Feature#15899] String#before and String#after
From:
jonathan@...
Date:
2019-11-04 20:57:07 UTC
List:
ruby-core #95677
Issue #15899 has been updated by jonathanhefner (Jonathan Hefner).
I use monkey-patched versions of these in many of my Ruby scripts. They have a few benefits vs. the alternatives:
* vs. `split` + `first` / `last`
* using `split` can cause an unintended result when the delimiter is not present, e.g. `"abc".split("x", 2).last == "abc"`
* vs. `partition`
* `before` and `after` can be chained, and can result in fewer object allocations
* vs. regex + capture group
* `before` and `after` are easier to read (and write)
I've also found [`before_last`](https://www.rubydoc.info/gems/casual_support/String:before_last) and [`after_last`](https://www.rubydoc.info/gems/casual_support/String:after_last) helpful for similar reasons.
kke (Kimmo Lehto) wrote:
> What should happen if the marker isn't found? In my opinion, `before` should return the full string and `after` an empty string.
Regarding `before`, I agree.
Regarding `after`, I originally wrote my monkey-patched `after` to return an empty string, but eventually changed it to return nil. I was hesitant because a nil result can be an unexpected "gotcha", but an empty string seems wrong because it throws away information. For example, if `str.after("x") == ""`, it might be because the delimiter wasn't found, or because the delimiter was at the end of the string. (Compared to `str.before("x") == str`, which always means the delimiter wasn't found.)
----------------------------------------
Feature #15899: String#before and String#after
https://bugs.ruby-lang.org/issues/15899#change-82464
* 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.
---Files--------------------------------
test.rb (712 Bytes)
test_mem.rb (326 Bytes)
2269.diff (3.77 KB)
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>