Feature #8350
closedExtending `%{}` notation in string to accept an array
Description
=begin
String#%
accepts a hash as an argument and interpolates the values into %{}
-notated portions of the string:
"%{b} said %{a} to %{c}" % {a: "Foo", b: "Bar", c: "Baz"} #=> "Bar said Foo to Baz"
I would like to have it allow an array as well, where the %{}
-notation has indices:
"%{1} said %{0} to %{2}" % ["Foo", "Bar", "Baz"] #=> "Bar said Foo to Baz"
Of course, since the %{}
notation interprets its content as a symbol, the following should also work:
"%{1} said %{0} to %{2}" % {:"0" => "Foo", :"1" => "Bar", :"2" => "Baz"} #=> "Bar said Foo to Baz"
but that would not cause any conflict.
There is also a different usage of String#%
with an array argument like this:
"%-5s: %08x" % [ "ID", self.object_id ] #=> "ID : 200e14d6"
but that does not allow the array elements to appear in the string in an order different from as is in the array. The proposed feature is distinct from this, and adds flexibility.
=end
Updated by nobu (Nobuyoshi Nakada) over 11 years ago
- Status changed from Open to Rejected
sawa (Tsuyoshi Sawada) wrote:
but that would not cause any conflict.
There is also a different usage ofString#%
with an array argument like this:"%-5s: %08x" % [ "ID", self.object_id ] #=> "ID : 200e14d6"
but that does not allow the array elements to appear in the string in an order different from as is in the array. The proposed feature is distinct from this, and adds flexibility.
It does allow.
"%2$-5s: %1$08x" % [self.object_id, "ID"] #=> "ID : 200e14d6"