反斜杠在红宝石中打破字符串插值 [英] Backslash breaks string interpolation in ruby

查看:129
本文介绍了反斜杠在红宝石中打破字符串插值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想最终得到字符串/ a_setting / c\blah = something,其中属性被评估为它的值:blah。但是,我看到以下行为,其中前面的反斜杠似乎停止对变量的评估:

I'd like to end up with the string "/a_setting/c\blah = something" where attribute gets evaluated to its value: blah. However, I'm seeing the following behavior where the preceding backslash seems to stop the evaluation of the variable:


attribute = blah

/ a_setting / c\#{attribute} = something

=> / a_setting / c\#{attribute} = something

/ a_setting / c\#{attribute} = something

=> / a_setting / c blah = something

推荐答案

要获取字符串想要:

"/a_setting/c\\#{attribute} = something"

您需要通过反斜杠转义反斜杠。

You need to escape the backslash by backslash.


  • 当您执行\#时,将被转义,并被解释为不插入元素,但是作为逐字 c>#,在检查中,在前面显示为\# {...} 以避免内插模糊。

  • 当您执行\ / code>,(被冗余地)转义,被解释为逐字

  • When you do "\#", the "#" is escaped, and is interpreted not as an interpolation element, but as the verbatim "#", which in inspection, appears as "\#" in front of {...} to avoid ambiguity with interpolation.
  • When you do "\ ", the " " is (redundantly) escaped, and is interpreted as the verbatim " ".

这篇关于反斜杠在红宝石中打破字符串插值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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