在 Python 中,这种字符串格式化的习语很常见
s = "hello, %s. Where is %s?" % ("John","Mary")
Ruby 中的等价物是什么?
最佳答案
最简单的方法是string interpolation .您可以将一小段 Ruby 代码直接注入(inject)到您的字符串中。
name1 = "John"
name2 = "Mary"
"hello, #{name1}. Where is #{name2}?"
你也可以在 Ruby 中格式化字符串。
"hello, %s. Where is %s?" % ["John", "Mary"]
记得在此处使用方括号。 Ruby 没有元组,只有数组,并且使用方括号。
关于python - 什么是 Ruby 等价于 Python 的 `s= "你好,%s。 %s 在哪里 ?"% ("John","Mary")`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3554344/