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
|
Reads and returns a line from the stream;
returns +nil+ if at end-of-stream.
Side effects:
- Increments stream position by the number of bytes read.
- Assigns the return value to global variable <tt>$_</tt>.
With no arguments given, reads a line using the default record separator
(global variable <tt>$/</tt>,* whose initial value is <tt>"\n"</tt>):
strio = StringIO.new(TEXT)
strio.pos # => 0
strio.gets # => "First line\n"
strio.pos # => 11
$_ # => "First line\n"
strio.gets # => "Second line\n"
strio.read # => "\nFourth line\nFifth line\n"
strio.eof? # => true
strio.gets # => nil
strio = StringIO.new('Привет') # Six 2-byte characters
strio.pos # => 0
strio.gets # => "Привет"
strio.pos # => 12
<b>Argument +sep+</b>
With only string argument +sep+ given, reads a line using that string as the record separator:
strio = StringIO.new(TEXT)
strio.gets(' ') # => "First "
strio.gets(' ') # => "line\nSecond "
strio.gets(' ') # => "line\n\nFourth "
<b>Argument +limit+</b>
With only integer argument +limit+ given,
reads a line using the default record separator;
limits the size (in characters) of each line to the given limit:
strio = StringIO.new(TEXT)
strio.gets(10) # => "First line"
strio.gets(10) # => "\n"
strio.gets(10) # => "Second lin"
strio.gets(10) # => "e\n"
<b>Arguments +sep+ and +limit+</b>
With arguments +sep+ and +limit+ both given, honors both:
strio = StringIO.new(TEXT)
strio.gets(' ', 10) # => "First "
strio.gets(' ', 10) # => "line\nSecon"
strio.gets(' ', 10) # => "d "
<b>Position</b>
As stated above, method +gets+ reads and returns the next 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:
strio = StringIO.new(TEXT)
strio.pos = 12
strio.gets # => "econd line\n"
The position need not be at a character boundary:
strio = StringIO.new('Привет') # Six 2-byte characters.
strio.pos = 2 # At beginning of second character.
strio.gets # => "ривет"
strio.pos = 3 # In middle of second character.
strio.gets # => "\x80ивет"
<b>Special Record Separators</b>
Like some methods in class IO, method +gets+ 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]:
strio = StringIO.new(TEXT)
strio.gets('') # Read "paragraph" (up to empty line).
# => "First line\nSecond line\n\n"
strio = StringIO.new(TEXT)
strio.gets(nil) # "Slurp": read all.
# => "First line\nSecond line\n\nFourth line\nFifth line\n"
<b>Keyword Argument +chomp+</b>
With keyword argument +chomp+ given as +true+ (the default is +false+),
removes the trailing newline (if any) from the returned line:
strio = StringIO.new(TEXT)
strio.gets # => "First line\n"
strio.gets(chomp: true) # => "Second line"
Related: #each_line, #readlines,
{Kernel#puts}[rdoc-ref:Kernel#puts].
|