Project

General

Profile

Actions

Feature #15927

closed

Allow string keys to be used for String#% and sprintf methods

Added by luke-gru (Luke Gruber) over 5 years ago. Updated 11 months ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:93161]

Description

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

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

String keys do not work like this:

"foo = %{foo}" % { 'foo' => 'bar' } #=> 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 found here: https://github.com/ruby/ruby/pull/2238

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

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

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

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0