Skip to content
instance method

String#inspect

() -> ::String
inspect → string

Returns a printable version of self, enclosed in double-quotes.

Most printable characters are rendered simply as themselves:

Example
'abc'.inspect        # => "\"abc\""
'012'.inspect        # => "\"012\""
''.inspect           # => "\"\""
"\u000012".inspect   # => "\"\\u000012\""
'тест'.inspect       # => "\"тест\""
'こんにちは'.inspect  # => "\"こんにちは\""

But printable characters double-quote ('"') and backslash and ('\') are escaped:

Example
'"'.inspect  # => "\"\\\"\""
'\\'.inspect # => "\"\\\\\""

Unprintable characters are the ASCII characters whose values are in range 0..31, along with the character whose value is 127.

Most of these characters are rendered thus:

Example
0.chr.inspect # => "\"\\x00\""
1.chr.inspect # => "\"\\x01\""
2.chr.inspect # => "\"\\x02\""
# ...

A few, however, have special renderings:

Example
7.chr.inspect  # => "\"\\a\""  # BEL
8.chr.inspect  # => "\"\\b\""  # BS
9.chr.inspect  # => "\"\\t\""  # TAB
10.chr.inspect # => "\"\\n\""  # LF
11.chr.inspect # => "\"\\v\""  # VT
12.chr.inspect # => "\"\\f\""  # FF
13.chr.inspect # => "\"\\r\""  # CR
27.chr.inspect # => "\"\\e\""  # ESC

Related: see Converting to Non-String.

See also #insert #intern #initialize_copy #length also in 4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0