Issue #17184 has been updated by Dan0042 (Daniel DeLorme).


> I think we should remove special treatment of `\+`, etc in the replacement string for `sub/gsub(String, String)`.
> There is no Regexp involved, so I think there is no reason to have those Regexp backreferences.

I agree that would be the sensible behavior, but I'm not sure the incompatibility is worth it. Imagine you try `"foo".gsub("o", '\+')` expecting to get `f\+\+` as a result. Instead you get just `f`, and after a lot of searching and debugging you end up with `"foo".gsub("o", '\\\\+')` which produces the correct result. If we were to change the behavior, the result will change to `f\\+\\+`. I'm sure there's a lot of ruby code out there that use a replacement string with a ton of backslashes to avoid this issue. Breaking all that code is not a good idea imho.

----------------------------------------
Bug #17184: No stdlib function to perform simple string replacement
https://bugs.ruby-lang.org/issues/17184#change-87684

* Author: sheerun (Adam Stankiewicz)
* Status: Open
* Priority: Normal
* ruby -v: ruby 2.5.5p157 (2019-03-15 revision 67260) [x86_64-darwin19]
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN
----------------------------------------
I have following simple `build.rb`:

```rb
template = File.read('template.vim')
script = File.read('script.vim')
File.write('app.vim', template.gsub("SCRIPT", script))
```

And then following `template.vim`:

```vim
" some header
SCRIPT
```

Plus following `script.vim`:


```vim
if g:something =~ "\s\+"
  echo 'g:something is empty'
endif
```

I'd expect that the script above produces `app.vim` with following contents:

```vim
" some header
if g:something =~ "\s\+"
  echo 'g:something is empty'
endif
```

Unfortunately it produces following:

```vim
" some header
if g:something =~ "\s"
  echo 'g:something is empty'
endif
```

It's probably because gsub interprets `\+` in script as back-reference.

I tried to find replacement function in ruby that just replaces one string with something else, without interpreting replacement in any way, but surprisingly I haven't found any.. Am I mistaken?




-- 
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>