如何从字符串插值中转义#{ [英] How do I escape #{ from string interpolation

查看:135
本文介绍了如何从字符串插值中转义#{的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个heredoc,正在使用#{}内插其他字符串,但是有一个实例,我也想在我的heredoc中编写实际的文本#{some_ruby_stuff},但不进行内插.有没有办法逃避#{.

I have a heredoc where I am using #{} to interpolate some other strings, but there is an instance where I also want to write the actual text #{some_ruby_stuff} in my heredoc, WITHOUT it being interpolated. Is there a way to escape the #{.

我尝试过"\",但是没有运气.尽管它转义了#{},但它还包含"\":

I've tried "\", but no luck. Although it escapes the #{}, it also includes the "\":

>> <<-END
 #{RAILS_ENV} \#{RAILS_ENV} 
END
=> " development \#{RAILS_ENV}\n"

推荐答案

我认为反斜杠哈希只是Ruby在某些基于irb的方式中有所帮助.

I think the backslash-hash is just Ruby being helpful in some irb-only way.

>> a,b = 1,2        #=> [1, 2]
>> s = "#{a} \#{b}" #=> "1 \#{b}"
>> puts s           #=> 1 #{b}
>> s.size           #=> 6

所以我认为您已经有了正确的答案.

So I think you already have the correct answer.

这篇关于如何从字符串插值中转义#{的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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