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>