Issue #17184 has been updated by byroot (Jean Boussier).


> But then we need to clarify if that replaces the first or all occurrences

As far as I know, String `replace` commonly means replace all occurrences.

- Python: replaces all occurrences, but has a `maxreplace` optional parameters. https://docs.python.org/2/library/string.html#string.replace
- PHP: replaces all occurrences https://www.php.net/manual/en/function.str-replace.php
- Javascript, is a bit weird, it's all if the search is a regexp with the `/g` modifier, for strings, it's the first occurence, and it has `replaceAll` alongside it.
- Java: replaces all occurrences https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#replace(char,%20char)
- C#: replaces all occurrences: https://docs.microsoft.com/en-us/dotnet/api/system.string.replace?view=netcore-3.1
- Go has replace and replaceAll like Javascript: https://golang.org/pkg/strings/#Replace
- Rust replaces all occurrences: https://doc.rust-lang.org/std/string/struct.String.html#method.replace

So all in all I don't think it's a huge problem, the least surprising IMHO would be replace all occurrence, and I find Python's optional `maxreplace` quite a good idea.

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

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