Issue #15927 has been updated by duerst (Martin D=FCrst).


I agree with @sawa that there's a difference between gsub (where strings ar=
e replaced by strings) and sprintf, where it's interpolating something very=
 close to variables.

A use case such as
```
"foo =3D %{foo}" % { 'foo' =3D> 'bar' }
```
can just be rewritten to =

```
"foo =3D %{foo}" % { foo: 'bar' }
```
Can you give us some actual use case(s) where such rewriting would not be p=
ossible, or very tedious?


----------------------------------------
Feature #15927: Allow string keys to be used for String#% and sprintf metho=
ds
https://bugs.ruby-lang.org/issues/15927#change-78612

* Author: luke-gru (Luke Gruber)
* Status: Open
* Priority: Normal
* Assignee: =

* Target version: =

----------------------------------------
Right now, in the methods sprintf() and String#%, only symbol keys can be u=
sed for named interpolation. For example (from the example documentation):

"foo =3D %{foo}" % { :foo =3D> 'bar' }        #=3D> "foo =3D bar"

String keys do not work like this:

"foo =3D %{foo}" % { 'foo' =3D> 'bar' }        #=3D> raises KeyError

I think string keys should work just the same as symbol keys here.

I've created a PR on github for this, perhaps misguidedly, but if can be fo=
und here: https://github.com/ruby/ruby/pull/2238

My argument for this feature is that String#gsub() and family works with st=
ring keys if given a Hash, for example:

"chocolate ice cream".gsub(/\bc/, { 'c' =3D> 'C' }) #=3D> 'Chocolate ice Cr=
eam'

Also, I don't like having to symbolize keys in strings unnecessarily, but m=
aybe that just goes back to when
Ruby couldn't GC some symbols.





-- =

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

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=3Dunsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>