Skip to content

Commit e8b66f8

Browse files
[DOC] Doc for StringIO#read (#197)
Previous doc merely linked to `IO#read`; new doc stays local, shows examples using `StringIO`.
1 parent 8983f32 commit e8b66f8

File tree

2 files changed

+86
-2
lines changed

2 files changed

+86
-2
lines changed

doc/stringio/read.rdoc

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
Reads and returns a string containing bytes read from the stream,
2+
beginning at the current position;
3+
advances the position by the count of bytes read.
4+
5+
With no arguments given,
6+
reads all remaining bytes in the stream;
7+
returns a new string containing bytes read:
8+
9+
strio = StringIO.new('Hello') # Five 1-byte characters.
10+
strio.read # => "Hello"
11+
strio.pos # => 5
12+
strio.read # => ""
13+
StringIO.new('').read # => ""
14+
15+
With non-negative argument +maxlen+ given,
16+
reads +maxlen+ bytes as available;
17+
returns a new string containing the bytes read, or +nil+ if none:
18+
19+
strio.rewind
20+
strio.read(3) # => "Hel"
21+
strio.read(3) # => "lo"
22+
strio.read(3) # => nil
23+
24+
russian = 'Привет' # Six 2-byte characters.
25+
russian.b
26+
# => "\xD0\x9F\xD1\x80\xD0\xB8\xD0\xB2\xD0\xB5\xD1\x82"
27+
strio = StringIO.new(russian)
28+
strio.read(6) # => "\xD0\x9F\xD1\x80\xD0\xB8"
29+
strio.read(6) # => "\xD0\xB2\xD0\xB5\xD1\x82"
30+
strio.read(6) # => nil
31+
32+
japanese = 'こんにちは'
33+
japanese.b
34+
# => "\xE3\x81\x93\xE3\x82\x93\xE3\x81\xAB\xE3\x81\xA1\xE3\x81\xAF"
35+
strio = StringIO.new(japanese)
36+
strio.read(9) # => "\xE3\x81\x93\xE3\x82\x93\xE3\x81\xAB"
37+
strio.read(9) # => "\xE3\x81\xA1\xE3\x81\xAF"
38+
strio.read(9) # => nil
39+
40+
With argument +max_len+ as +nil+ and string argument +out_string+ given,
41+
reads the remaining bytes in the stream;
42+
clears +out_string+ and writes the bytes into it;
43+
returns +out_string+:
44+
45+
out_string = 'Will be overwritten'
46+
strio = StringIO.new('Hello')
47+
strio.read(nil, out_string) # => "Hello"
48+
strio.read(nil, out_string) # => ""
49+
50+
With non-negative argument +maxlen+ and string argument +out_string+ given,
51+
reads the +maxlen bytes from the stream, as availble;
52+
clears +out_string+ and writes the bytes into it;
53+
returns +out_string+ if any bytes were read, or +nil+ if none:
54+
55+
out_string = 'Will be overwritten'
56+
strio = StringIO.new('Hello')
57+
strio.read(3, out_string) # => "Hel"
58+
strio.read(3, out_string) # => "lo"
59+
strio.read(3, out_string) # => nil
60+
61+
out_string = 'Will be overwritten'
62+
strio = StringIO.new(russian)
63+
strio.read(6, out_string) # => "При"
64+
strio.read(6, out_string) # => "вет"
65+
strio.read(6, out_string) # => nil
66+
strio.rewind
67+
russian.b
68+
# => "\xD0\x9F\xD1\x80\xD0\xB8\xD0\xB2\xD0\xB5\xD1\x82"
69+
strio.read(3) # => "\xD0\x9F\xD1"
70+
strio.read(3) # => "\x80\xD0\xB8"
71+
72+
out_string = 'Will be overwritten'
73+
strio = StringIO.new(japanese)
74+
strio.read(9, out_string) # => "こんに"
75+
strio.read(9, out_string) # => "ちは"
76+
strio.read(9, out_string) # => nil
77+
strio.rewind
78+
japanese.b
79+
# => "\xE3\x81\x93\xE3\x82\x93\xE3\x81\xAB\xE3\x81\xA1\xE3\x81\xAF"
80+
strio.read(4) # => "\xE3\x81\x93\xE3"
81+
strio.read(4) # => "\x82\x93\xE3\x81"
82+
83+
Related: #gets, #readlines.

ext/stringio/stringio.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1650,9 +1650,10 @@ strio_putc(VALUE self, VALUE ch)
16501650

16511651
/*
16521652
* call-seq:
1653-
* strio.read([length [, outbuf]]) -> string, outbuf, or nil
1653+
* read(maxlen = nil, out_string = nil) → new_string, out_string, or nil
1654+
*
1655+
* :include: stringio/read.rdoc
16541656
*
1655-
* See IO#read.
16561657
*/
16571658
static VALUE
16581659
strio_read(int argc, VALUE *argv, VALUE self)

0 commit comments

Comments
 (0)