Skip to content

Commit fbbed33

Browse files
committed
[DOC] Tweaks for StringIO#each_line
1 parent 3562c34 commit fbbed33

File tree

2 files changed

+188
-163
lines changed

2 files changed

+188
-163
lines changed

doc/stringio/each_line.md

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
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+
```
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+
```
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+
```
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+
**Arguments `sep` and `limit`**
73+
74+
With arguments `sep` and `limit` both given,
75+
honors both:
76+
77+
```
78+
strio = StringIO.new(TEXT)
79+
strio.each_line(' ', 10) {|line| p line }
80+
```
81+
82+
Output:
83+
84+
```
85+
"First "
86+
"line\nSecon"
87+
"d "
88+
"line\n\nFour"
89+
"th "
90+
"line\nFifth"
91+
" "
92+
"line\n"
93+
```
94+
95+
**Position**
96+
97+
As stated above, method `each` _remaining_ line in the stream.
98+
99+
In the examples above each `strio` object starts with its position at beginning-of-stream;
100+
but in other cases the position may be anywhere (see StringIO#pos):
101+
102+
```
103+
strio = StringIO.new(TEXT)
104+
strio.pos = 30 # Set stream position to character 30.
105+
strio.each_line {|line| p line }
106+
```
107+
108+
Output:
109+
110+
```
111+
" line\n"
112+
"Fifth line\n"
113+
```
114+
115+
In all the examples above, the stream position is at the beginning of a character;
116+
in other cases, that need not be so:
117+
118+
```ruby
119+
s = 'こんにちは' # Five 3-byte characters.
120+
strio = StringIO.new(s)
121+
strio.pos = 3 # At beginning of second character.
122+
strio.each_line {|line| p line }
123+
strio.pos = 4 # At second byte of second character.
124+
strio.each_line {|line| p line }
125+
strio.pos = 5 # At third byte of second character.
126+
strio.each_line {|line| p line }
127+
```
128+
129+
Output:
130+
131+
```ruby
132+
"んにちは"
133+
"\x82\x93にちは"
134+
"\x93にちは"
135+
```
136+
137+
**Special Record Separators**
138+
139+
Like some methods in class `IO`, StringIO.each honors two special record separators;
140+
see {Special Line Separators}[https://docs.ruby-lang.org/en/master/IO.html#class-IO-label-Special+Line+Separator+Values].
141+
142+
```
143+
strio = StringIO.new(TEXT)
144+
strio.each_line('') {|line| p line } # Read as paragraphs (separated by blank lines).
145+
```
146+
147+
Output:
148+
149+
```
150+
"First line\nSecond line\n\n"
151+
"Fourth line\nFifth line\n"
152+
```
153+
154+
```
155+
strio = StringIO.new(TEXT)
156+
strio.each_line(nil) {|line| p line } # "Slurp"; read it all.
157+
```
158+
159+
Output:
160+
161+
```
162+
"First line\nSecond line\n\nFourth line\nFifth line\n"
163+
```
164+
165+
**Keyword Argument `chomp`**
166+
167+
With keyword argument `chomp` given as `true` (the default is `false`),
168+
removes trailing newline (if any) from each line:
169+
170+
```
171+
strio = StringIO.new(TEXT)
172+
strio.each_line(chomp: true) {|line| p line }
173+
```
174+
175+
Output:
176+
177+
```
178+
"First line"
179+
"Second line"
180+
""
181+
"Fourth line"
182+
"Fifth line"
183+
```
184+
185+
With no block given, returns a new {Enumerator}[https://docs.ruby-lang.org/en/master/Enumerator.html].
186+
187+
Related: StringIO.each_byte, StringIO.each_char, StringIO.each_codepoint.

ext/stringio/stringio.c

Lines changed: 1 addition & 163 deletions
Original file line numberDiff line numberDiff line change
@@ -1466,170 +1466,8 @@ strio_readline(int argc, VALUE *argv, VALUE self)
14661466
* each_line(limit, chomp: false) {|line| ... } -> self
14671467
* each_line(sep, limit, chomp: false) {|line| ... } -> self
14681468
*
1469-
* With a block given calls the block with each remaining line (see "Position" below) in the stream;
1470-
* returns `self`.
1469+
* :include: stringio/each_line.md
14711470
*
1472-
* Leaves stream position as end-of-stream.
1473-
*
1474-
* **No Arguments**
1475-
*
1476-
* With no arguments given,
1477-
* reads lines using the default record separator global variable `$/`, whose initial value is `"\n"`.
1478-
*
1479-
* ```
1480-
* strio = StringIO.new(TEXT)
1481-
* strio.each_line {|line| p line }
1482-
* strio.eof? # => true
1483-
* ```
1484-
*
1485-
* Output:
1486-
*
1487-
* ```
1488-
* "First line\n"
1489-
* "Second line\n"
1490-
* "\n"
1491-
* "Fourth line\n"
1492-
* "Fifth line\n"
1493-
* ```
1494-
*
1495-
* **Argument `sep`**
1496-
*
1497-
* With only string argument `sep` given,
1498-
* reads lines using that string as the record separator:
1499-
*
1500-
* ```
1501-
* strio = StringIO.new(TEXT)
1502-
* strio.each_line(' ') {|line| p line }
1503-
* ```
1504-
*
1505-
* Output:
1506-
*
1507-
* ```
1508-
* "First "
1509-
* "line\nSecond "
1510-
* "line\n\nFourth "
1511-
* "line\nFifth "
1512-
* "line\n"
1513-
* ```
1514-
*
1515-
* **Argument `limit`**
1516-
*
1517-
* With only integer argument `limit` given,
1518-
* reads lines using the default record separator global variable `$/`, whose initial value is `"\n"`;
1519-
* also limits the size (in characters) of each line to the given limit:
1520-
*
1521-
* ```
1522-
* strio = StringIO.new(TEXT)
1523-
* strio.each_line(10) {|line| p line }
1524-
* ```
1525-
*
1526-
* Output:
1527-
*
1528-
* ```
1529-
* "First line"
1530-
* "\n"
1531-
* "Second lin"
1532-
* "e\n"
1533-
* "\n"
1534-
* "Fourth lin"
1535-
* "e\n"
1536-
* "Fifth line"
1537-
* "\n"
1538-
* ```
1539-
* **Arguments `sep` and `limit`**
1540-
*
1541-
* With arguments `sep` and `limit` both given,
1542-
* honors both:
1543-
*
1544-
* ```
1545-
* strio = StringIO.new(TEXT)
1546-
* strio.each_line(' ', 10) {|line| p line }
1547-
* ```
1548-
*
1549-
* Output:
1550-
*
1551-
* ```
1552-
* "First "
1553-
* "line\nSecon"
1554-
* "d "
1555-
* "line\n\nFour"
1556-
* "th "
1557-
* "line\nFifth"
1558-
* " "
1559-
* "line\n"
1560-
* ```
1561-
*
1562-
* **Position**
1563-
*
1564-
* As stated above, method `each` _remaining_ line in the stream.
1565-
*
1566-
* In the examples above each `strio` object starts with its position at beginning-of-stream;
1567-
* but in other cases the position may be anywhere (see StringIO#pos):
1568-
*
1569-
* ```
1570-
* strio = StringIO.new(TEXT)
1571-
* strio.pos = 30 # Set stream position to character 30.
1572-
* strio.each_line {|line| p line }
1573-
* ```
1574-
*
1575-
* Output:
1576-
*
1577-
* ```
1578-
* " line\n"
1579-
* "Fifth line\n"
1580-
* ```
1581-
*
1582-
* **Special Record Separators**
1583-
*
1584-
* Like some methds in class `IO`, StringIO.each honors two special record separators;
1585-
* see {Special Line Separators}[https://docs.ruby-lang.org/en/master/IO.html#class-IO-label-Special+Line+Separator+Values].
1586-
*
1587-
* ```
1588-
* strio = StringIO.new(TEXT)
1589-
* strio.each_line('') {|line| p line } # Read as paragraphs (separated by blank lines).
1590-
* ```
1591-
*
1592-
* Output:
1593-
*
1594-
* ```
1595-
* "First line\nSecond line\n\n"
1596-
* "Fourth line\nFifth line\n"
1597-
* ```
1598-
*
1599-
* ```
1600-
* strio = StringIO.new(TEXT)
1601-
* strio.each_line(nil) {|line| p line } # "Slurp"; read it all.
1602-
* ```
1603-
*
1604-
* Output:
1605-
*
1606-
* ```
1607-
* "First line\nSecond line\n\nFourth line\nFifth line\n"
1608-
* ```
1609-
*
1610-
* **Keyword Argument `chomp`**
1611-
*
1612-
* With keyword argument `chomp` given as `true` (the default is `false`),
1613-
* removes trailing newline (if any) from each line:
1614-
*
1615-
* ```
1616-
* strio = StringIO.new(TEXT)
1617-
* strio.each_line(chomp: true) {|line| p line }
1618-
* ```
1619-
*
1620-
* Output:
1621-
*
1622-
* ```
1623-
* "First line"
1624-
* "Second line"
1625-
* ""
1626-
* "Fourth line"
1627-
* "Fifth line"
1628-
* ```
1629-
*
1630-
* With no block given, returns a new {Enumerator}[https://docs.ruby-lang.org/en/master/Enumerator.html].
1631-
*
1632-
* Related: StringIO.each_byte, StringIO.each_char, StringIO.each_codepoint.
16331471
*/
16341472
static VALUE
16351473
strio_each(int argc, VALUE *argv, VALUE self)

0 commit comments

Comments
 (0)