Skip to content

Commit 21c78cb

Browse files
committed
[DOC] Docs for String#dump
1 parent 3bbdcf0 commit 21c78cb

2 files changed

Lines changed: 54 additions & 9 deletions

File tree

doc/string/dump.rdoc

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
Returns a printable version of +self+, enclosed in double-quotes:
2+
3+
'hello'.dump # => "\"hello\""
4+
5+
Certain special characters are rendered with escapes:
6+
7+
'"'.dump # => "\"\\\"\""
8+
'\\'.dump # => "\"\\\\\""
9+
10+
Non-printing characters are rendered with escapes:
11+
12+
s = ''
13+
s << 7 # Alarm (bell).
14+
s << 8 # Back space.
15+
s << 9 # Horizontal tab.
16+
s << 10 # Line feed.
17+
s << 11 # Vertical tab.
18+
s << 12 # Form feed.
19+
s << 13 # Carriage return.
20+
s # => "\a\b\t\n\v\f\r"
21+
s.dump # => "\"\\a\\b\\t\\n\\v\\f\\r\""
22+
23+
If +self+ is encoded in UTF-8 and contains Unicode characters, renders Unicode
24+
characters in Unicode escape sequence:
25+
26+
'тест'.dump # => "\"\\u0442\\u0435\\u0441\\u0442\""
27+
'こんにちは'.dump # => "\"\\u3053\\u3093\\u306B\\u3061\\u306F\""
28+
29+
If the encoding of +self+ is not ASCII-compatible (i.e., +self.encoding.ascii_compatible?+
30+
returns +false+), renders all ASCII-compatible bytes as ASCII characters and all
31+
other bytes as hexadecimal. Appends <tt>.dup.force_encoding(\"encoding\")</tt>, where
32+
<tt><encoding></tt> is +self.encoding.name+:
33+
34+
s = 'hello'
35+
s.encoding # => #<Encoding:UTF-8>
36+
s.dump # => "\"hello\""
37+
s.encode('utf-16').dump # => "\"\\xFE\\xFF\\x00h\\x00e\\x00l\\x00l\\x00o\".dup.force_encoding(\"UTF-16\")"
38+
s.encode('utf-16le').dump # => "\"h\\x00e\\x00l\\x00l\\x00o\\x00\".dup.force_encoding(\"UTF-16LE\")"
39+
40+
s = 'тест'
41+
s.encoding # => #<Encoding:UTF-8>
42+
s.dump # => "\"\\u0442\\u0435\\u0441\\u0442\""
43+
s.encode('utf-16').dump # => "\"\\xFE\\xFF\\x04B\\x045\\x04A\\x04B\".dup.force_encoding(\"UTF-16\")"
44+
s.encode('utf-16le').dump # => "\"B\\x045\\x04A\\x04B\\x04\".dup.force_encoding(\"UTF-16LE\")"
45+
46+
s = 'こんにちは'
47+
s.encoding # => #<Encoding:UTF-8>
48+
s.dump # => "\"\\u3053\\u3093\\u306B\\u3061\\u306F\""
49+
s.encode('utf-16').dump # => "\"\\xFE\\xFF0S0\\x930k0a0o\".dup.force_encoding(\"UTF-16\")"
50+
s.encode('utf-16le').dump # => "\"S0\\x930k0a0o0\".dup.force_encoding(\"UTF-16LE\")"
51+
52+
Related: see {Converting to New String}[rdoc-ref:String@Converting+to+New+String].

string.c

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7413,16 +7413,9 @@ rb_str_inspect(VALUE str)
74137413

74147414
/*
74157415
* call-seq:
7416-
* dump -> string
7416+
* dump -> new_string
74177417
*
7418-
* Returns a printable version of +self+, enclosed in double-quotes,
7419-
* with special characters escaped, and with non-printing characters
7420-
* replaced by hexadecimal notation:
7421-
*
7422-
* "hello \n ''".dump # => "\"hello \\n ''\""
7423-
* "\f\x00\xff\\\"".dump # => "\"\\f\\x00\\xFF\\\\\\\"\""
7424-
*
7425-
* Related: String#undump (inverse of String#dump).
7418+
* :include: doc/string/dump.rdoc
74267419
*
74277420
*/
74287421

0 commit comments

Comments
 (0)