Single quote string string interpolation

后端 未结 3 924
借酒劲吻你
借酒劲吻你 2021-01-12 20:18

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

3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-12 20:23

    You can also use format. I have not seen it used as commonly in Ruby as in other languages (e.g. C, Python), but it works just as well:

    from = format('"Name of Person", <%s>', ENV["EMAIL"])
    

    Alternative syntax using the % operator:

    from = '"Name of Person", <%s>' % ENV["EMAIL"]
    

    Here is the documentation for format (aka sprintf):

    http://ruby-doc.org/core-2.2.0/Kernel.html#method-i-format

提交回复
热议问题