summaryrefslogtreecommitdiff
path: root/doc/stringio/each_line.md
blob: e29640a12a9203fe0073a1be32e7ab306d402d8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
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"`).

```ruby
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:

```ruby
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:

```ruby
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:

```ruby
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):

```ruby
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:

```
"んにちは"
"\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].

```ruby
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"
```

```ruby
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:

```ruby
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.