是否有可能在ruby中用单引号转义字符?

时间:2022-09-15 15:35:18

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

我刚刚在面试中遇到了这个问题。是否有可能在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 个解决方案

#1


3  

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

需要在单引号字符串中转义的唯一字符是'\\'(对于反斜杠\)和'\''(对于单引号'本身)。

#1


3  

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

需要在单引号字符串中转义的唯一字符是'\\'(对于反斜杠\)和'\''(对于单引号'本身)。