-1

我对 Ruby 有点陌生,我想用字符串替换局部变量。如果我有:

user = "foo"
pass = "bar"
puts "if you #{user}, you should #{bar}" 

#prints: if you foo, you should bar

但在我的情况下,字符串是文件中的一行文本,因此在解析时未声明。是否有任何内置功能可以让我赶上这种机制?

4

1 回答 1

4
string = "if you %{user}, you should %{pass}"
string % {user: "foo", pass: "bar"}
# => "if you foo, you should bar"
于 2013-11-04T16:17:28.057 回答