Skip to content

Commit d1b4604

Browse files
committed
[DOC] Doc for StringIO.read
1 parent 487dbe1 commit d1b4604

File tree

1 file changed

+82
-2
lines changed

1 file changed

+82
-2
lines changed

doc/stringio/read.rdoc

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,83 @@
1-
Reads and returns bytes from the stream.
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.
24

3-
With no arguments,
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.

0 commit comments

Comments
 (0)