字符串插值不打印变量值,是打印精确字符串

时间:2013-08-20 18:58:11

标签: ruby

我有一个看起来像(@user@foo传递给包含方法的邮件):

mail(to: @user.email, subject: 'Foo is expired', 
    body: 'Your Foo reservation for #{@foo.bar.name} in position #{@foo.position}
    has expired.  Please recreate the reservation if necessary')

我正在使用一些put来测试它,把mail.body看起来像:

'Your Foo reservation for #{@foo.bar.name} in position #{@foo.position}
 has expired.  Please recreate the reservation if necessary'

我只是在误解我是如何进行插值的?它与ActionMailer有关,还是将文本输出到控制台?

感谢。

2 个答案:

答案 0 :(得分:6)

ruby中的字符串插值仅适用于双引号""

答案 1 :(得分:3)

要进行插值,您需要使用双引号"而非单引号'

> a = 'test'
 => "test" 
> 'Testing #{a}'     # This won't interpolate
 => "Testing \#{a}" 
> "Testing {a}"      # This interpolates               
 => "Testing test"