rbs 3.10.0.pre.2 → 3.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/c-check.yml +1 -1
- data/.github/workflows/comments.yml +2 -2
- data/.github/workflows/ruby.yml +7 -7
- data/CHANGELOG.md +49 -0
- data/core/array.rbs +56 -3
- data/core/complex.rbs +32 -21
- data/core/encoding.rbs +3 -7
- data/core/enumerable.rbs +1 -1
- data/core/enumerator.rbs +18 -1
- data/core/fiber.rbs +2 -1
- data/core/file.rbs +1 -1
- data/core/file_test.rbs +1 -1
- data/core/float.rbs +208 -21
- data/core/gc.rbs +4 -9
- data/core/hash.rbs +4 -4
- data/core/integer.rbs +78 -38
- data/core/io/buffer.rbs +18 -7
- data/core/io.rbs +8 -8
- data/core/kernel.rbs +8 -8
- data/core/module.rbs +17 -6
- data/core/numeric.rbs +8 -8
- data/core/object_space.rbs +13 -20
- data/core/pathname.rbs +2 -3
- data/core/ractor.rbs +4 -4
- data/core/range.rbs +1 -1
- data/core/rational.rbs +37 -24
- data/core/rbs/unnamed/argf.rbs +1 -1
- data/core/regexp.rbs +3 -3
- data/core/ruby.rbs +53 -0
- data/core/rubygems/version.rbs +2 -3
- data/core/set.rbs +86 -64
- data/core/string.rbs +275 -141
- data/core/thread.rbs +9 -9
- data/core/trace_point.rbs +7 -4
- data/lib/rbs/test/type_check.rb +1 -0
- data/lib/rbs/version.rb +1 -1
- data/lib/rdoc/discover.rb +1 -1
- data/stdlib/bigdecimal/0/big_decimal.rbs +100 -82
- data/stdlib/bigdecimal-math/0/big_math.rbs +169 -8
- data/stdlib/date/0/date.rbs +67 -59
- data/stdlib/date/0/date_time.rbs +1 -1
- data/stdlib/json/0/json.rbs +1 -0
- data/stdlib/objspace/0/objspace.rbs +1 -1
- data/stdlib/openssl/0/openssl.rbs +150 -80
- data/stdlib/psych/0/psych.rbs +3 -3
- data/stdlib/stringio/0/stringio.rbs +796 -37
- data/stdlib/strscan/0/string_scanner.rbs +1 -1
- data/stdlib/tempfile/0/tempfile.rbs +2 -2
- data/stdlib/time/0/time.rbs +1 -1
- data/stdlib/timeout/0/timeout.rbs +63 -7
- data/stdlib/uri/0/generic.rbs +1 -1
- metadata +3 -2
|
@@ -1,12 +1,33 @@
|
|
|
1
1
|
# <!-- rdoc-file=ext/stringio/stringio.c -->
|
|
2
|
-
#
|
|
3
|
-
# [
|
|
4
|
-
#
|
|
5
|
-
#
|
|
2
|
+
# Class StringIO supports accessing a string as a stream,
|
|
3
|
+
# similar in some ways to [class
|
|
4
|
+
# IO](https://docs.ruby-lang.org/en/master/IO.html).
|
|
5
|
+
# You can create a StringIO instance using:
|
|
6
|
+
# * StringIO.new: returns a new StringIO object containing the given string.
|
|
7
|
+
# * StringIO.open: passes a new StringIO object to the given block.
|
|
8
|
+
# Like an IO stream, a StringIO stream has certain properties:
|
|
9
|
+
# * **Read/write mode**: whether the stream may be read, written, appended to,
|
|
10
|
+
# etc.;
|
|
11
|
+
# see [Read/Write Mode](rdoc-ref:StringIO@Read-2FWrite+Mode).
|
|
12
|
+
# * **Data mode**: text-only or binary;
|
|
13
|
+
# see [Data Mode](rdoc-ref:StringIO@Data+Mode).
|
|
14
|
+
# * **Encodings**: internal and external encodings;
|
|
15
|
+
# see [Encodings](rdoc-ref:StringIO@Encodings).
|
|
16
|
+
# * **Position**: where in the stream the next read or write is to occur;
|
|
17
|
+
# see [Position](rdoc-ref:StringIO@Position).
|
|
18
|
+
# * **Line number**: a special, line-oriented, "position" (different from the
|
|
19
|
+
# position mentioned above);
|
|
20
|
+
# see [Line Number](rdoc-ref:StringIO@Line+Number).
|
|
21
|
+
# * **Open/closed**: whether the stream is open or closed, for reading or
|
|
22
|
+
# writing.
|
|
23
|
+
# see [Open/Closed Streams](rdoc-ref:StringIO@Open-2FClosed+Streams).
|
|
24
|
+
# * **BOM**: byte mark order;
|
|
25
|
+
# see [Byte Order Mark](rdoc-ref:StringIO@BOM+-28Byte+Order+Mark-29).
|
|
26
|
+
# ## About the Examples
|
|
6
27
|
# Examples on this page assume that StringIO has been required:
|
|
7
28
|
# require 'stringio'
|
|
8
29
|
#
|
|
9
|
-
# And that
|
|
30
|
+
# And that this constant has been defined:
|
|
10
31
|
# TEXT = <<EOT
|
|
11
32
|
# First line
|
|
12
33
|
# Second line
|
|
@@ -15,8 +36,477 @@
|
|
|
15
36
|
# Fifth line
|
|
16
37
|
# EOT
|
|
17
38
|
#
|
|
18
|
-
#
|
|
19
|
-
#
|
|
39
|
+
# ## Stream Properties
|
|
40
|
+
# ### Read/Write Mode
|
|
41
|
+
# #### Summary
|
|
42
|
+
# Mode |Initial Clear?| Read | Write
|
|
43
|
+
# -------------------|--------------|--------|--------
|
|
44
|
+
# `'r'`: read-only | No |Anywhere| Error
|
|
45
|
+
# `'w'`: write-only | Yes | Error |Anywhere
|
|
46
|
+
# `'a'`: append-only | No | Error |End only
|
|
47
|
+
# `'r+'`: read/write | No |Anywhere|Anywhere
|
|
48
|
+
# `'w+'`: read-write | Yes |Anywhere|Anywhere
|
|
49
|
+
# `'a+'`: read/append| No |Anywhere|End only
|
|
50
|
+
# Each section below describes a read/write mode.
|
|
51
|
+
# Any of the modes may be given as a string or as file constants;
|
|
52
|
+
# example:
|
|
53
|
+
# strio = StringIO.new('foo', 'a')
|
|
54
|
+
# strio = StringIO.new('foo', File::WRONLY | File::APPEND)
|
|
55
|
+
#
|
|
56
|
+
# #### `'r'`: Read-Only
|
|
57
|
+
# Mode specified as one of:
|
|
58
|
+
# * String: `'r'`.
|
|
59
|
+
# * Constant: `File::RDONLY`.
|
|
60
|
+
# Initial state:
|
|
61
|
+
# strio = StringIO.new('foobarbaz', 'r')
|
|
62
|
+
# strio.pos # => 0 # Beginning-of-stream.
|
|
63
|
+
# strio.string # => "foobarbaz" # Not cleared.
|
|
64
|
+
#
|
|
65
|
+
# May be read anywhere:
|
|
66
|
+
# strio.gets(3) # => "foo"
|
|
67
|
+
# strio.gets(3) # => "bar"
|
|
68
|
+
# strio.pos = 9
|
|
69
|
+
# strio.gets(3) # => nil
|
|
70
|
+
#
|
|
71
|
+
# May not be written:
|
|
72
|
+
# strio.write('foo') # Raises IOError: not opened for writing
|
|
73
|
+
#
|
|
74
|
+
# #### `'w'`: Write-Only
|
|
75
|
+
# Mode specified as one of:
|
|
76
|
+
# * String: `'w'`.
|
|
77
|
+
# * Constant: `File::WRONLY`.
|
|
78
|
+
# Initial state:
|
|
79
|
+
# strio = StringIO.new('foo', 'w')
|
|
80
|
+
# strio.pos # => 0 # Beginning of stream.
|
|
81
|
+
# strio.string # => "" # Initially cleared.
|
|
82
|
+
#
|
|
83
|
+
# May be written anywhere (even past end-of-stream):
|
|
84
|
+
# strio.write('foobar')
|
|
85
|
+
# strio.string # => "foobar"
|
|
86
|
+
# strio.rewind
|
|
87
|
+
# strio.write('FOO')
|
|
88
|
+
# strio.string # => "FOObar"
|
|
89
|
+
# strio.pos = 3
|
|
90
|
+
# strio.write('BAR')
|
|
91
|
+
# strio.string # => "FOOBAR"
|
|
92
|
+
# strio.pos = 9
|
|
93
|
+
# strio.write('baz')
|
|
94
|
+
# strio.string # => "FOOBAR\u0000\u0000\u0000baz" # Null-padded.
|
|
95
|
+
#
|
|
96
|
+
# May not be read:
|
|
97
|
+
# strio.read # Raises IOError: not opened for reading
|
|
98
|
+
#
|
|
99
|
+
# #### `'a'`: Append-Only
|
|
100
|
+
# Mode specified as one of:
|
|
101
|
+
# * String: `'a'`.
|
|
102
|
+
# * Constant: `File::WRONLY | File::APPEND`.
|
|
103
|
+
# Initial state:
|
|
104
|
+
# strio = StringIO.new('foo', 'a')
|
|
105
|
+
# strio.pos # => 0 # Beginning-of-stream.
|
|
106
|
+
# strio.string # => "foo" # Not cleared.
|
|
107
|
+
#
|
|
108
|
+
# May be written only at the end; position does not affect writing:
|
|
109
|
+
# strio.write('bar')
|
|
110
|
+
# strio.string # => "foobar"
|
|
111
|
+
# strio.write('baz')
|
|
112
|
+
# strio.string # => "foobarbaz"
|
|
113
|
+
# strio.pos = 400
|
|
114
|
+
# strio.write('bat')
|
|
115
|
+
# strio.string # => "foobarbazbat"
|
|
116
|
+
#
|
|
117
|
+
# May not be read:
|
|
118
|
+
# strio.gets # Raises IOError: not opened for reading
|
|
119
|
+
#
|
|
120
|
+
# #### `'r+'`: Read/Write
|
|
121
|
+
# Mode specified as one of:
|
|
122
|
+
# * String: `'r+'`.
|
|
123
|
+
# * Constant: `File::RDRW`.
|
|
124
|
+
# Initial state:
|
|
125
|
+
# strio = StringIO.new('foobar', 'r+')
|
|
126
|
+
# strio.pos # => 0 # Beginning-of-stream.
|
|
127
|
+
# strio.string # => "foobar" # Not cleared.
|
|
128
|
+
#
|
|
129
|
+
# May be written anywhere (even past end-of-stream):
|
|
130
|
+
# strio.write('FOO')
|
|
131
|
+
# strio.string # => "FOObar"
|
|
132
|
+
# strio.write('BAR')
|
|
133
|
+
# strio.string # => "FOOBAR"
|
|
134
|
+
# strio.write('BAZ')
|
|
135
|
+
# strio.string # => "FOOBARBAZ"
|
|
136
|
+
# strio.pos = 12
|
|
137
|
+
# strio.write('BAT')
|
|
138
|
+
# strio.string # => "FOOBARBAZ\u0000\u0000\u0000BAT" # Null padded.
|
|
139
|
+
#
|
|
140
|
+
# May be read anywhere:
|
|
141
|
+
# strio.pos = 0
|
|
142
|
+
# strio.gets(3) # => "FOO"
|
|
143
|
+
# strio.pos = 6
|
|
144
|
+
# strio.gets(3) # => "BAZ"
|
|
145
|
+
# strio.pos = 400
|
|
146
|
+
# strio.gets(3) # => nil
|
|
147
|
+
#
|
|
148
|
+
# #### `'w+'`: Read/Write (Initially Clear)
|
|
149
|
+
# Mode specified as one of:
|
|
150
|
+
# * String: `'w+'`.
|
|
151
|
+
# * Constant: `File::RDWR | File::TRUNC`.
|
|
152
|
+
# Initial state:
|
|
153
|
+
# strio = StringIO.new('foo', 'w+')
|
|
154
|
+
# strio.pos # => 0 # Beginning-of-stream.
|
|
155
|
+
# strio.string # => "" # Truncated.
|
|
156
|
+
#
|
|
157
|
+
# May be written anywhere (even past end-of-stream):
|
|
158
|
+
# strio.write('foobar')
|
|
159
|
+
# strio.string # => "foobar"
|
|
160
|
+
# strio.rewind
|
|
161
|
+
# strio.write('FOO')
|
|
162
|
+
# strio.string # => "FOObar"
|
|
163
|
+
# strio.write('BAR')
|
|
164
|
+
# strio.string # => "FOOBAR"
|
|
165
|
+
# strio.write('BAZ')
|
|
166
|
+
# strio.string # => "FOOBARBAZ"
|
|
167
|
+
# strio.pos = 12
|
|
168
|
+
# strio.write('BAT')
|
|
169
|
+
# strio.string # => "FOOBARBAZ\u0000\u0000\u0000BAT" # Null-padded.
|
|
170
|
+
#
|
|
171
|
+
# May be read anywhere:
|
|
172
|
+
# strio.rewind
|
|
173
|
+
# strio.gets(3) # => "FOO"
|
|
174
|
+
# strio.gets(3) # => "BAR"
|
|
175
|
+
# strio.pos = 12
|
|
176
|
+
# strio.gets(3) # => "BAT"
|
|
177
|
+
# strio.pos = 400
|
|
178
|
+
# strio.gets(3) # => nil
|
|
179
|
+
#
|
|
180
|
+
# #### `'a+'`: Read/Append
|
|
181
|
+
# Mode specified as one of:
|
|
182
|
+
# * String: `'a+'`.
|
|
183
|
+
# * Constant: `File::RDWR | File::APPEND`.
|
|
184
|
+
# Initial state:
|
|
185
|
+
# strio = StringIO.new('foo', 'a+')
|
|
186
|
+
# strio.pos # => 0 # Beginning-of-stream.
|
|
187
|
+
# strio.string # => "foo" # Not cleared.
|
|
188
|
+
#
|
|
189
|
+
# May be written only at the end; #rewind; position does not affect writing:
|
|
190
|
+
# strio.write('bar')
|
|
191
|
+
# strio.string # => "foobar"
|
|
192
|
+
# strio.write('baz')
|
|
193
|
+
# strio.string # => "foobarbaz"
|
|
194
|
+
# strio.pos = 400
|
|
195
|
+
# strio.write('bat')
|
|
196
|
+
# strio.string # => "foobarbazbat"
|
|
197
|
+
#
|
|
198
|
+
# May be read anywhere:
|
|
199
|
+
# strio.rewind
|
|
200
|
+
# strio.gets(3) # => "foo"
|
|
201
|
+
# strio.gets(3) # => "bar"
|
|
202
|
+
# strio.pos = 9
|
|
203
|
+
# strio.gets(3) # => "bat"
|
|
204
|
+
# strio.pos = 400
|
|
205
|
+
# strio.gets(3) # => nil
|
|
206
|
+
#
|
|
207
|
+
# ### Data Mode
|
|
208
|
+
# To specify whether the stream is to be treated as text or as binary data,
|
|
209
|
+
# either of the following may be suffixed to any of the string read/write modes
|
|
210
|
+
# above:
|
|
211
|
+
# * `'t'`: Text;
|
|
212
|
+
# initializes the encoding as Encoding::UTF_8.
|
|
213
|
+
# * `'b'`: Binary;
|
|
214
|
+
# initializes the encoding as Encoding::ASCII_8BIT.
|
|
215
|
+
# If neither is given, the stream defaults to text data.
|
|
216
|
+
# Examples:
|
|
217
|
+
# strio = StringIO.new('foo', 'rt')
|
|
218
|
+
# strio.external_encoding # => #<Encoding:UTF-8>
|
|
219
|
+
# data = "\u9990\u9991\u9992\u9993\u9994"
|
|
220
|
+
# strio = StringIO.new(data, 'rb')
|
|
221
|
+
# strio.external_encoding # => #<Encoding:BINARY (ASCII-8BIT)>
|
|
222
|
+
#
|
|
223
|
+
# When the data mode is specified, the read/write mode may not be omitted:
|
|
224
|
+
# StringIO.new(data, 'b') # Raises ArgumentError: invalid access mode b
|
|
225
|
+
#
|
|
226
|
+
# A text stream may be changed to binary by calling instance method #binmode;
|
|
227
|
+
# a binary stream may not be changed to text.
|
|
228
|
+
# ### Encodings
|
|
229
|
+
# A stream has an encoding; see
|
|
230
|
+
# [Encodings](https://docs.ruby-lang.org/en/master/language/encodings_rdoc.html)
|
|
231
|
+
# .
|
|
232
|
+
# The initial encoding for a new or re-opened stream depends on its [data
|
|
233
|
+
# mode](rdoc-ref:StringIO@Data+Mode):
|
|
234
|
+
# * Text: `Encoding::UTF_8`.
|
|
235
|
+
# * Binary: `Encoding::ASCII_8BIT`.
|
|
236
|
+
# These instance methods are relevant:
|
|
237
|
+
# * #external_encoding: returns the current encoding of the stream as an
|
|
238
|
+
# `Encoding` object.
|
|
239
|
+
# * #internal_encoding: returns `nil`; a stream does not have an internal
|
|
240
|
+
# encoding.
|
|
241
|
+
# * #set_encoding: sets the encoding for the stream.
|
|
242
|
+
# * #set_encoding_by_bom: sets the encoding for the stream to the stream's BOM
|
|
243
|
+
# (byte order mark).
|
|
244
|
+
# Examples:
|
|
245
|
+
# strio = StringIO.new('foo', 'rt') # Text mode.
|
|
246
|
+
# strio.external_encoding # => #<Encoding:UTF-8>
|
|
247
|
+
# data = "\u9990\u9991\u9992\u9993\u9994"
|
|
248
|
+
# strio = StringIO.new(data, 'rb') # Binary mode.
|
|
249
|
+
# strio.external_encoding # => #<Encoding:BINARY (ASCII-8BIT)>
|
|
250
|
+
# strio = StringIO.new('foo')
|
|
251
|
+
# strio.external_encoding # => #<Encoding:UTF-8>
|
|
252
|
+
# strio.set_encoding('US-ASCII')
|
|
253
|
+
# strio.external_encoding # => #<Encoding:US-ASCII>
|
|
254
|
+
#
|
|
255
|
+
# ### Position
|
|
256
|
+
# A stream has a *position*, and integer offset (in bytes) into the stream.
|
|
257
|
+
# The initial position of a stream is zero.
|
|
258
|
+
# #### Getting and Setting the Position
|
|
259
|
+
# Each of these methods initializes (to zero) the position of a new or re-opened
|
|
260
|
+
# stream:
|
|
261
|
+
# * ::new: returns a new stream.
|
|
262
|
+
# * ::open: passes a new stream to the block.
|
|
263
|
+
# * #reopen: re-initializes the stream.
|
|
264
|
+
# Each of these methods queries, gets, or sets the position, without otherwise
|
|
265
|
+
# changing the stream:
|
|
266
|
+
# * #eof?: returns whether the position is at end-of-stream.
|
|
267
|
+
# * #pos: returns the position.
|
|
268
|
+
# * #pos=: sets the position.
|
|
269
|
+
# * #rewind: sets the position to zero.
|
|
270
|
+
# * #seek: sets the position.
|
|
271
|
+
# Examples:
|
|
272
|
+
# strio = StringIO.new('foobar')
|
|
273
|
+
# strio.pos # => 0
|
|
274
|
+
# strio.pos = 3
|
|
275
|
+
# strio.pos # => 3
|
|
276
|
+
# strio.eof? # => false
|
|
277
|
+
# strio.rewind
|
|
278
|
+
# strio.pos # => 0
|
|
279
|
+
# strio.seek(0, IO::SEEK_END)
|
|
280
|
+
# strio.pos # => 6
|
|
281
|
+
# strio.eof? # => true
|
|
282
|
+
#
|
|
283
|
+
# #### Position Before and After Reading
|
|
284
|
+
# Except for #pread, a stream reading method (see [Basic
|
|
285
|
+
# Reading](rdoc-ref:StringIO@Basic+Reading))
|
|
286
|
+
# begins reading at the current position.
|
|
287
|
+
# Except for #pread, a read method advances the position past the read
|
|
288
|
+
# substring.
|
|
289
|
+
# Examples:
|
|
290
|
+
# strio = StringIO.new(TEXT)
|
|
291
|
+
# strio.string # => "First line\nSecond line\n\nFourth line\nFifth line\n"
|
|
292
|
+
# strio.pos # => 0
|
|
293
|
+
# strio.getc # => "F"
|
|
294
|
+
# strio.pos # => 1
|
|
295
|
+
# strio.gets # => "irst line\n"
|
|
296
|
+
# strio.pos # => 11
|
|
297
|
+
# strio.pos = 24
|
|
298
|
+
# strio.gets # => "Fourth line\n"
|
|
299
|
+
# strio.pos # => 36
|
|
300
|
+
#
|
|
301
|
+
# strio = StringIO.new('тест') # Four 2-byte characters.
|
|
302
|
+
# strio.pos = 0 # At first byte of first character.
|
|
303
|
+
# strio.read # => "тест"
|
|
304
|
+
# strio.pos = 1 # At second byte of first character.
|
|
305
|
+
# strio.read # => "\x82ест"
|
|
306
|
+
# strio.pos = 2 # At first of second character.
|
|
307
|
+
# strio.read # => "ест"
|
|
308
|
+
#
|
|
309
|
+
# strio = StringIO.new(TEXT)
|
|
310
|
+
# strio.pos = 15
|
|
311
|
+
# a = []
|
|
312
|
+
# strio.each_line {|line| a.push(line) }
|
|
313
|
+
# a # => ["nd line\n", "\n", "Fourth line\n", "Fifth line\n"]
|
|
314
|
+
# strio.pos # => 47 ## End-of-stream.
|
|
315
|
+
#
|
|
316
|
+
# #### Position Before and After Writing
|
|
317
|
+
# Each of these methods begins writing at the current position,
|
|
318
|
+
# and advances the position to the end of the written substring:
|
|
319
|
+
# * #putc: writes the given character.
|
|
320
|
+
# * #write: writes the given objects as strings.
|
|
321
|
+
# * [Kernel#puts](https://docs.ruby-lang.org/en/master/Kernel.html#method-i-pu
|
|
322
|
+
# ts): writes given objects as strings, each followed by newline.
|
|
323
|
+
# Examples:
|
|
324
|
+
# strio = StringIO.new('foo')
|
|
325
|
+
# strio.pos # => 0
|
|
326
|
+
# strio.putc('b')
|
|
327
|
+
# strio.string # => "boo"
|
|
328
|
+
# strio.pos # => 1
|
|
329
|
+
# strio.write('r')
|
|
330
|
+
# strio.string # => "bro"
|
|
331
|
+
# strio.pos # => 2
|
|
332
|
+
# strio.puts('ew')
|
|
333
|
+
# strio.string # => "brew\n"
|
|
334
|
+
# strio.pos # => 5
|
|
335
|
+
# strio.pos = 8
|
|
336
|
+
# strio.write('foo')
|
|
337
|
+
# strio.string # => "brew\n\u0000\u0000\u0000foo"
|
|
338
|
+
# strio.pos # => 11
|
|
339
|
+
#
|
|
340
|
+
# Each of these methods writes *before* the current position, and decrements the
|
|
341
|
+
# position
|
|
342
|
+
# so that the written data is next to be read:
|
|
343
|
+
# * #ungetbyte: unshifts the given byte.
|
|
344
|
+
# * #ungetc: unshifts the given character.
|
|
345
|
+
# Examples:
|
|
346
|
+
# strio = StringIO.new('foo')
|
|
347
|
+
# strio.pos = 2
|
|
348
|
+
# strio.ungetc('x')
|
|
349
|
+
# strio.pos # => 1
|
|
350
|
+
# strio.string # => "fxo"
|
|
351
|
+
# strio.ungetc('x')
|
|
352
|
+
# strio.pos # => 0
|
|
353
|
+
# strio.string # => "xxo"
|
|
354
|
+
#
|
|
355
|
+
# This method does not affect the position:
|
|
356
|
+
# * #truncate: truncates the stream's string to the given size.
|
|
357
|
+
# Examples:
|
|
358
|
+
# strio = StringIO.new('foobar')
|
|
359
|
+
# strio.pos # => 0
|
|
360
|
+
# strio.truncate(3)
|
|
361
|
+
# strio.string # => "foo"
|
|
362
|
+
# strio.pos # => 0
|
|
363
|
+
# strio.pos = 500
|
|
364
|
+
# strio.truncate(0)
|
|
365
|
+
# strio.string # => ""
|
|
366
|
+
# strio.pos # => 500
|
|
367
|
+
#
|
|
368
|
+
# ### Line Number
|
|
369
|
+
# A stream has a line number, which initially is zero:
|
|
370
|
+
# * Method #lineno returns the line number.
|
|
371
|
+
# * Method #lineno= sets the line number.
|
|
372
|
+
# The line number can be affected by reading (but never by writing);
|
|
373
|
+
# in general, the line number is incremented each time the record separator
|
|
374
|
+
# (default: `"\n"`) is read.
|
|
375
|
+
# Examples:
|
|
376
|
+
# strio = StringIO.new(TEXT)
|
|
377
|
+
# strio.string # => "First line\nSecond line\n\nFourth line\nFifth line\n"
|
|
378
|
+
# strio.lineno # => 0
|
|
379
|
+
# strio.gets # => "First line\n"
|
|
380
|
+
# strio.lineno # => 1
|
|
381
|
+
# strio.getc # => "S"
|
|
382
|
+
# strio.lineno # => 1
|
|
383
|
+
# strio.gets # => "econd line\n"
|
|
384
|
+
# strio.lineno # => 2
|
|
385
|
+
# strio.gets # => "\n"
|
|
386
|
+
# strio.lineno # => 3
|
|
387
|
+
# strio.gets # => "Fourth line\n"
|
|
388
|
+
# strio.lineno # => 4
|
|
389
|
+
#
|
|
390
|
+
# Setting the position does not affect the line number:
|
|
391
|
+
# strio.pos = 0
|
|
392
|
+
# strio.lineno # => 4
|
|
393
|
+
# strio.gets # => "First line\n"
|
|
394
|
+
# strio.pos # => 11
|
|
395
|
+
# strio.lineno # => 5
|
|
396
|
+
#
|
|
397
|
+
# And setting the line number does not affect the position:
|
|
398
|
+
# strio.lineno = 10
|
|
399
|
+
# strio.pos # => 11
|
|
400
|
+
# strio.gets # => "Second line\n"
|
|
401
|
+
# strio.lineno # => 11
|
|
402
|
+
# strio.pos # => 23
|
|
403
|
+
#
|
|
404
|
+
# ### Open/Closed Streams
|
|
405
|
+
# A new stream is open for either reading or writing, and may be open for both;
|
|
406
|
+
# see [Read/Write Mode](rdoc-ref:StringIO@Read-2FWrite+Mode).
|
|
407
|
+
# Each of these methods initializes the read/write mode for a new or re-opened
|
|
408
|
+
# stream:
|
|
409
|
+
# * ::new: returns a new stream.
|
|
410
|
+
# * ::open: passes a new stream to the block.
|
|
411
|
+
# * #reopen: re-initializes the stream.
|
|
412
|
+
# Other relevant methods:
|
|
413
|
+
# * #close: closes the stream for both reading and writing.
|
|
414
|
+
# * #close_read: closes the stream for reading.
|
|
415
|
+
# * #close_write: closes the stream for writing.
|
|
416
|
+
# * #closed?: returns whether the stream is closed for both reading and
|
|
417
|
+
# writing.
|
|
418
|
+
# * #closed_read?: returns whether the stream is closed for reading.
|
|
419
|
+
# * #closed_write?: returns whether the stream is closed for writing.
|
|
420
|
+
# ### BOM (Byte Order Mark)
|
|
421
|
+
# The string provided for ::new, ::open, or #reopen
|
|
422
|
+
# may contain an optional [BOM](https://en.wikipedia.org/wiki/Byte_order_mark)
|
|
423
|
+
# (byte order mark) at the beginning of the string;
|
|
424
|
+
# the BOM can affect the stream's encoding.
|
|
425
|
+
# The BOM (if provided):
|
|
426
|
+
# * Is stored as part of the stream's string.
|
|
427
|
+
# * Does *not* immediately affect the encoding.
|
|
428
|
+
# * Is *initially* considered part of the stream.
|
|
429
|
+
# utf8_bom = "\xEF\xBB\xBF"
|
|
430
|
+
# string = utf8_bom + 'foo'
|
|
431
|
+
# string.bytes # => [239, 187, 191, 102, 111, 111]
|
|
432
|
+
# strio.string.bytes.take(3) # => [239, 187, 191] # The BOM.
|
|
433
|
+
# strio = StringIO.new(string, 'rb')
|
|
434
|
+
# strio.string.bytes # => [239, 187, 191, 102, 111, 111] # BOM is part of the stored string.
|
|
435
|
+
# strio.external_encoding # => #<Encoding:BINARY (ASCII-8BIT)> # Default for a binary stream.
|
|
436
|
+
# strio.gets # => "\xEF\xBB\xBFfoo" # BOM is part of the stream.
|
|
437
|
+
#
|
|
438
|
+
# You can call instance method #set_encoding_by_bom to "activate" the stored
|
|
439
|
+
# BOM;
|
|
440
|
+
# after doing so the BOM:
|
|
441
|
+
# * Is *still* stored as part of the stream's string.
|
|
442
|
+
# * *Determines* (and may have changed) the stream's encoding.
|
|
443
|
+
# * Is *no longer* considered part of the stream.
|
|
444
|
+
# strio.set_encoding_by_bom
|
|
445
|
+
# strio.string.bytes # => [239, 187, 191, 102, 111, 111] # BOM is still part of the stored string.
|
|
446
|
+
# strio.external_encoding # => #<Encoding:UTF-8> # The new encoding.
|
|
447
|
+
# strio.rewind # => 0
|
|
448
|
+
# strio.gets # => "foo" # BOM is not part of the stream.
|
|
449
|
+
#
|
|
450
|
+
# ## Basic Stream IO
|
|
451
|
+
# ### Basic Reading
|
|
452
|
+
# You can read from the stream using these instance methods:
|
|
453
|
+
# * #getbyte: reads and returns the next byte.
|
|
454
|
+
# * #getc: reads and returns the next character.
|
|
455
|
+
# * #gets: reads and returns all or part of the next line.
|
|
456
|
+
# * #read: reads and returns all or part of the remaining data in the stream.
|
|
457
|
+
# * #readlines: reads the remaining data the stream and returns an array of
|
|
458
|
+
# its lines.
|
|
459
|
+
# * [Kernel#readline](https://docs.ruby-lang.org/en/master/Kernel.html#method-
|
|
460
|
+
# i-readline): like #gets, but raises an exception if at end-of-stream.
|
|
461
|
+
# You can iterate over the stream using these instance methods:
|
|
462
|
+
# * #each_byte: reads each remaining byte, passing it to the block.
|
|
463
|
+
# * #each_char: reads each remaining character, passing it to the block.
|
|
464
|
+
# * #each_codepoint: reads each remaining codepoint, passing it to the block.
|
|
465
|
+
# * #each_line: reads all or part of each remaining line, passing the read
|
|
466
|
+
# string to the block
|
|
467
|
+
# This instance method is useful in a multi-threaded application:
|
|
468
|
+
# * #pread: reads and returns all or part of the stream.
|
|
469
|
+
# ### Basic Writing
|
|
470
|
+
# You can write to the stream, advancing the position, using these instance
|
|
471
|
+
# methods:
|
|
472
|
+
# * #putc: writes a given character.
|
|
473
|
+
# * #write: writes the given objects as strings.
|
|
474
|
+
# * [Kernel#puts](https://docs.ruby-lang.org/en/master/Kernel.html#method-i-pu
|
|
475
|
+
# ts) writes given objects as strings, each followed by newline.
|
|
476
|
+
# You can "unshift" to the stream using these instance methods;
|
|
477
|
+
# each writes *before* the current position, and decrements the position
|
|
478
|
+
# so that the written data is next to be read.
|
|
479
|
+
# * #ungetbyte: unshifts the given byte.
|
|
480
|
+
# * #ungetc: unshifts the given character.
|
|
481
|
+
# One more writing method:
|
|
482
|
+
# * #truncate: truncates the stream's string to the given size.
|
|
483
|
+
# ## Line IO
|
|
484
|
+
# Reading:
|
|
485
|
+
# * #gets: reads and returns the next line.
|
|
486
|
+
# * [Kernel#readline](https://docs.ruby-lang.org/en/master/Kernel.html#method-
|
|
487
|
+
# i-readline): like #gets, but raises an exception if at end-of-stream.
|
|
488
|
+
# * #readlines: reads the remaining data the stream and returns an array of
|
|
489
|
+
# its lines.
|
|
490
|
+
# * #each_line: reads each remaining line, passing it to the block
|
|
491
|
+
# Writing:
|
|
492
|
+
# * [Kernel#puts](https://docs.ruby-lang.org/en/master/Kernel.html#method-i-pu
|
|
493
|
+
# ts): writes given objects, each followed by newline.
|
|
494
|
+
# ## Character IO
|
|
495
|
+
# Reading:
|
|
496
|
+
# * #each_char: reads each remaining character, passing it to the block.
|
|
497
|
+
# * #getc: reads and returns the next character.
|
|
498
|
+
# Writing:
|
|
499
|
+
# * #putc: writes the given character.
|
|
500
|
+
# * #ungetc.: unshifts the given character.
|
|
501
|
+
# ## Byte IO
|
|
502
|
+
# Reading:
|
|
503
|
+
# * #each_byte: reads each remaining byte, passing it to the block.
|
|
504
|
+
# * #getbyte: reads and returns the next byte.
|
|
505
|
+
# Writing:
|
|
506
|
+
# * #ungetbyte: unshifts the given byte.
|
|
507
|
+
# ## Codepoint IO
|
|
508
|
+
# Reading:
|
|
509
|
+
# * #each_codepoint: reads each remaining codepoint, passing it to the block.
|
|
20
510
|
#
|
|
21
511
|
class StringIO
|
|
22
512
|
# <!--
|
|
@@ -78,7 +568,7 @@ class StringIO
|
|
|
78
568
|
# - binmode -> self
|
|
79
569
|
# -->
|
|
80
570
|
# Sets the data mode in `self` to binary mode; see [Data
|
|
81
|
-
# Mode](rdoc-ref:
|
|
571
|
+
# Mode](rdoc-ref:StringIO@Data+Mode).
|
|
82
572
|
#
|
|
83
573
|
def binmode: () -> self
|
|
84
574
|
|
|
@@ -188,6 +678,137 @@ class StringIO
|
|
|
188
678
|
# - each_line(limit, chomp: false) {|line| ... } -> self
|
|
189
679
|
# - each_line(sep, limit, chomp: false) {|line| ... } -> self
|
|
190
680
|
# -->
|
|
681
|
+
# With a block given calls the block with each remaining line (see "Position"
|
|
682
|
+
# below) in the stream;
|
|
683
|
+
# returns `self`.
|
|
684
|
+
# Leaves stream position at end-of-stream.
|
|
685
|
+
# **No Arguments**
|
|
686
|
+
# With no arguments given,
|
|
687
|
+
# reads lines using the default record separator
|
|
688
|
+
# (global variable `$/`, whose initial value is `"\n"`).
|
|
689
|
+
# strio = StringIO.new(TEXT)
|
|
690
|
+
# strio.each_line {|line| p line }
|
|
691
|
+
# strio.eof? # => true
|
|
692
|
+
#
|
|
693
|
+
# Output:
|
|
694
|
+
# "First line\n"
|
|
695
|
+
# "Second line\n"
|
|
696
|
+
# "\n"
|
|
697
|
+
# "Fourth line\n"
|
|
698
|
+
# "Fifth line\n"
|
|
699
|
+
#
|
|
700
|
+
# **Argument `sep`**
|
|
701
|
+
# With only string argument `sep` given,
|
|
702
|
+
# reads lines using that string as the record separator:
|
|
703
|
+
# strio = StringIO.new(TEXT)
|
|
704
|
+
# strio.each_line(' ') {|line| p line }
|
|
705
|
+
#
|
|
706
|
+
# Output:
|
|
707
|
+
# "First "
|
|
708
|
+
# "line\nSecond "
|
|
709
|
+
# "line\n\nFourth "
|
|
710
|
+
# "line\nFifth "
|
|
711
|
+
# "line\n"
|
|
712
|
+
#
|
|
713
|
+
# **Argument `limit`**
|
|
714
|
+
# With only integer argument `limit` given,
|
|
715
|
+
# reads lines using the default record separator;
|
|
716
|
+
# also limits the size (in characters) of each line to the given limit:
|
|
717
|
+
# strio = StringIO.new(TEXT)
|
|
718
|
+
# strio.each_line(10) {|line| p line }
|
|
719
|
+
#
|
|
720
|
+
# Output:
|
|
721
|
+
# "First line"
|
|
722
|
+
# "\n"
|
|
723
|
+
# "Second lin"
|
|
724
|
+
# "e\n"
|
|
725
|
+
# "\n"
|
|
726
|
+
# "Fourth lin"
|
|
727
|
+
# "e\n"
|
|
728
|
+
# "Fifth line"
|
|
729
|
+
# "\n"
|
|
730
|
+
#
|
|
731
|
+
# **Arguments `sep` and `limit`**
|
|
732
|
+
# With arguments `sep` and `limit` both given,
|
|
733
|
+
# honors both:
|
|
734
|
+
# strio = StringIO.new(TEXT)
|
|
735
|
+
# strio.each_line(' ', 10) {|line| p line }
|
|
736
|
+
#
|
|
737
|
+
# Output:
|
|
738
|
+
# "First "
|
|
739
|
+
# "line\nSecon"
|
|
740
|
+
# "d "
|
|
741
|
+
# "line\n\nFour"
|
|
742
|
+
# "th "
|
|
743
|
+
# "line\nFifth"
|
|
744
|
+
# " "
|
|
745
|
+
# "line\n"
|
|
746
|
+
#
|
|
747
|
+
# **Position**
|
|
748
|
+
# As stated above, method `each` *remaining* line in the stream.
|
|
749
|
+
# In the examples above each `strio` object starts with its position at
|
|
750
|
+
# beginning-of-stream;
|
|
751
|
+
# but in other cases the position may be anywhere (see StringIO#pos):
|
|
752
|
+
# strio = StringIO.new(TEXT)
|
|
753
|
+
# strio.pos = 30 # Set stream position to character 30.
|
|
754
|
+
# strio.each_line {|line| p line }
|
|
755
|
+
#
|
|
756
|
+
# Output:
|
|
757
|
+
# " line\n"
|
|
758
|
+
# "Fifth line\n"
|
|
759
|
+
#
|
|
760
|
+
# In all the examples above, the stream position is at the beginning of a
|
|
761
|
+
# character;
|
|
762
|
+
# in other cases, that need not be so:
|
|
763
|
+
# s = 'こんにちは' # Five 3-byte characters.
|
|
764
|
+
# strio = StringIO.new(s)
|
|
765
|
+
# strio.pos = 3 # At beginning of second character.
|
|
766
|
+
# strio.each_line {|line| p line }
|
|
767
|
+
# strio.pos = 4 # At second byte of second character.
|
|
768
|
+
# strio.each_line {|line| p line }
|
|
769
|
+
# strio.pos = 5 # At third byte of second character.
|
|
770
|
+
# strio.each_line {|line| p line }
|
|
771
|
+
#
|
|
772
|
+
# Output:
|
|
773
|
+
# "んにちは"
|
|
774
|
+
# "\x82\x93にちは"
|
|
775
|
+
# "\x93にちは"
|
|
776
|
+
#
|
|
777
|
+
# **Special Record Separators**
|
|
778
|
+
# Like some methods in class `IO`, StringIO.each honors two special record
|
|
779
|
+
# separators;
|
|
780
|
+
# see [Special Line
|
|
781
|
+
# Separators](https://docs.ruby-lang.org/en/master/IO.html#class-IO-label-Specia
|
|
782
|
+
# l+Line+Separator+Values).
|
|
783
|
+
# strio = StringIO.new(TEXT)
|
|
784
|
+
# strio.each_line('') {|line| p line } # Read as paragraphs (separated by blank lines).
|
|
785
|
+
#
|
|
786
|
+
# Output:
|
|
787
|
+
# "First line\nSecond line\n\n"
|
|
788
|
+
# "Fourth line\nFifth line\n"
|
|
789
|
+
#
|
|
790
|
+
# strio = StringIO.new(TEXT)
|
|
791
|
+
# strio.each_line(nil) {|line| p line } # "Slurp"; read it all.
|
|
792
|
+
#
|
|
793
|
+
# Output:
|
|
794
|
+
# "First line\nSecond line\n\nFourth line\nFifth line\n"
|
|
795
|
+
#
|
|
796
|
+
# **Keyword Argument `chomp`**
|
|
797
|
+
# With keyword argument `chomp` given as `true` (the default is `false`),
|
|
798
|
+
# removes trailing newline (if any) from each line:
|
|
799
|
+
# strio = StringIO.new(TEXT)
|
|
800
|
+
# strio.each_line(chomp: true) {|line| p line }
|
|
801
|
+
#
|
|
802
|
+
# Output:
|
|
803
|
+
# "First line"
|
|
804
|
+
# "Second line"
|
|
805
|
+
# ""
|
|
806
|
+
# "Fourth line"
|
|
807
|
+
# "Fifth line"
|
|
808
|
+
#
|
|
809
|
+
# With no block given, returns a new
|
|
810
|
+
# [Enumerator](https://docs.ruby-lang.org/en/master/Enumerator.html).
|
|
811
|
+
# Related: StringIO.each_byte, StringIO.each_char, StringIO.each_codepoint.
|
|
191
812
|
#
|
|
192
813
|
def each: (?String sep, ?Integer limit, ?chomp: boolish) { (String) -> untyped } -> self
|
|
193
814
|
| (?String sep, ?Integer limit, ?chomp: boolish) -> ::Enumerator[String, self]
|
|
@@ -394,19 +1015,21 @@ class StringIO
|
|
|
394
1015
|
#
|
|
395
1016
|
# Returns a byte, not a character:
|
|
396
1017
|
#
|
|
397
|
-
# s = '
|
|
398
|
-
# s.bytes
|
|
1018
|
+
# s = 'Привет'
|
|
1019
|
+
# s.bytes
|
|
1020
|
+
# # => [208, 159, 209, 128, 208, 184, 208, 178, 208, 181, 209, 130]
|
|
399
1021
|
# strio = StringIO.new(s)
|
|
400
|
-
# strio.getbyte # =>
|
|
401
|
-
# strio.getbyte # =>
|
|
1022
|
+
# strio.getbyte # => 208
|
|
1023
|
+
# strio.getbyte # => 159
|
|
402
1024
|
#
|
|
403
1025
|
# s = 'こんにちは'
|
|
404
|
-
# s.bytes
|
|
1026
|
+
# s.bytes
|
|
1027
|
+
# # => [227, 129, 147, 227, 130, 147, 227, 129, 171, 227, 129, 161, 227, 129, 175]
|
|
405
1028
|
# strio = StringIO.new(s)
|
|
406
1029
|
# strio.getbyte # => 227
|
|
407
1030
|
# strio.getbyte # => 129
|
|
408
1031
|
#
|
|
409
|
-
# Related:
|
|
1032
|
+
# Related: #each_byte, #ungetbyte, #getc.
|
|
410
1033
|
#
|
|
411
1034
|
def getbyte: () -> Integer?
|
|
412
1035
|
|
|
@@ -428,9 +1051,9 @@ class StringIO
|
|
|
428
1051
|
#
|
|
429
1052
|
# Returns characters, not bytes:
|
|
430
1053
|
#
|
|
431
|
-
# strio = StringIO.new('
|
|
432
|
-
# strio.getc # => "
|
|
433
|
-
# strio.getc # => "
|
|
1054
|
+
# strio = StringIO.new('Привет')
|
|
1055
|
+
# strio.getc # => "П"
|
|
1056
|
+
# strio.getc # => "р"
|
|
434
1057
|
#
|
|
435
1058
|
# strio = StringIO.new('こんにちは')
|
|
436
1059
|
# strio.getc # => "こ"
|
|
@@ -447,7 +1070,7 @@ class StringIO
|
|
|
447
1070
|
# strio.pos = 5 # => 5 # At third byte of second character; returns byte.
|
|
448
1071
|
# strio.getc # => "\x93"
|
|
449
1072
|
#
|
|
450
|
-
# Related:
|
|
1073
|
+
# Related: #getbyte, #putc, #ungetc.
|
|
451
1074
|
#
|
|
452
1075
|
def getc: () -> String?
|
|
453
1076
|
|
|
@@ -477,10 +1100,10 @@ class StringIO
|
|
|
477
1100
|
# strio.eof? # => true
|
|
478
1101
|
# strio.gets # => nil
|
|
479
1102
|
#
|
|
480
|
-
# strio = StringIO.new('
|
|
1103
|
+
# strio = StringIO.new('Привет') # Six 2-byte characters
|
|
481
1104
|
# strio.pos # => 0
|
|
482
|
-
# strio.gets # => "
|
|
483
|
-
# strio.pos # =>
|
|
1105
|
+
# strio.gets # => "Привет"
|
|
1106
|
+
# strio.pos # => 12
|
|
484
1107
|
#
|
|
485
1108
|
# **Argument `sep`**
|
|
486
1109
|
#
|
|
@@ -526,11 +1149,11 @@ class StringIO
|
|
|
526
1149
|
#
|
|
527
1150
|
# The position need not be at a character boundary:
|
|
528
1151
|
#
|
|
529
|
-
# strio = StringIO.new('
|
|
530
|
-
# strio.pos = 2
|
|
531
|
-
# strio.gets # => "
|
|
532
|
-
# strio.pos = 3
|
|
533
|
-
# strio.gets # => "\
|
|
1152
|
+
# strio = StringIO.new('Привет') # Six 2-byte characters.
|
|
1153
|
+
# strio.pos = 2 # At beginning of second character.
|
|
1154
|
+
# strio.gets # => "ривет"
|
|
1155
|
+
# strio.pos = 3 # In middle of second character.
|
|
1156
|
+
# strio.gets # => "\x80ивет"
|
|
534
1157
|
#
|
|
535
1158
|
# **Special Record Separators**
|
|
536
1159
|
#
|
|
@@ -556,7 +1179,7 @@ class StringIO
|
|
|
556
1179
|
# strio.gets # => "First line\n"
|
|
557
1180
|
# strio.gets(chomp: true) # => "Second line"
|
|
558
1181
|
#
|
|
559
|
-
# Related:
|
|
1182
|
+
# Related: #each_line, #readlines, [Kernel#puts](rdoc-ref:Kernel#puts).
|
|
560
1183
|
#
|
|
561
1184
|
def gets: (?String sep, ?Integer limit, ?chomp: boolish) -> String?
|
|
562
1185
|
|
|
@@ -573,7 +1196,7 @@ class StringIO
|
|
|
573
1196
|
# - external_encoding -> encoding or nil
|
|
574
1197
|
# -->
|
|
575
1198
|
# Returns an Encoding object that represents the encoding of the string; see
|
|
576
|
-
# [
|
|
1199
|
+
# [Encodings](rdoc-ref:StringIO@Encodings):
|
|
577
1200
|
#
|
|
578
1201
|
# strio = StringIO.new('foo')
|
|
579
1202
|
# strio.external_encoding # => #<Encoding:UTF-8>
|
|
@@ -598,7 +1221,7 @@ class StringIO
|
|
|
598
1221
|
# - lineno -> current_line_number
|
|
599
1222
|
# -->
|
|
600
1223
|
# Returns the current line number in `self`; see [Line
|
|
601
|
-
# Number](rdoc-ref:
|
|
1224
|
+
# Number](rdoc-ref:StringIO@Line+Number).
|
|
602
1225
|
#
|
|
603
1226
|
def lineno: () -> Integer
|
|
604
1227
|
|
|
@@ -607,7 +1230,7 @@ class StringIO
|
|
|
607
1230
|
# - lineno = new_line_number -> new_line_number
|
|
608
1231
|
# -->
|
|
609
1232
|
# Sets the current line number in `self` to the given `new_line_number`; see
|
|
610
|
-
# [Line Number](rdoc-ref:
|
|
1233
|
+
# [Line Number](rdoc-ref:StringIO@Line+Number).
|
|
611
1234
|
#
|
|
612
1235
|
def lineno=: (Integer arg0) -> Integer
|
|
613
1236
|
|
|
@@ -623,7 +1246,8 @@ class StringIO
|
|
|
623
1246
|
# rdoc-file=ext/stringio/stringio.c
|
|
624
1247
|
# - pos -> stream_position
|
|
625
1248
|
# -->
|
|
626
|
-
# Returns the current position (in bytes); see
|
|
1249
|
+
# Returns the current position (in bytes); see
|
|
1250
|
+
# [Position](rdoc-ref:StringIO@Position).
|
|
627
1251
|
#
|
|
628
1252
|
def pos: () -> Integer
|
|
629
1253
|
|
|
@@ -631,7 +1255,8 @@ class StringIO
|
|
|
631
1255
|
# rdoc-file=ext/stringio/stringio.c
|
|
632
1256
|
# - pos = new_position -> new_position
|
|
633
1257
|
# -->
|
|
634
|
-
# Sets the current position (in bytes); see
|
|
1258
|
+
# Sets the current position (in bytes); see
|
|
1259
|
+
# [Position](rdoc-ref:StringIO@Position).
|
|
635
1260
|
#
|
|
636
1261
|
def pos=: (Integer arg0) -> Integer
|
|
637
1262
|
|
|
@@ -779,6 +1404,11 @@ class StringIO
|
|
|
779
1404
|
# rdoc-file=ext/stringio/stringio.c
|
|
780
1405
|
# - size -> integer
|
|
781
1406
|
# -->
|
|
1407
|
+
# Returns the number of bytes in the string in `self`:
|
|
1408
|
+
#
|
|
1409
|
+
# StringIO.new('hello').size # => 5 # Five 1-byte characters.
|
|
1410
|
+
# StringIO.new('тест').size # => 8 # Four 2-byte characters.
|
|
1411
|
+
# StringIO.new('こんにちは').size # => 15 # Five 3-byte characters.
|
|
782
1412
|
#
|
|
783
1413
|
def size: () -> Integer
|
|
784
1414
|
|
|
@@ -806,7 +1436,8 @@ class StringIO
|
|
|
806
1436
|
# rdoc-file=ext/stringio/stringio.c
|
|
807
1437
|
# - pos -> stream_position
|
|
808
1438
|
# -->
|
|
809
|
-
# Returns the current position (in bytes); see
|
|
1439
|
+
# Returns the current position (in bytes); see
|
|
1440
|
+
# [Position](rdoc-ref:StringIO@Position).
|
|
810
1441
|
#
|
|
811
1442
|
def tell: () -> Integer
|
|
812
1443
|
|
|
@@ -868,10 +1499,138 @@ class StringIO
|
|
|
868
1499
|
def codepoints: () { (Integer arg0) -> untyped } -> self
|
|
869
1500
|
| () -> ::Enumerator[Integer, self]
|
|
870
1501
|
|
|
871
|
-
# <!--
|
|
872
|
-
#
|
|
873
|
-
#
|
|
874
|
-
#
|
|
1502
|
+
# <!-- rdoc-file=ext/stringio/stringio.c -->
|
|
1503
|
+
# With a block given calls the block with each remaining line (see "Position"
|
|
1504
|
+
# below) in the stream;
|
|
1505
|
+
# returns `self`.
|
|
1506
|
+
# Leaves stream position at end-of-stream.
|
|
1507
|
+
# **No Arguments**
|
|
1508
|
+
# With no arguments given,
|
|
1509
|
+
# reads lines using the default record separator
|
|
1510
|
+
# (global variable `$/`, whose initial value is `"\n"`).
|
|
1511
|
+
# strio = StringIO.new(TEXT)
|
|
1512
|
+
# strio.each_line {|line| p line }
|
|
1513
|
+
# strio.eof? # => true
|
|
1514
|
+
#
|
|
1515
|
+
# Output:
|
|
1516
|
+
# "First line\n"
|
|
1517
|
+
# "Second line\n"
|
|
1518
|
+
# "\n"
|
|
1519
|
+
# "Fourth line\n"
|
|
1520
|
+
# "Fifth line\n"
|
|
1521
|
+
#
|
|
1522
|
+
# **Argument `sep`**
|
|
1523
|
+
# With only string argument `sep` given,
|
|
1524
|
+
# reads lines using that string as the record separator:
|
|
1525
|
+
# strio = StringIO.new(TEXT)
|
|
1526
|
+
# strio.each_line(' ') {|line| p line }
|
|
1527
|
+
#
|
|
1528
|
+
# Output:
|
|
1529
|
+
# "First "
|
|
1530
|
+
# "line\nSecond "
|
|
1531
|
+
# "line\n\nFourth "
|
|
1532
|
+
# "line\nFifth "
|
|
1533
|
+
# "line\n"
|
|
1534
|
+
#
|
|
1535
|
+
# **Argument `limit`**
|
|
1536
|
+
# With only integer argument `limit` given,
|
|
1537
|
+
# reads lines using the default record separator;
|
|
1538
|
+
# also limits the size (in characters) of each line to the given limit:
|
|
1539
|
+
# strio = StringIO.new(TEXT)
|
|
1540
|
+
# strio.each_line(10) {|line| p line }
|
|
1541
|
+
#
|
|
1542
|
+
# Output:
|
|
1543
|
+
# "First line"
|
|
1544
|
+
# "\n"
|
|
1545
|
+
# "Second lin"
|
|
1546
|
+
# "e\n"
|
|
1547
|
+
# "\n"
|
|
1548
|
+
# "Fourth lin"
|
|
1549
|
+
# "e\n"
|
|
1550
|
+
# "Fifth line"
|
|
1551
|
+
# "\n"
|
|
1552
|
+
#
|
|
1553
|
+
# **Arguments `sep` and `limit`**
|
|
1554
|
+
# With arguments `sep` and `limit` both given,
|
|
1555
|
+
# honors both:
|
|
1556
|
+
# strio = StringIO.new(TEXT)
|
|
1557
|
+
# strio.each_line(' ', 10) {|line| p line }
|
|
1558
|
+
#
|
|
1559
|
+
# Output:
|
|
1560
|
+
# "First "
|
|
1561
|
+
# "line\nSecon"
|
|
1562
|
+
# "d "
|
|
1563
|
+
# "line\n\nFour"
|
|
1564
|
+
# "th "
|
|
1565
|
+
# "line\nFifth"
|
|
1566
|
+
# " "
|
|
1567
|
+
# "line\n"
|
|
1568
|
+
#
|
|
1569
|
+
# **Position**
|
|
1570
|
+
# As stated above, method `each` *remaining* line in the stream.
|
|
1571
|
+
# In the examples above each `strio` object starts with its position at
|
|
1572
|
+
# beginning-of-stream;
|
|
1573
|
+
# but in other cases the position may be anywhere (see StringIO#pos):
|
|
1574
|
+
# strio = StringIO.new(TEXT)
|
|
1575
|
+
# strio.pos = 30 # Set stream position to character 30.
|
|
1576
|
+
# strio.each_line {|line| p line }
|
|
1577
|
+
#
|
|
1578
|
+
# Output:
|
|
1579
|
+
# " line\n"
|
|
1580
|
+
# "Fifth line\n"
|
|
1581
|
+
#
|
|
1582
|
+
# In all the examples above, the stream position is at the beginning of a
|
|
1583
|
+
# character;
|
|
1584
|
+
# in other cases, that need not be so:
|
|
1585
|
+
# s = 'こんにちは' # Five 3-byte characters.
|
|
1586
|
+
# strio = StringIO.new(s)
|
|
1587
|
+
# strio.pos = 3 # At beginning of second character.
|
|
1588
|
+
# strio.each_line {|line| p line }
|
|
1589
|
+
# strio.pos = 4 # At second byte of second character.
|
|
1590
|
+
# strio.each_line {|line| p line }
|
|
1591
|
+
# strio.pos = 5 # At third byte of second character.
|
|
1592
|
+
# strio.each_line {|line| p line }
|
|
1593
|
+
#
|
|
1594
|
+
# Output:
|
|
1595
|
+
# "んにちは"
|
|
1596
|
+
# "\x82\x93にちは"
|
|
1597
|
+
# "\x93にちは"
|
|
1598
|
+
#
|
|
1599
|
+
# **Special Record Separators**
|
|
1600
|
+
# Like some methods in class `IO`, StringIO.each honors two special record
|
|
1601
|
+
# separators;
|
|
1602
|
+
# see [Special Line
|
|
1603
|
+
# Separators](https://docs.ruby-lang.org/en/master/IO.html#class-IO-label-Specia
|
|
1604
|
+
# l+Line+Separator+Values).
|
|
1605
|
+
# strio = StringIO.new(TEXT)
|
|
1606
|
+
# strio.each_line('') {|line| p line } # Read as paragraphs (separated by blank lines).
|
|
1607
|
+
#
|
|
1608
|
+
# Output:
|
|
1609
|
+
# "First line\nSecond line\n\n"
|
|
1610
|
+
# "Fourth line\nFifth line\n"
|
|
1611
|
+
#
|
|
1612
|
+
# strio = StringIO.new(TEXT)
|
|
1613
|
+
# strio.each_line(nil) {|line| p line } # "Slurp"; read it all.
|
|
1614
|
+
#
|
|
1615
|
+
# Output:
|
|
1616
|
+
# "First line\nSecond line\n\nFourth line\nFifth line\n"
|
|
1617
|
+
#
|
|
1618
|
+
# **Keyword Argument `chomp`**
|
|
1619
|
+
# With keyword argument `chomp` given as `true` (the default is `false`),
|
|
1620
|
+
# removes trailing newline (if any) from each line:
|
|
1621
|
+
# strio = StringIO.new(TEXT)
|
|
1622
|
+
# strio.each_line(chomp: true) {|line| p line }
|
|
1623
|
+
#
|
|
1624
|
+
# Output:
|
|
1625
|
+
# "First line"
|
|
1626
|
+
# "Second line"
|
|
1627
|
+
# ""
|
|
1628
|
+
# "Fourth line"
|
|
1629
|
+
# "Fifth line"
|
|
1630
|
+
#
|
|
1631
|
+
# With no block given, returns a new
|
|
1632
|
+
# [Enumerator](https://docs.ruby-lang.org/en/master/Enumerator.html).
|
|
1633
|
+
# Related: StringIO.each_byte, StringIO.each_char, StringIO.each_codepoint.
|
|
875
1634
|
#
|
|
876
1635
|
def each_line: (?String sep, ?Integer limit, ?chomp: boolish) { (String) -> untyped } -> self
|
|
877
1636
|
| (?String sep, ?Integer limit, ?chomp: boolish) -> ::Enumerator[String, self]
|