Project

General

Profile

Actions

Feature #12698

closed

Method to delete a substring by regex match

Added by sawa (Tsuyoshi Sawada) over 7 years ago. Updated about 1 year ago.

Status:
Feedback
Assignee:
-
Target version:
-
[ruby-core:<unknown>]

Description

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:

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

Then, the destructive ones:

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:

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

Related issues 1 (1 open0 closed)

Related to Ruby master - Feature #13890: Allow a regexp as an argument to 'count', to count more interesting things than single charactersOpenActions
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like1Like1Like0Like0