8

我可以在“ Ruby Strings ”和“ Escape sequences ”中找到有关 Ruby 转义序列的详细信息。但是,在官方 Ruby 文档中,我可以在哪里找到有关字符串转义序列的详细信息?

这个问题对于刚刚学习 Ruby 的人来说很重要,因为了解如何简单地浏览文档是一项初步挑战。

4

3 回答 3

15

请参阅官方文档(或在ruby​​-doc.org上),该文档已更新了支持的转义字符的完整列表:

\a             bell, ASCII 07h (BEL)
\b             backspace, ASCII 08h (BS)
\t             horizontal tab, ASCII 09h (TAB)
\n             newline (line feed), ASCII 0Ah (LF)
\v             vertical tab, ASCII 0Bh (VT)
\f             form feed, ASCII 0Ch (FF)
\r             carriage return, ASCII 0Dh (CR)
\e             escape, ASCII 1Bh (ESC)
\s             space, ASCII 20h (SPC)
\\             backslash, \
\nnn           octal bit pattern, where nnn is 1-3 octal digits ([0-7])
\xnn           hexadecimal bit pattern, where nn is 1-2 hexadecimal digits ([0-9a-fA-F])
\unnnn         Unicode character, where nnnn is exactly 4 hexadecimal digits ([0-9a-fA-F])
\u{nnnn ...}   Unicode character(s), where each nnnn is 1-6 hexadecimal digits ([0-9a-fA-F])
\cx or \C-x    control character, where x is an ASCII printable character
\M-x           meta character, where x is an ASCII printable character
\M-\C-x        meta control character, where x is an ASCII printable character
\M-\cx         same as above
\c\M-x         same as above
\c? or \C-?    delete, ASCII 7Fh (DEL)

如果您发现任何要添加或修复的内容,我们欢迎通过 GitHub 上的拉取请求或通过我们的问题跟踪器提供反馈和贡献。

于 2015-09-16T13:18:32.730 回答
3

虽然关于什么是“官方”文档的观点各不相同,但我会提供http://www.ruby-doc.org/core-2.0.0/doc/syntax/literals_rdoc.html#label-Strings作为您的答案问题。

于 2013-11-02T16:19:06.243 回答
1

以下是一些可能出现在双引号内的更常见的转义序列。

\"– 双引号 ( ")

\\– 单反斜杠 ( \)

\a– 铃声/警报(播放音调)

\b- 退格(删除前一个字符,将光标移回一个位置)

\r– 回车(将光标移动到行首)

\n- 换行(将光标移动到下一行)

\s– 空格 (  )

\t– 选项卡 ( )

于 2013-12-31T13:32:56.373 回答