is it possible to escape character within single q

2019-09-05 23:53发布

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条回答
仙女界的扛把子
2楼-- · 2019-09-06 00:36

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

查看更多
登录 后发表回答