单引号字符串字符串内插 [英] Single quote string string interpolation

查看:119
本文介绍了单引号字符串字符串内插的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Rails在ActionMailer中设置一个电子邮件地址。在进行硬编码之前,但我们现在想将它们设置为ENV变量,这样就不必在每次电子邮件更改时都修改代码。

I am trying to set an email address within ActionMailer with Rails. Before it was hard coded, but we want to make them ENV variables now so we don't need to amend code each time an email changes.

这是当前的定义:

from = '"Name of Person" <email@email.com>'

我尝试将电子邮件设置为使用 ENV ['EMAIL'] 的环境变量,但即使使用#{ENV ['EMAIL'}

I've tried setting the email as an environment variable using ENV['EMAIL'] but I'm having no luck even with #{ENV['EMAIL'}.

有人能指出我正确的方向吗?

Can anyone point me in the right direction?

推荐答案

在Ruby中,不能对单引号字符串使用字符串插值。

You cannot use string interpolation with single-quoted strings in Ruby.

但是带双引号的字符串可以!

But double-quoted strings can!

from = "'Name of Person' <#{ENV['EMAIL']}>"

但是如果您想保留双引号将姓名,您可以使用反斜杠 \

But if you want to keep your double-quotes wrapping the Name of Person, you can escape them with a backslash \:

from = "\"Name of Person\" <#{ENV['EMAIL']}>"

或使用字符串连接:

from = '"Name of Person" <' + ENV['EMAIL'] + '>'
# but I find it ugly

这篇关于单引号字符串字符串内插的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆