-
Notifications
You must be signed in to change notification settings - Fork 5.4k
[DOC] Tweaks for String#dump #13883
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
[DOC] Tweaks for String#dump #13883
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
Returns a printable version of +self+, enclosed in double-quotes: | ||
|
||
'hello'.dump # => "\"hello\"" | ||
'тест'.dump # => "\"\\u0442\\u0435\\u0441\\u0442\"" | ||
'こんにちは'.dump # => "\"\\u3053\\u3093\\u306B\\u3061\\u306F\"" | ||
|
||
The format of the result depends on the encoding of the string: | ||
|
||
s = 'hello' | ||
s.encoding # => #<Encoding:UTF-8> | ||
s.dump # => "\"hello\"" | ||
s.encode('utf-16').dump # => "\"\\xFE\\xFF\\x00h\\x00e\\x00l\\x00l\\x00o\".dup.force_encoding(\"UTF-16\")" | ||
s.encode('utf-16le').dump # => "\"h\\x00e\\x00l\\x00l\\x00o\\x00\".dup.force_encoding(\"UTF-16LE\")" | ||
|
||
s = 'тест' | ||
s.encoding # => #<Encoding:UTF-8> | ||
s.dump # => "\"\\u0442\\u0435\\u0441\\u0442\"" | ||
s.encode('utf-16').dump # => "\"\\xFE\\xFF\\x04B\\x045\\x04A\\x04B\".dup.force_encoding(\"UTF-16\")" | ||
s.encode('utf-16le').dump # => "\"B\\x045\\x04A\\x04B\\x04\".dup.force_encoding(\"UTF-16LE\")" | ||
|
||
s = 'こんにちは' | ||
s.encoding # => #<Encoding:UTF-8> | ||
s.dump # => "\"\\u3053\\u3093\\u306B\\u3061\\u306F\"" | ||
s.encode('utf-16').dump # => "\"\\xFE\\xFF0S0\\x930k0a0o\".dup.force_encoding(\"UTF-16\")" | ||
s.encode('utf-16le').dump # => "\"S0\\x930k0a0o0\".dup.force_encoding(\"UTF-16LE\")" | ||
|
||
Certain special characters are rendered with escapes: | ||
|
||
'"'.dump # => "\"\\\"\"" | ||
'\\'.dump # => "\"\\\\\"" | ||
|
||
Non-printing characters are rendered with escapes: | ||
|
||
s = '' | ||
s << 7 # Alarm (bell). | ||
s << 8 # Back space. | ||
s << 9 # Horizontal tab. | ||
s << 10 # Line feed. | ||
s << 11 # Vertical tab. | ||
s << 12 # Form feed. | ||
s << 13 # Carriage return. | ||
s # => "\a\b\t\n\v\f\r" | ||
s.dump # => "\"\\a\\b\\t\\n\\v\\f\\r\"" | ||
|
||
Related: see {Converting to New String}[rdoc-ref:String@Converting+to+New+String]. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be better to move the examples of non-UTF8 encodings to a separate section with some text describing it (e.g. using hexadecimal format and adding
dup.force_encoding(<encoding name>)
. This is because non-UTF8 is more of an edge case rather than a commonly used case.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've moved the cited lines to the end. I think you want other changes, but I'm not sure what exactly is needed. Can you fix up one, as a guide for me?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@peterzhu2118, I'll take another shot at this; marking as Draft in the interim.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@peterzhu2118, I take it back. I don't know what to do with this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I opened #13965