From: ashmaroli@... Date: 2019-06-03T09:18:58+00:00 Subject: [ruby-core:92933] [Ruby trunk Bug#15895] String#gsub and String#sub should return original string if no substitution(s) have been made Issue #15895 has been reported by ashmaroli (Ashwin Maroli). ---------------------------------------- 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 * Author: ashmaroli (Ashwin Maroli) * Status: Open * 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: