Issue #12698 has been updated by duerst (Martin Drst).


matz (Yukihiro Matsumoto) wrote:
> I don't think it's worth adding which is easily done by `sub/gsub`.

If put this way, it's easy to agree. But looking at it starting from `String#delete`, it feels annoying that `String#delete` doesn't accept a regular expression. If the above argument were taken to its full conclusion, it would mean that we could depreciate `String#delete`, because `str.delete chars` can be easily rewritten as `str.gsub /[chars]/, ''`.

`String#count` is a quite similar case, and even stronger, because in that case, it's not easy to replace it by `sub/gsub`. See feature #13890.

The only other method on `String` that has a list of characters as an argument is `String#tr`. For that, I don't see how to add a regular expression as a parameter.


----------------------------------------
Feature #12698: Method to delete a substring by regex match
https://bugs.ruby-lang.org/issues/12698#change-74927

* Author: sawa (Tsuyoshi Sawada)
* Status: Feedback
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
There is frequent need to delete a substring from a string. There already are methods `String#delete` and `String#delete!`, but their feature is a little bit different from the use cases that I am mentioning here.

I request methods that take a string or a regexp as an argument, and delete the matches from the receiver string. I am not sure of the method name, and I will use the term `remove` here. It can be named in some other better way. I request all combinations of global vs. local, and non-destructive vs. destructive. The expected feature is something like the following. First, the non-destructive ones:

```ruby
"abcabc".remove("c") # => "ababc"
"abcabc".remove(/\zc/) # => "abcab"
"abcabc".gremove("c") # => "abab"
"abcabc".gremove(/c/) # => "abab"
```

Then, the destructive ones:

```ruby
s = "abcabc"
s.remove!("c") # => "ababc"
s # => "ababc"

s = "abcabc"
s.gremove!("d") # => nil
s # => "abcabc"
```

Using this, cases like https://bugs.ruby-lang.org/issues/12694 would be just special cases. They can be handled like this:

```ruby
"abcdef".remove(/\Aabc/) # => "def"



-- 
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>