Better way to remove all occurrence of matches on a String

B

Benoit Daloze

Hi Rubyists !

I remember many times I searched for a better way to "remove all
occurrence of matches on a String".

I usually do:
str.gsub! /\bword\b/, ''

I remembered the String#[]= from tryruby.org, but that only replace
the first occurrence.
String#delete "Uses the same rules for building the set of characters
as String#count", and does not work with Regexp.
String#tr follow the same rules, and would need a second '' parameter.

What feels a bit "awkward" to me in the first approach is to specify I
want no replacement (so nothing, so I should not have to specify it).

I think for a default second parameter to (g)sub(!) (without block),
but this case is supposed to return an Enumerator (is it sometimes
useful?).
Also the "sub" part of the name clearly indicate it is substituting
sth for a new thing (the new thing should not be empty).

I would then go for #delete, adding a special case for RegExp
arguments, but that is not consistent with current behavior with a
String.
However, String#delete seems very rarely used, and could get some
interest back. (and "str".delete(/\bword\b/) do feel right to me)

What do you think ?

Benoit Daloze
 
H

Harry Kakueki

What feels a bit "awkward" to me in the first approach is to specify I
want no replacement (so nothing, so I should not have to specify it).

If you *really* want to avoid a second parameter, you could try
something like this.
But, I would not actually say it is better.
Anyway....


str = "foo bar foo bar foo"
p str.split(/\bbar\b/)*""


Harry
 
B

Benoit Daloze

If you *really* want to avoid a second parameter, you could try
something like this.
But, I would not actually say it is better.
Anyway....


str = "foo bar foo bar foo"
p str.split(/\bbar\b/)*""


Harry

Nice one, but I guess the extra parameter is not too bad compared to this :)
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

Hi Rubyists !

I remember many times I searched for a better way to "remove all
occurrence of matches on a String".

I usually do:
str.gsub! /\bword\b/, ''

I remembered the String#[]= from tryruby.org, but that only replace
the first occurrence.
String#delete "Uses the same rules for building the set of characters
as String#count", and does not work with Regexp.
String#tr follow the same rules, and would need a second '' parameter.

What feels a bit "awkward" to me in the first approach is to specify I
want no replacement (so nothing, so I should not have to specify it).

I think for a default second parameter to (g)sub(!) (without block),
but this case is supposed to return an Enumerator (is it sometimes
useful?).
Also the "sub" part of the name clearly indicate it is substituting
sth for a new thing (the new thing should not be empty).

I would then go for #delete, adding a special case for RegExp
arguments, but that is not consistent with current behavior with a
String.
However, String#delete seems very rarely used, and could get some
interest back. (and "str".delete(/\bword\b/) do feel right to me)

What do you think ?

Benoit Daloze
I think gsub with an empty string is ugly too, but I am pretty confident
there is not a better way.

You need to be careful, though, delete and tr don't work the way you think
they work. They are based on characters not words. They don't work with
regex because they consider the input string to be a set of characters, and
that doesn't make sense with regex.

"abcde".delete("bd") # => "ace"
"abcde".tr("bd","12") # => "a1c2e"
 
B

Benoit Daloze

I think gsub with an empty string is ugly too, but I am pretty confident
there is not a better way.

So I am not the only to think that :)
You need to be careful, though, delete and tr don't work the way you think
they work. They are based on characters not words. They don't work with
regex because they consider the input string to be a set of characters, and
that doesn't make sense with regex.

"abcde".delete("bd") # => "ace"
"abcde".tr("bd","12") # => "a1c2e"

Yes, I know, I proposed to add some functionality to #delete.
That is indeed a consistence issue.
However, the semantic of "delete" (in general) seems right to me to
delete/remove a part of a String (and I think is makes even more sense
than the current implementation which does tr(str,'')).
 
R

Robert Klemme

Hi Rubyists !

I remember many times I searched for a better way to "remove all
occurrence of matches on a String".

I usually do:
str.gsub! /\bword\b/, ''

I remembered the String#[]= from tryruby.org, but that only replace
the first occurrence.
String#delete "Uses the same rules for building the set of characters
as String#count", and does not work with Regexp.
String#tr follow the same rules, and would need a second '' parameter.

What feels a bit "awkward" to me in the first approach is to specify I
want no replacement (so nothing, so I should not have to specify it).

I think for a default second parameter to (g)sub(!) (without block),
but this case is supposed to return an Enumerator (is it sometimes
useful?).
Also the "sub" part of the name clearly indicate it is substituting
sth for a new thing (the new thing should not be empty).

I would then go for #delete, adding a special case for RegExp
arguments, but that is not consistent with current behavior with a
String.
However, String#delete seems very rarely used, and could get some
interest back. (and "str".delete(/\bword\b/) do feel right to me)

What do you think ?

Don't waste too much thought on this. After all the second parameter
makes it clear what's happening here. My 0.02 EUR.

Cheers

robert
 
B

Benoit Daloze

Don't waste too much thought on this. =A0After all the second parameter m= akes
it clear what's happening here. =A0My 0.02 EUR.

Cheers

=A0 =A0 =A0 =A0robert

Sure, it is already quite good now and it makes sense.

... But it can be better :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top