-
Notifications
You must be signed in to change notification settings - Fork 36
[DOC] Tweaks for StringIO#each_line #165
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,187 @@ | ||
| With a block given calls the block with each remaining line (see "Position" below) in the stream; | ||
| returns `self`. | ||
|
|
||
| Leaves stream position at end-of-stream. | ||
|
|
||
| **No Arguments** | ||
|
|
||
| With no arguments given, | ||
| reads lines using the default record separator | ||
| (global variable `$/`, whose initial value is `"\n"`). | ||
|
|
||
| ``` | ||
| strio = StringIO.new(TEXT) | ||
| strio.each_line {|line| p line } | ||
| strio.eof? # => true | ||
| ``` | ||
|
|
||
| Output: | ||
|
|
||
| ``` | ||
| "First line\n" | ||
| "Second line\n" | ||
| "\n" | ||
| "Fourth line\n" | ||
| "Fifth line\n" | ||
| ``` | ||
|
|
||
| **Argument `sep`** | ||
|
|
||
| With only string argument `sep` given, | ||
| reads lines using that string as the record separator: | ||
|
|
||
| ``` | ||
| strio = StringIO.new(TEXT) | ||
| strio.each_line(' ') {|line| p line } | ||
| ``` | ||
|
|
||
| Output: | ||
|
|
||
| ``` | ||
| "First " | ||
| "line\nSecond " | ||
| "line\n\nFourth " | ||
| "line\nFifth " | ||
| "line\n" | ||
| ``` | ||
|
|
||
| **Argument `limit`** | ||
|
|
||
| With only integer argument `limit` given, | ||
| reads lines using the default record separator; | ||
| also limits the size (in characters) of each line to the given limit: | ||
|
|
||
| ``` | ||
| strio = StringIO.new(TEXT) | ||
| strio.each_line(10) {|line| p line } | ||
| ``` | ||
|
|
||
| Output: | ||
|
|
||
| ``` | ||
| "First line" | ||
| "\n" | ||
| "Second lin" | ||
| "e\n" | ||
| "\n" | ||
| "Fourth lin" | ||
| "e\n" | ||
| "Fifth line" | ||
| "\n" | ||
| ``` | ||
| **Arguments `sep` and `limit`** | ||
|
|
||
| With arguments `sep` and `limit` both given, | ||
| honors both: | ||
|
|
||
| ``` | ||
| strio = StringIO.new(TEXT) | ||
| strio.each_line(' ', 10) {|line| p line } | ||
| ``` | ||
|
|
||
| Output: | ||
|
|
||
| ``` | ||
| "First " | ||
| "line\nSecon" | ||
| "d " | ||
| "line\n\nFour" | ||
| "th " | ||
| "line\nFifth" | ||
| " " | ||
| "line\n" | ||
| ``` | ||
|
|
||
| **Position** | ||
|
|
||
| As stated above, method `each` _remaining_ line in the stream. | ||
|
|
||
| In the examples above each `strio` object starts with its position at beginning-of-stream; | ||
| but in other cases the position may be anywhere (see StringIO#pos): | ||
|
|
||
| ``` | ||
| strio = StringIO.new(TEXT) | ||
| strio.pos = 30 # Set stream position to character 30. | ||
| strio.each_line {|line| p line } | ||
| ``` | ||
|
|
||
| Output: | ||
|
|
||
| ``` | ||
| " line\n" | ||
| "Fifth line\n" | ||
| ``` | ||
|
|
||
| In all the examples above, the stream position is at the beginning of a character; | ||
| in other cases, that need not be so: | ||
|
|
||
| ```ruby | ||
| s = 'こんにちは' # Five 3-byte characters. | ||
| strio = StringIO.new(s) | ||
| strio.pos = 3 # At beginning of second character. | ||
| strio.each_line {|line| p line } | ||
| strio.pos = 4 # At second byte of second character. | ||
| strio.each_line {|line| p line } | ||
| strio.pos = 5 # At third byte of second character. | ||
| strio.each_line {|line| p line } | ||
| ``` | ||
|
|
||
| Output: | ||
|
|
||
| ```ruby | ||
|
BurdetteLamar marked this conversation as resolved.
Outdated
|
||
| "んにちは" | ||
| "\x82\x93にちは" | ||
| "\x93にちは" | ||
| ``` | ||
|
|
||
| **Special Record Separators** | ||
|
|
||
| Like some methods in class `IO`, StringIO.each honors two special record separators; | ||
| see {Special Line Separators}[https://docs.ruby-lang.org/en/master/IO.html#class-IO-label-Special+Line+Separator+Values]. | ||
|
|
||
| ``` | ||
| strio = StringIO.new(TEXT) | ||
| strio.each_line('') {|line| p line } # Read as paragraphs (separated by blank lines). | ||
| ``` | ||
|
|
||
| Output: | ||
|
|
||
| ``` | ||
| "First line\nSecond line\n\n" | ||
| "Fourth line\nFifth line\n" | ||
| ``` | ||
|
|
||
| ``` | ||
| strio = StringIO.new(TEXT) | ||
| strio.each_line(nil) {|line| p line } # "Slurp"; read it all. | ||
| ``` | ||
|
|
||
| Output: | ||
|
|
||
| ``` | ||
| "First line\nSecond line\n\nFourth line\nFifth line\n" | ||
| ``` | ||
|
|
||
| **Keyword Argument `chomp`** | ||
|
|
||
| With keyword argument `chomp` given as `true` (the default is `false`), | ||
| removes trailing newline (if any) from each line: | ||
|
|
||
| ``` | ||
| strio = StringIO.new(TEXT) | ||
| strio.each_line(chomp: true) {|line| p line } | ||
| ``` | ||
|
|
||
| Output: | ||
|
|
||
| ``` | ||
| "First line" | ||
| "Second line" | ||
| "" | ||
| "Fourth line" | ||
| "Fifth line" | ||
| ``` | ||
|
|
||
| With no block given, returns a new {Enumerator}[https://docs.ruby-lang.org/en/master/Enumerator.html]. | ||
|
|
||
| Related: StringIO.each_byte, StringIO.each_char, StringIO.each_codepoint. | ||
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.
Uh oh!
There was an error while loading. Please reload this page.