1
是否可以將參數的順序更改爲sprintf
?在ruby sprintf中更改參數的順序
像sprintf(" this is %arg[2] test %arg[1]" , arg1, arg2)
我需要動態改變的參數的順序,從而是可能的sprintf
?
是否可以將參數的順序更改爲sprintf
?在ruby sprintf中更改參數的順序
像sprintf(" this is %arg[2] test %arg[1]" , arg1, arg2)
我需要動態改變的參數的順序,從而是可能的sprintf
?
是的。
irb(main):007:0> arg1 = 'foo'
=> "foo"
irb(main):008:0> arg2 = 'bar'
=> "bar"
irb(main):009:0> sprintf("%3$0.3f this is %2$s test %1$s" , arg1, arg2, Math::PI)
=> "3.142 this is bar test foo"
格式爲%N$fmt
,其中N表示該參數的順序位置,並且fmt
是你會使用sprintf
當正常的%
符號後放什麼。