TIM*_*MEX 140 ruby python string-formatting
在Python中,这种用于字符串格式化的习惯用法很常见
s = "hello, %s. Where is %s?" % ("John","Mary")
Run Code Online (Sandbox Code Playgroud)
Ruby中的等价物是什么?
Abo*_*uby 242
最简单的方法是字符串插值.您可以将少量Ruby代码直接注入到字符串中.
name1 = "John"
name2 = "Mary"
"hello, #{name1}. Where is #{name2}?"
Run Code Online (Sandbox Code Playgroud)
您还可以在Ruby中格式化字符串.
"hello, %s. Where is %s?" % ["John", "Mary"]
Run Code Online (Sandbox Code Playgroud)
记得在那里使用方括号.Ruby没有元组,只有数组,而那些使用方括号.
too*_*ong 51
在Ruby> 1.9中你可以这样做:
s = 'hello, %{name1}. Where is %{name2}?' % { name1: 'John', name2: 'Mary' }
Run Code Online (Sandbox Code Playgroud)
Man*_*dan 19
几乎相同的方式:
irb(main):003:0> "hello, %s. Where is %s?" % ["John","Mary"]
=> "hello, John. Where is Mary?"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
77713 次 |
| 最近记录: |