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