Issue #17184 has been updated by Eregon (Benoit Daloze).

Assignee set to matz (Yukihiro Matsumoto)

Dan0042 (Daniel DeLorme) wrote in #note-12:
> 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.

Are there examples of such code?
It seems hard to read and a worse version of `"foo".gsub("o") { '\+' }` which is more general.
I would guess it's so rare that this is definitely worth the tiny corner-case incompatibility.
And I would think it would reveal more unintended strange replacement cases than actually breaking such manually-escaped constant string cases.

byroot (Jean Boussier) wrote in #note-13:
> IMHO, rather than change the behavior of `gsub`, introducing a much simpler `String#replace(search, replace)` would make more sense, and would be more discoverable.

But then we need to clarify if that replaces the first or all occurrences (and String#[]= is hardly intuitive or related by name).

`sub`/`gsub` already have optimizations for (String, String) cases (notably, they don't use a Regexp internally).
It seems sad to not simply reuse them when the intended semantics are very clear.
It seems a huge pitfall to me that `sub/gsub(String, String)` will not use the replacement String as-is. A new method does not address that.

@matz Can we experiment with that change? I expect extremely few incompatibilities.
I expect it will fix many usages which do not expect any magic backreferences replacement.

Does anyone know why this behavior exists in the first place?
I guess maybe because sub/gsub(String, String) were once implemented by making a Regexp for the pattern and that scanned the replacement String for backreferences, causing this strange behavior?

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

* Author: sheerun (Adam Stankiewicz)
* Status: Open
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
* 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>