is it possible to escape character within single quotes in ruby?

不打扰是莪最后的温柔 提交于 2019-12-25 03:29:08

问题


I just faced this question in interview. Is it possible to escape character within single quotes in ruby?

The confusion is in following code

puts '\\'   # Output: \
puts '\n'   # Output: \n

It seems that backword slash is escaped but the newline character isn't.

I am aware of this question but I am not asking about difference between single and double quote. I am asking about whether it's possible to escape characters in single quotes or not? And why only backslash is allowed to escape?


回答1:


The only characters that needs to be escaped in a single quoted string are '\\' (for backslash \) and '\'' (for single quote ' itself).



来源:https://stackoverflow.com/questions/25499046/is-it-possible-to-escape-character-within-single-quotes-in-ruby

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