Just correct the original answer a bit.
Common usage of sprintf ( String#% ) expects the values ββto be presented in the same sequence as the format codes in the format string. The first value following the format string is used for the first substitution, the second value is used for the second substitution, etc.
sprintf("%d : %f : %d", 123, 456, 789) => "123 : 456.000000 : 789"
Ruby also supports link by name in format strings. To format the replacement value, use the form %<name>s . This form expects a formatting code following > . The order of values ββ(i.e., in the hash) does not determine where the values ββare used. The same value can be used more than once.
sprintf("%<foo>d : %<bar>f : %<bar>d", { :bar => 123, :foo => 456 }) => "456 : 123.000000 : 123"
If you do not want to format the replacement value, you can use the form %{name} , which does not expect code formatting. Everything that follows } is considered a literal, not a formatting code.
sprintf("%<foo>s", { :foo => 456 }) => "456" sprintf("%{foo} : %{foo}s", { :bar => 123, :foo => 456 }) => "456 : 456s"
source share