Interpolation within single quotes

点点圈 提交于 2019-11-27 17:54:24

问题


How can I perform interpolation within single quotes?

I tried something like this but there are two problems.

string = 'text contains "#{search.query}"'
  1. It doesn't work
  2. I need the final string to have the dynamic content wrapped in double quotes like so:

    'text contains "candy"'
    

Probably seems strange but the gem that I'm working with requires this.


回答1:


You can use %{text contains "#{search.query}"} if you don't want to escape the double quotes "text contains \"#{search.query}\"".




回答2:


'Hi, %{professor}, You have been invited for %{booking_type}. You may accept, reject or keep discussing more about this offer' % {professor: 'Mr. Ashaan', booking_type: 'Dinner'}



回答3:


Use

%q(text contains "#{search.query}") 

which means start the string with single quote. Or if you want to start with double quote use:

%Q(text contains '#{text}')


来源:https://stackoverflow.com/questions/14912486/interpolation-within-single-quotes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!