Issue #17184 has been updated by briankung (Brian Kung).


The hash replacement form of gsub doesn't have this problem, strangely enough:

``` ruby
'mislocated cat, vindicating'.gsub('cat', 'dog\+')
=> "mislodoged dog, vindidoging"
'mislocated cat, vindicating'.gsub('cat', 'cat' => 'dog\+')
=> "mislodog\\+ed dog\\+, vindidog\\+ing"

```


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

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