简体   繁体   中英

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

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM