Issue #15895 has been updated by k0kubun (Takashi Kokubun).

Status changed from Open to Feedback

I can understand why you want it, but that would be a breaking change for existing code which assumes that `gsub` always creates a new String instance and performs a destructive operation on the `gsub`'s return value. In that code the original string is suddenly modified once `gsub` behavior is changed as such.
If you really need it, please consider proposing an option to force the behavior or another method.

Besides, please share a code which reflects your real-world use case which has `gsub` in a loop. The code and its benchmark results may encourage us to introduce such a feature. It may not be a bottleneck for your application and in that case the feature could be just a useless micro optimization. At least, I don't think we write a code like `'Hello World'.sub(/\d+/, 'x')`, and also you don't need to write `'Hello World'.gsub(/[<&>]/, html_entities_hash)` because I wrote the fast HTML escape method in Ruby core which is NOT using `gsub` https://github.com/ruby/ruby/pull/1164.

----------------------------------------
Bug #15895: String#gsub and String#sub should return original string if no substitution(s) have been made 
https://bugs.ruby-lang.org/issues/15895#change-78311

* Author: ashmaroli (Ashwin Maroli)
* Status: Feedback
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: 2.4.5
* Backport: 2.4: UNKNOWN, 2.5: UNKNOWN, 2.6: UNKNOWN
----------------------------------------
Currently if one were to call `'Hello World'.gsub(/[<&>]/, html_entities_hash)` , a copy of 'Hello World' is allocated and returned. If such a call were to occur inside a loop, then that would cause numerous copies to be allocated simply from an attempt at substitution.

Likewise for `'Hello World'.sub(/\d+/, 'x')`

Opting for the destructive alternatives is not possible since the original string should remain unchanged in all cases.

IMO, it'd be great to have the original string returned if substitution(s) couldn't be made.



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>