|
| 1 | +With a block given calls the block with each remaining line (see "Position" below) in the stream; |
| 2 | +returns `self`. |
| 3 | + |
| 4 | +Leaves stream position at end-of-stream. |
| 5 | + |
| 6 | +**No Arguments** |
| 7 | + |
| 8 | +With no arguments given, |
| 9 | +reads lines using the default record separator |
| 10 | +(global variable `$/`, whose initial value is `"\n"`). |
| 11 | + |
| 12 | +```ruby |
| 13 | +strio = StringIO.new(TEXT) |
| 14 | +strio.each_line {|line| p line } |
| 15 | +strio.eof? # => true |
| 16 | +``` |
| 17 | + |
| 18 | +Output: |
| 19 | + |
| 20 | +``` |
| 21 | +"First line\n" |
| 22 | +"Second line\n" |
| 23 | +"\n" |
| 24 | +"Fourth line\n" |
| 25 | +"Fifth line\n" |
| 26 | +``` |
| 27 | + |
| 28 | +**Argument `sep`** |
| 29 | + |
| 30 | +With only string argument `sep` given, |
| 31 | +reads lines using that string as the record separator: |
| 32 | + |
| 33 | +```ruby |
| 34 | +strio = StringIO.new(TEXT) |
| 35 | +strio.each_line(' ') {|line| p line } |
| 36 | +``` |
| 37 | + |
| 38 | +Output: |
| 39 | + |
| 40 | +``` |
| 41 | +"First " |
| 42 | +"line\nSecond " |
| 43 | +"line\n\nFourth " |
| 44 | +"line\nFifth " |
| 45 | +"line\n" |
| 46 | +``` |
| 47 | + |
| 48 | +**Argument `limit`** |
| 49 | + |
| 50 | +With only integer argument `limit` given, |
| 51 | +reads lines using the default record separator; |
| 52 | +also limits the size (in characters) of each line to the given limit: |
| 53 | + |
| 54 | +```ruby |
| 55 | +strio = StringIO.new(TEXT) |
| 56 | +strio.each_line(10) {|line| p line } |
| 57 | +``` |
| 58 | + |
| 59 | +Output: |
| 60 | + |
| 61 | +``` |
| 62 | +"First line" |
| 63 | +"\n" |
| 64 | +"Second lin" |
| 65 | +"e\n" |
| 66 | +"\n" |
| 67 | +"Fourth lin" |
| 68 | +"e\n" |
| 69 | +"Fifth line" |
| 70 | +"\n" |
| 71 | +``` |
| 72 | + |
| 73 | +**Arguments `sep` and `limit`** |
| 74 | + |
| 75 | +With arguments `sep` and `limit` both given, |
| 76 | +honors both: |
| 77 | + |
| 78 | +```ruby |
| 79 | +strio = StringIO.new(TEXT) |
| 80 | +strio.each_line(' ', 10) {|line| p line } |
| 81 | +``` |
| 82 | + |
| 83 | +Output: |
| 84 | + |
| 85 | +``` |
| 86 | +"First " |
| 87 | +"line\nSecon" |
| 88 | +"d " |
| 89 | +"line\n\nFour" |
| 90 | +"th " |
| 91 | +"line\nFifth" |
| 92 | +" " |
| 93 | +"line\n" |
| 94 | +``` |
| 95 | + |
| 96 | +**Position** |
| 97 | + |
| 98 | +As stated above, method `each` _remaining_ line in the stream. |
| 99 | + |
| 100 | +In the examples above each `strio` object starts with its position at beginning-of-stream; |
| 101 | +but in other cases the position may be anywhere (see StringIO#pos): |
| 102 | + |
| 103 | +```ruby |
| 104 | +strio = StringIO.new(TEXT) |
| 105 | +strio.pos = 30 # Set stream position to character 30. |
| 106 | +strio.each_line {|line| p line } |
| 107 | +``` |
| 108 | + |
| 109 | +Output: |
| 110 | + |
| 111 | +``` |
| 112 | +" line\n" |
| 113 | +"Fifth line\n" |
| 114 | +``` |
| 115 | + |
| 116 | +In all the examples above, the stream position is at the beginning of a character; |
| 117 | +in other cases, that need not be so: |
| 118 | + |
| 119 | +```ruby |
| 120 | +s = 'こんにちは' # Five 3-byte characters. |
| 121 | +strio = StringIO.new(s) |
| 122 | +strio.pos = 3 # At beginning of second character. |
| 123 | +strio.each_line {|line| p line } |
| 124 | +strio.pos = 4 # At second byte of second character. |
| 125 | +strio.each_line {|line| p line } |
| 126 | +strio.pos = 5 # At third byte of second character. |
| 127 | +strio.each_line {|line| p line } |
| 128 | +``` |
| 129 | + |
| 130 | +Output: |
| 131 | + |
| 132 | +``` |
| 133 | +"んにちは" |
| 134 | +"\x82\x93にちは" |
| 135 | +"\x93にちは" |
| 136 | +``` |
| 137 | + |
| 138 | +**Special Record Separators** |
| 139 | + |
| 140 | +Like some methods in class `IO`, StringIO.each honors two special record separators; |
| 141 | +see {Special Line Separators}[https://docs.ruby-lang.org/en/master/IO.html#class-IO-label-Special+Line+Separator+Values]. |
| 142 | + |
| 143 | +```ruby |
| 144 | +strio = StringIO.new(TEXT) |
| 145 | +strio.each_line('') {|line| p line } # Read as paragraphs (separated by blank lines). |
| 146 | +``` |
| 147 | + |
| 148 | +Output: |
| 149 | + |
| 150 | +``` |
| 151 | +"First line\nSecond line\n\n" |
| 152 | +"Fourth line\nFifth line\n" |
| 153 | +``` |
| 154 | + |
| 155 | +```ruby |
| 156 | +strio = StringIO.new(TEXT) |
| 157 | +strio.each_line(nil) {|line| p line } # "Slurp"; read it all. |
| 158 | +``` |
| 159 | + |
| 160 | +Output: |
| 161 | + |
| 162 | +``` |
| 163 | +"First line\nSecond line\n\nFourth line\nFifth line\n" |
| 164 | +``` |
| 165 | + |
| 166 | +**Keyword Argument `chomp`** |
| 167 | + |
| 168 | +With keyword argument `chomp` given as `true` (the default is `false`), |
| 169 | +removes trailing newline (if any) from each line: |
| 170 | + |
| 171 | +```ruby |
| 172 | +strio = StringIO.new(TEXT) |
| 173 | +strio.each_line(chomp: true) {|line| p line } |
| 174 | +``` |
| 175 | + |
| 176 | +Output: |
| 177 | + |
| 178 | +``` |
| 179 | +"First line" |
| 180 | +"Second line" |
| 181 | +"" |
| 182 | +"Fourth line" |
| 183 | +"Fifth line" |
| 184 | +``` |
| 185 | + |
| 186 | +With no block given, returns a new {Enumerator}[https://docs.ruby-lang.org/en/master/Enumerator.html]. |
| 187 | + |
| 188 | + |
| 189 | +Related: StringIO.each_byte, StringIO.each_char, StringIO.each_codepoint. |
0 commit comments