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) almost 5 years ago. Updated almost 5 years ago.

Status:
Closed
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

Like0
Like0Like0Like0Like0