[ruby-core:78239] Re: [Ruby trunk Feature#12964] Add sub? and gsub? method to string class.
From:
Eric Wong <normalperson@...>
Date:
2016-11-21 21:50:34 UTC
List:
ruby-core #78239
lachlan@wilgerfamily.com wrote:
> The sub? and gsub? method would allow you to check if you can
> sub or gsub the string.
We already added Regexp#match? for Ruby 2.4 to check this.
Fwiw, I prefer to use bang methods if the substitution is likely;
and maybe String#dup if the original needs to be preserved[*].
if foo.sub!(/^something /, '')
...
end
[*] Strings are copy-on-write in MRI, so at most you allocate
one object and copy 40 bytes if no substitution is possible.
Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>