爲什麼會返回"\x21"
? \x
表示法是爲非打印字符保留的,但由於\x21
等於!
,所以顯示內容。
inspect
版本的字符串一般是最可讀的它的版本,而不是最字面的。
同樣:
"!".ord
# => 33
"\x21".ord
# => 33
"\x21".ord.to_s(16)
# => "21"
33.chr
# => "!"
有一些特殊字符documented in the Strings section:
\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