Skip to content

Commit e221a4b

Browse files
BurdetteLamarmatzbot
authored andcommitted
[ruby/stringio] [DOC] Doc for StringIO#each
(ruby/stringio#154) ruby/stringio@eca2588274
1 parent 2dd01c8 commit e221a4b

File tree

1 file changed

+187
-6
lines changed

1 file changed

+187
-6
lines changed

ext/stringio/stringio.c

Lines changed: 187 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1462,15 +1462,177 @@ strio_readline(int argc, VALUE *argv, VALUE self)
14621462
}
14631463

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

19712133
/*
2134+
* :markup: markdown
2135+
*
19722136
* \IO streams for strings, with access similar to
19732137
* {IO}[rdoc-ref:IO];
19742138
* see {IO}[rdoc-ref:IO].
19752139
*
1976-
* === About the Examples
2140+
* ### About the Examples
19772141
*
19782142
* Examples on this page assume that \StringIO has been required:
19792143
*
1980-
* require 'stringio'
2144+
* ```
2145+
* require 'stringio'
2146+
* ```
2147+
*
2148+
* And that these constants have been defined:
2149+
*
2150+
* ```
2151+
* TEXT = <<EOT
2152+
* First line
2153+
* Second line
2154+
*
2155+
* Fourth line
2156+
* Fifth line
2157+
* EOT
2158+
*
2159+
* RUSSIAN = 'тест'
2160+
* DATA = "\u9990\u9991\u9992\u9993\u9994"
2161+
* ```
19812162
*
19822163
*/
19832164
void

0 commit comments

Comments
 (0)