From dbfd0973d3f2810c6ee490f0bc84a35d6440a3f1 Mon Sep 17 00:00:00 2001 From: Burdette Lamar Date: Tue, 26 Aug 2025 13:27:42 -0500 Subject: [PATCH] [DOC] Tweaks for String#inspect --- doc/string/inspect.rdoc | 39 +++++++++++++++++++++++++++++++++++++++ string.c | 7 +------ 2 files changed, 40 insertions(+), 6 deletions(-) create mode 100644 doc/string/inspect.rdoc diff --git a/doc/string/inspect.rdoc b/doc/string/inspect.rdoc new file mode 100644 index 0000000000..828ecf966d --- /dev/null +++ b/doc/string/inspect.rdoc @@ -0,0 +1,39 @@ +Returns a printable version of +self+, enclosed in double-quotes. + +Most printable characters are rendered simply as themselves: + + 'abc'.inspect # => "\"abc\"" + '012'.inspect # => "\"012\"" + ''.inspect # => "\"\"" + "\u000012".inspect # => "\"\\u000012\"" + 'тест'.inspect # => "\"тест\"" + 'こんにちは'.inspect # => "\"こんにちは\"" + +But printable characters double-quote ('"') and backslash and ('\\') are escaped: + + '"'.inspect # => "\"\\\"\"" + '\\'.inspect # => "\"\\\\\"" + +Unprintable characters are the {ASCII characters}[https://en.wikipedia.org/wiki/ASCII] +whose values are in range 0..31, +along with the character whose value is +127+. + +Most of these characters are rendered thus: + + 0.chr.inspect # => "\"\\x00\"" + 1.chr.inspect # => "\"\\x01\"" + 2.chr.inspect # => "\"\\x02\"" + # ... + +A few, however, have special renderings: + + 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}[rdoc-ref:String@Converting+to+Non--5CString]. diff --git a/string.c b/string.c index 709ada47db..01cfc9c175 100644 --- a/string.c +++ b/string.c @@ -7303,12 +7303,7 @@ rb_str_escape(VALUE str) * call-seq: * inspect -> string * - * Returns a printable version of +self+, enclosed in double-quotes, - * and with special characters escaped: - * - * s = "foo\tbar\tbaz\n" - * s.inspect - * # => "\"foo\\tbar\\tbaz\\n\"" + * :include: doc/string/inspect.rdoc * */