什么是Ruby的等同于Python的s =“hello,%s。 %s?%(“John”,“Mary”)`
在Python中,这种string格式的习惯用法很常见
s = "hello, %s. Where is %s?" % ("John","Mary") Ruby中的等价物是什么?
最简单的方法是string插值 。 你可以直接在你的string中注入一小段Ruby代码。
 name1 = "John" name2 = "Mary" "hello, #{name1}. Where is #{name2}?" 
你也可以在Ruby中格式化string。
 "hello, %s. Where is %s?" % ["John", "Mary"] 
记得在那里使用方括号。 Ruby没有元组,只有数组,而那些使用方括号。
在Ruby 1.9中,你可以这样做:
 s = "hello, %{name1}. Where is %{name2} ?" % { :name1 => 'John', :name2 => 'Mary' } 
编辑:添加缺less':'s
参考: http : //ruby-doc.org/core-1.9.3/String.html
几乎相同的方式:
 irb(main):003:0> "hello, %s. Where is %s?" % ["John","Mary"] => "hello, John. Where is Mary?" 
其实几乎一样
 s = "hello, %s. Where is %s?" % ["John","Mary"]