Issue #11793 has been updated by Matthew Kerwin.


Substitution parameters work in string-string mode:

~~~
irb> puts 'ab'.gsub('a', %q(\`))
b
irb> puts 'ab'.gsub('a', %q(\&))
ab
irb> puts 'ab'.gsub('a', %q(\'))
bb
irb> puts 'ab'.gsub('a', %q(\0))
ab
irb> puts 'ab'.gsub('a', %q(\\0))
ab
irb> puts 'ab'.gsub('a', %q(\\\0))
\0b
irb> puts 'ab'.gsub('a', %q(\\\+))
\+b
~~~

I used `%q()` to make it clear just how many blackslashes are involved.

Note with the `\0` examples, either a single *or* double-backslash invokes the special value replacement.

The plus symbol (`$+` or `\+`) is an alias for the `$LAST_PAREN_MATCH` special value, which in this case is blank.

----------------------------------------
Bug #11793: puts 'ab'.gsub('a', '\\+') - unexpected output
https://bugs.ruby-lang.org/issues/11793#change-55393

* Author: William Burnson
* Status: Rejected
* Priority: Normal
* Assignee: 
* ruby -v: ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-darwin15]
* Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN
----------------------------------------
Test case:
puts 'ab'.gsub('a', '\\\\+')

Expected output:
\\+b

Actual output:
b

The way I understand gsub(pattern, replacement) when used with two string arguments is that it will perform a literal replacement, so it is quite unexpected that substituting with \\+ removes the pattern entirely.

Doc: http://ruby-doc.org/core-2.2.3/String.html#method-i-gsub



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