Project

General

Profile

Actions

Bug #15895

closed

String#gsub and String#sub should return original string if no substitution(s) have been made

Added by ashmaroli (Ashwin Maroli) over 3 years ago. Updated over 3 years ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:92933]

Description

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.

Actions

Also available in: Atom PDF