rbs 3.9.5 → 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/.clang-format +74 -0
- data/.clangd +2 -0
- data/.github/workflows/c-check.yml +54 -0
- data/.github/workflows/comments.yml +3 -3
- data/.github/workflows/ruby.yml +34 -19
- data/.github/workflows/typecheck.yml +1 -1
- data/.github/workflows/windows.yml +1 -1
- data/.gitignore +4 -0
- data/CHANGELOG.md +49 -0
- data/README.md +38 -1
- data/Rakefile +152 -23
- data/config.yml +190 -62
- data/core/array.rbs +100 -46
- data/core/complex.rbs +32 -21
- data/core/dir.rbs +2 -2
- data/core/encoding.rbs +6 -9
- data/core/enumerable.rbs +90 -3
- data/core/enumerator.rbs +18 -1
- data/core/errno.rbs +8 -0
- data/core/errors.rbs +28 -1
- data/core/exception.rbs +2 -2
- data/core/fiber.rbs +5 -4
- data/core/file.rbs +27 -12
- data/core/file_test.rbs +1 -1
- data/core/float.rbs +209 -22
- data/core/gc.rbs +417 -281
- data/core/hash.rbs +1024 -727
- data/core/integer.rbs +78 -38
- data/core/io/buffer.rbs +18 -7
- data/core/io/wait.rbs +11 -33
- data/core/io.rbs +14 -12
- data/core/kernel.rbs +57 -51
- data/core/marshal.rbs +1 -1
- data/core/match_data.rbs +1 -1
- data/core/math.rbs +42 -3
- data/core/method.rbs +14 -6
- data/core/module.rbs +88 -17
- data/core/nil_class.rbs +3 -3
- data/core/numeric.rbs +16 -16
- data/core/object.rbs +3 -3
- data/core/object_space.rbs +21 -15
- data/{stdlib/pathname/0 → core}/pathname.rbs +255 -355
- data/core/proc.rbs +15 -8
- data/core/process.rbs +2 -2
- data/core/ractor.rbs +278 -437
- data/core/range.rbs +7 -8
- data/core/rational.rbs +37 -24
- data/core/rbs/unnamed/argf.rbs +2 -2
- data/core/rbs/unnamed/env_class.rbs +1 -1
- data/core/rbs/unnamed/random.rbs +4 -2
- data/core/regexp.rbs +25 -20
- data/core/ruby.rbs +53 -0
- data/core/ruby_vm.rbs +6 -4
- data/core/rubygems/errors.rbs +3 -70
- data/core/rubygems/rubygems.rbs +11 -79
- data/core/rubygems/version.rbs +2 -3
- data/core/set.rbs +488 -359
- data/core/string.rbs +3145 -1231
- data/core/struct.rbs +1 -1
- data/core/symbol.rbs +4 -4
- data/core/thread.rbs +92 -29
- data/core/time.rbs +35 -9
- data/core/trace_point.rbs +7 -4
- data/core/unbound_method.rbs +14 -6
- data/docs/aliases.md +79 -0
- data/docs/collection.md +2 -2
- data/docs/encoding.md +56 -0
- data/docs/gem.md +0 -1
- data/docs/sigs.md +3 -3
- data/ext/rbs_extension/ast_translation.c +1016 -0
- data/ext/rbs_extension/ast_translation.h +37 -0
- data/ext/rbs_extension/class_constants.c +155 -0
- data/{include/rbs/constants.h → ext/rbs_extension/class_constants.h} +7 -1
- data/ext/rbs_extension/compat.h +10 -0
- data/ext/rbs_extension/extconf.rb +25 -1
- data/ext/rbs_extension/legacy_location.c +317 -0
- data/ext/rbs_extension/legacy_location.h +45 -0
- data/ext/rbs_extension/main.c +357 -14
- data/ext/rbs_extension/rbs_extension.h +6 -21
- data/ext/rbs_extension/rbs_string_bridging.c +9 -0
- data/ext/rbs_extension/rbs_string_bridging.h +24 -0
- data/include/rbs/ast.h +687 -0
- data/include/rbs/defines.h +86 -0
- data/include/rbs/lexer.h +199 -0
- data/include/rbs/location.h +59 -0
- data/include/rbs/parser.h +135 -0
- data/include/rbs/string.h +47 -0
- data/include/rbs/util/rbs_allocator.h +59 -0
- data/include/rbs/util/rbs_assert.h +20 -0
- data/include/rbs/util/rbs_buffer.h +83 -0
- data/include/rbs/util/rbs_constant_pool.h +6 -67
- data/include/rbs/util/rbs_encoding.h +282 -0
- data/include/rbs/util/rbs_unescape.h +24 -0
- data/include/rbs.h +1 -2
- data/lib/rbs/annotate/formatter.rb +3 -13
- data/lib/rbs/annotate/rdoc_annotator.rb +3 -1
- data/lib/rbs/annotate/rdoc_source.rb +1 -1
- data/lib/rbs/cli/validate.rb +2 -2
- data/lib/rbs/cli.rb +1 -1
- data/lib/rbs/collection/config/lockfile_generator.rb +1 -0
- data/lib/rbs/definition_builder/ancestor_builder.rb +5 -5
- data/lib/rbs/environment.rb +64 -59
- data/lib/rbs/environment_loader.rb +1 -1
- data/lib/rbs/errors.rb +1 -1
- data/lib/rbs/parser_aux.rb +5 -0
- data/lib/rbs/resolver/constant_resolver.rb +2 -2
- data/lib/rbs/resolver/type_name_resolver.rb +124 -38
- data/lib/rbs/test/type_check.rb +14 -0
- data/lib/rbs/types.rb +3 -1
- data/lib/rbs/version.rb +1 -1
- data/lib/rbs.rb +1 -1
- data/lib/rdoc/discover.rb +1 -1
- data/lib/rdoc_plugin/parser.rb +3 -3
- data/sig/annotate/formatter.rbs +2 -2
- data/sig/annotate/rdoc_annotater.rbs +1 -1
- data/sig/environment.rbs +57 -6
- data/sig/manifest.yaml +0 -1
- data/sig/parser.rbs +20 -0
- data/sig/resolver/type_name_resolver.rbs +38 -7
- data/sig/types.rbs +4 -1
- data/src/ast.c +1256 -0
- data/src/lexer.c +2956 -0
- data/src/lexer.re +147 -0
- data/src/lexstate.c +205 -0
- data/src/location.c +71 -0
- data/src/parser.c +3507 -0
- data/src/string.c +41 -0
- data/src/util/rbs_allocator.c +152 -0
- data/src/util/rbs_assert.c +19 -0
- data/src/util/rbs_buffer.c +54 -0
- data/src/util/rbs_constant_pool.c +18 -88
- data/src/util/rbs_encoding.c +21308 -0
- data/src/util/rbs_unescape.c +167 -0
- data/stdlib/bigdecimal/0/big_decimal.rbs +100 -82
- data/stdlib/bigdecimal-math/0/big_math.rbs +169 -8
- data/stdlib/cgi/0/core.rbs +2 -396
- data/stdlib/cgi/0/manifest.yaml +1 -0
- data/stdlib/cgi-escape/0/escape.rbs +153 -0
- data/stdlib/coverage/0/coverage.rbs +3 -1
- data/stdlib/date/0/date.rbs +67 -59
- data/stdlib/date/0/date_time.rbs +1 -1
- data/stdlib/delegate/0/delegator.rbs +10 -7
- data/stdlib/erb/0/erb.rbs +737 -347
- data/stdlib/fileutils/0/fileutils.rbs +18 -13
- data/stdlib/forwardable/0/forwardable.rbs +3 -0
- data/stdlib/json/0/json.rbs +68 -48
- data/stdlib/net-http/0/net-http.rbs +3 -0
- data/stdlib/objspace/0/objspace.rbs +9 -4
- data/stdlib/open-uri/0/open-uri.rbs +40 -0
- data/stdlib/openssl/0/openssl.rbs +331 -228
- data/stdlib/optparse/0/optparse.rbs +3 -3
- data/stdlib/psych/0/psych.rbs +3 -3
- data/stdlib/rdoc/0/code_object.rbs +2 -2
- data/stdlib/rdoc/0/comment.rbs +2 -0
- data/stdlib/rdoc/0/options.rbs +76 -0
- data/stdlib/rdoc/0/rdoc.rbs +7 -5
- data/stdlib/rdoc/0/store.rbs +1 -1
- data/stdlib/resolv/0/resolv.rbs +25 -68
- data/stdlib/ripper/0/ripper.rbs +5 -2
- data/stdlib/singleton/0/singleton.rbs +3 -0
- data/stdlib/socket/0/socket.rbs +13 -1
- data/stdlib/socket/0/tcp_socket.rbs +10 -2
- data/stdlib/stringio/0/stringio.rbs +1176 -85
- data/stdlib/strscan/0/string_scanner.rbs +31 -31
- data/stdlib/tempfile/0/tempfile.rbs +3 -3
- data/stdlib/time/0/time.rbs +1 -1
- data/stdlib/timeout/0/timeout.rbs +63 -7
- data/stdlib/tsort/0/cyclic.rbs +3 -0
- data/stdlib/uri/0/common.rbs +11 -2
- data/stdlib/uri/0/file.rbs +1 -1
- data/stdlib/uri/0/generic.rbs +17 -16
- data/stdlib/uri/0/rfc2396_parser.rbs +6 -7
- data/stdlib/zlib/0/zstream.rbs +1 -0
- metadata +43 -18
- data/ext/rbs_extension/lexer.c +0 -2728
- data/ext/rbs_extension/lexer.h +0 -179
- data/ext/rbs_extension/lexer.re +0 -147
- data/ext/rbs_extension/lexstate.c +0 -175
- data/ext/rbs_extension/location.c +0 -325
- data/ext/rbs_extension/location.h +0 -85
- data/ext/rbs_extension/parser.c +0 -2982
- data/ext/rbs_extension/parser.h +0 -18
- data/ext/rbs_extension/parserstate.c +0 -411
- data/ext/rbs_extension/parserstate.h +0 -163
- data/ext/rbs_extension/unescape.c +0 -32
- data/include/rbs/ruby_objs.h +0 -72
- data/src/constants.c +0 -153
- data/src/ruby_objs.c +0 -799
|
@@ -1,50 +1,561 @@
|
|
|
1
1
|
# <!-- rdoc-file=ext/stringio/stringio.c -->
|
|
2
|
-
#
|
|
3
|
-
# [
|
|
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
|
|
27
|
+
# Examples on this page assume that StringIO has been required:
|
|
28
|
+
# require 'stringio'
|
|
4
29
|
#
|
|
5
|
-
#
|
|
30
|
+
# And that this constant has been defined:
|
|
31
|
+
# TEXT = <<EOT
|
|
32
|
+
# First line
|
|
33
|
+
# Second line
|
|
6
34
|
#
|
|
7
|
-
#
|
|
35
|
+
# Fourth line
|
|
36
|
+
# Fifth line
|
|
37
|
+
# EOT
|
|
8
38
|
#
|
|
9
|
-
#
|
|
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.
|
|
10
510
|
#
|
|
11
511
|
class StringIO
|
|
12
512
|
# <!--
|
|
13
513
|
# rdoc-file=ext/stringio/stringio.c
|
|
14
514
|
# - StringIO.new(string = '', mode = 'r+') -> new_stringio
|
|
15
515
|
# -->
|
|
16
|
-
#
|
|
516
|
+
# Returns a new StringIO instance formed from `string` and `mode`; the instance
|
|
517
|
+
# should be closed when no longer needed:
|
|
518
|
+
#
|
|
519
|
+
# strio = StringIO.new
|
|
520
|
+
# strio.string # => ""
|
|
521
|
+
# strio.closed_read? # => false
|
|
522
|
+
# strio.closed_write? # => false
|
|
523
|
+
# strio.close
|
|
17
524
|
#
|
|
18
|
-
#
|
|
19
|
-
# Modes](rdoc-ref:File@Access+Modes):
|
|
525
|
+
# If `string` is frozen, the default `mode` is `'r'`:
|
|
20
526
|
#
|
|
21
|
-
# strio = StringIO.new
|
|
527
|
+
# strio = StringIO.new('foo'.freeze)
|
|
528
|
+
# strio.string # => "foo"
|
|
529
|
+
# strio.closed_read? # => false
|
|
530
|
+
# strio.closed_write? # => true
|
|
22
531
|
# strio.close
|
|
23
532
|
#
|
|
24
|
-
#
|
|
533
|
+
# Argument `mode` must be a valid [Access Mode](rdoc-ref:File@Access+Modes),
|
|
534
|
+
# which may be a string or an integer constant:
|
|
25
535
|
#
|
|
26
|
-
#
|
|
536
|
+
# StringIO.new('foo', 'w+')
|
|
537
|
+
# StringIO.new('foo', File::RDONLY)
|
|
538
|
+
#
|
|
539
|
+
# Related: StringIO.open (passes the StringIO object to the block; closes the
|
|
540
|
+
# object automatically on block exit).
|
|
27
541
|
#
|
|
28
542
|
def initialize: (?String string, ?String? mode) -> void
|
|
29
543
|
|
|
30
544
|
# <!--
|
|
31
545
|
# rdoc-file=ext/stringio/stringio.c
|
|
32
|
-
# - StringIO.open(string = '', mode = 'r+')
|
|
546
|
+
# - StringIO.open(string = '', mode = 'r+') -> new_stringio
|
|
547
|
+
# - StringIO.open(string = '', mode = 'r+') {|strio| ... } -> object
|
|
33
548
|
# -->
|
|
34
|
-
#
|
|
35
|
-
#
|
|
36
|
-
# Creates a new StringIO instance formed from `string` and `mode`; see [Access
|
|
37
|
-
# Modes](rdoc-ref:File@Access+Modes).
|
|
549
|
+
# Creates new StringIO instance by calling `StringIO.new(string, mode)`.
|
|
38
550
|
#
|
|
39
|
-
# With no block, returns the new instance:
|
|
551
|
+
# With no block given, returns the new instance:
|
|
40
552
|
#
|
|
41
553
|
# strio = StringIO.open # => #<StringIO>
|
|
42
554
|
#
|
|
43
|
-
# With a block, calls the block with the new instance and returns the
|
|
44
|
-
# value; closes the instance on block exit
|
|
555
|
+
# With a block given, calls the block with the new instance and returns the
|
|
556
|
+
# block's value; closes the instance on block exit:
|
|
45
557
|
#
|
|
46
|
-
# StringIO.open {|strio|
|
|
47
|
-
# # => #<StringIO>
|
|
558
|
+
# StringIO.open('foo') {|strio| strio.string.upcase } # => "FOO"
|
|
48
559
|
#
|
|
49
560
|
# Related: StringIO.new.
|
|
50
561
|
#
|
|
@@ -57,7 +568,7 @@ class StringIO
|
|
|
57
568
|
# - binmode -> self
|
|
58
569
|
# -->
|
|
59
570
|
# Sets the data mode in `self` to binary mode; see [Data
|
|
60
|
-
# Mode](rdoc-ref:
|
|
571
|
+
# Mode](rdoc-ref:StringIO@Data+Mode).
|
|
61
572
|
#
|
|
62
573
|
def binmode: () -> self
|
|
63
574
|
|
|
@@ -65,11 +576,16 @@ class StringIO
|
|
|
65
576
|
# rdoc-file=ext/stringio/stringio.c
|
|
66
577
|
# - close -> nil
|
|
67
578
|
# -->
|
|
68
|
-
# Closes `self` for both reading and writing
|
|
579
|
+
# Closes `self` for both reading and writing; returns `nil`:
|
|
69
580
|
#
|
|
70
|
-
#
|
|
581
|
+
# strio = StringIO.new
|
|
582
|
+
# strio.closed? # => false
|
|
583
|
+
# strio.close # => nil
|
|
584
|
+
# strio.closed? # => true
|
|
585
|
+
# strio.read # Raises IOError: not opened for reading
|
|
586
|
+
# strio.write # Raises IOError: not opened for writing
|
|
71
587
|
#
|
|
72
|
-
# Related: StringIO#close_read, StringIO#close_write.
|
|
588
|
+
# Related: StringIO#close_read, StringIO#close_write, StringIO.closed?.
|
|
73
589
|
#
|
|
74
590
|
def close: () -> nil
|
|
75
591
|
|
|
@@ -77,9 +593,15 @@ class StringIO
|
|
|
77
593
|
# rdoc-file=ext/stringio/stringio.c
|
|
78
594
|
# - close_read -> nil
|
|
79
595
|
# -->
|
|
80
|
-
# Closes `self` for reading; closed-write setting remains unchanged
|
|
596
|
+
# Closes `self` for reading; closed-write setting remains unchanged; returns
|
|
597
|
+
# `nil`:
|
|
81
598
|
#
|
|
82
|
-
#
|
|
599
|
+
# strio = StringIO.new
|
|
600
|
+
# strio.closed_read? # => false
|
|
601
|
+
# strio.close_read # => nil
|
|
602
|
+
# strio.closed_read? # => true
|
|
603
|
+
# strio.closed_write? # => false
|
|
604
|
+
# strio.read # Raises IOError: not opened for reading
|
|
83
605
|
#
|
|
84
606
|
# Related: StringIO#close, StringIO#close_write.
|
|
85
607
|
#
|
|
@@ -89,11 +611,17 @@ class StringIO
|
|
|
89
611
|
# rdoc-file=ext/stringio/stringio.c
|
|
90
612
|
# - close_write -> nil
|
|
91
613
|
# -->
|
|
92
|
-
# Closes `self` for writing; closed-read setting remains unchanged
|
|
614
|
+
# Closes `self` for writing; closed-read setting remains unchanged; returns
|
|
615
|
+
# `nil`:
|
|
93
616
|
#
|
|
94
|
-
#
|
|
617
|
+
# strio = StringIO.new
|
|
618
|
+
# strio.closed_write? # => false
|
|
619
|
+
# strio.close_write # => nil
|
|
620
|
+
# strio.closed_write? # => true
|
|
621
|
+
# strio.closed_read? # => false
|
|
622
|
+
# strio.write('foo') # Raises IOError: not opened for writing
|
|
95
623
|
#
|
|
96
|
-
# Related: StringIO#close, StringIO#close_read
|
|
624
|
+
# Related: StringIO#close, StringIO#close_read, StringIO#closed_write?.
|
|
97
625
|
#
|
|
98
626
|
def close_write: () -> nil
|
|
99
627
|
|
|
@@ -101,8 +629,16 @@ class StringIO
|
|
|
101
629
|
# rdoc-file=ext/stringio/stringio.c
|
|
102
630
|
# - closed? -> true or false
|
|
103
631
|
# -->
|
|
104
|
-
# Returns
|
|
105
|
-
#
|
|
632
|
+
# Returns whether `self` is closed for both reading and writing:
|
|
633
|
+
#
|
|
634
|
+
# strio = StringIO.new
|
|
635
|
+
# strio.closed? # => false # Open for reading and writing.
|
|
636
|
+
# strio.close_read
|
|
637
|
+
# strio.closed? # => false # Still open for writing.
|
|
638
|
+
# strio.close_write
|
|
639
|
+
# strio.closed? # => true # Now closed for both.
|
|
640
|
+
#
|
|
641
|
+
# Related: StringIO.closed_read?, StringIO.closed_write?.
|
|
106
642
|
#
|
|
107
643
|
def closed?: () -> bool
|
|
108
644
|
|
|
@@ -110,7 +646,14 @@ class StringIO
|
|
|
110
646
|
# rdoc-file=ext/stringio/stringio.c
|
|
111
647
|
# - closed_read? -> true or false
|
|
112
648
|
# -->
|
|
113
|
-
# Returns
|
|
649
|
+
# Returns whether `self` is closed for reading:
|
|
650
|
+
#
|
|
651
|
+
# strio = StringIO.new
|
|
652
|
+
# strio.closed_read? # => false
|
|
653
|
+
# strio.close_read
|
|
654
|
+
# strio.closed_read? # => true
|
|
655
|
+
#
|
|
656
|
+
# Related: StringIO#closed?, StringIO#closed_write?, StringIO#close_read.
|
|
114
657
|
#
|
|
115
658
|
def closed_read?: () -> bool
|
|
116
659
|
|
|
@@ -118,7 +661,14 @@ class StringIO
|
|
|
118
661
|
# rdoc-file=ext/stringio/stringio.c
|
|
119
662
|
# - closed_write? -> true or false
|
|
120
663
|
# -->
|
|
121
|
-
# Returns
|
|
664
|
+
# Returns whether `self` is closed for writing:
|
|
665
|
+
#
|
|
666
|
+
# strio = StringIO.new
|
|
667
|
+
# strio.closed_write? # => false
|
|
668
|
+
# strio.close_write
|
|
669
|
+
# strio.closed_write? # => true
|
|
670
|
+
#
|
|
671
|
+
# Related: StringIO#close_write, StringIO#closed?, StringIO#closed_read?.
|
|
122
672
|
#
|
|
123
673
|
def closed_write?: () -> bool
|
|
124
674
|
|
|
@@ -128,8 +678,137 @@ class StringIO
|
|
|
128
678
|
# - each_line(limit, chomp: false) {|line| ... } -> self
|
|
129
679
|
# - each_line(sep, limit, chomp: false) {|line| ... } -> self
|
|
130
680
|
# -->
|
|
131
|
-
#
|
|
132
|
-
#
|
|
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.
|
|
133
812
|
#
|
|
134
813
|
def each: (?String sep, ?Integer limit, ?chomp: boolish) { (String) -> untyped } -> self
|
|
135
814
|
| (?String sep, ?Integer limit, ?chomp: boolish) -> ::Enumerator[String, self]
|
|
@@ -139,21 +818,83 @@ class StringIO
|
|
|
139
818
|
# - each_byte {|byte| ... } -> self
|
|
140
819
|
# -->
|
|
141
820
|
# With a block given, calls the block with each remaining byte in the stream;
|
|
142
|
-
#
|
|
821
|
+
# positions the stream at end-of-file; returns `self`:
|
|
822
|
+
#
|
|
823
|
+
# bytes = []
|
|
824
|
+
# strio = StringIO.new('hello') # Five 1-byte characters.
|
|
825
|
+
# strio.each_byte {|byte| bytes.push(byte) }
|
|
826
|
+
# strio.eof? # => true
|
|
827
|
+
# bytes # => [104, 101, 108, 108, 111]
|
|
828
|
+
# bytes = []
|
|
829
|
+
# strio = StringIO.new('тест') # Four 2-byte characters.
|
|
830
|
+
# strio.each_byte {|byte| bytes.push(byte) }
|
|
831
|
+
# bytes # => [209, 130, 208, 181, 209, 129, 209, 130]
|
|
832
|
+
# bytes = []
|
|
833
|
+
# strio = StringIO.new('こんにちは') # Five 3-byte characters.
|
|
834
|
+
# strio.each_byte {|byte| bytes.push(byte) }
|
|
835
|
+
# bytes # => [227, 129, 147, 227, 130, 147, 227, 129, 171, 227, 129, 161, 227, 129, 175]
|
|
143
836
|
#
|
|
144
|
-
#
|
|
837
|
+
# The position in the stream matters:
|
|
838
|
+
#
|
|
839
|
+
# bytes = []
|
|
840
|
+
# strio = StringIO.new('こんにちは')
|
|
841
|
+
# strio.getc # => "こ"
|
|
842
|
+
# strio.pos # => 3 # 3-byte character was read.
|
|
843
|
+
# strio.each_byte {|byte| bytes.push(byte) }
|
|
844
|
+
# bytes # => [227, 130, 147, 227, 129, 171, 227, 129, 161, 227, 129, 175]
|
|
845
|
+
#
|
|
846
|
+
# If at end-of-file, does not call the block:
|
|
847
|
+
#
|
|
848
|
+
# strio.eof? # => true
|
|
849
|
+
# strio.each_byte {|byte| fail 'Boo!' }
|
|
850
|
+
# strio.eof? # => true
|
|
851
|
+
#
|
|
852
|
+
# With no block given, returns a new [Enumerator](rdoc-ref:Enumerator).
|
|
853
|
+
#
|
|
854
|
+
# Related: StringIO#each_char, StringIO#each_codepoint, StringIO#each_line.
|
|
145
855
|
#
|
|
146
856
|
def each_byte: () { (Integer arg0) -> untyped } -> self
|
|
147
857
|
| () -> ::Enumerator[Integer, self]
|
|
148
858
|
|
|
149
859
|
# <!--
|
|
150
860
|
# rdoc-file=ext/stringio/stringio.c
|
|
151
|
-
# - each_char {|
|
|
861
|
+
# - each_char {|char| ... } -> self
|
|
152
862
|
# -->
|
|
153
863
|
# With a block given, calls the block with each remaining character in the
|
|
154
|
-
# stream;
|
|
864
|
+
# stream; positions the stream at end-of-file; returns `self`:
|
|
865
|
+
#
|
|
866
|
+
# chars = []
|
|
867
|
+
# strio = StringIO.new('hello')
|
|
868
|
+
# strio.each_char {|char| chars.push(char) }
|
|
869
|
+
# strio.eof? # => true
|
|
870
|
+
# chars # => ["h", "e", "l", "l", "o"]
|
|
871
|
+
# chars = []
|
|
872
|
+
# strio = StringIO.new('тест')
|
|
873
|
+
# strio.each_char {|char| chars.push(char) }
|
|
874
|
+
# chars # => ["т", "е", "с", "т"]
|
|
875
|
+
# chars = []
|
|
876
|
+
# strio = StringIO.new('こんにちは')
|
|
877
|
+
# strio.each_char {|char| chars.push(char) }
|
|
878
|
+
# chars # => ["こ", "ん", "に", "ち", "は"]
|
|
879
|
+
#
|
|
880
|
+
# Stream position matters:
|
|
881
|
+
#
|
|
882
|
+
# chars = []
|
|
883
|
+
# strio = StringIO.new('こんにちは')
|
|
884
|
+
# strio.getc # => "こ"
|
|
885
|
+
# strio.pos # => 3 # 3-byte character was read.
|
|
886
|
+
# strio.each_char {|char| chars.push(char) }
|
|
887
|
+
# chars # => ["ん", "に", "ち", "は"]
|
|
888
|
+
#
|
|
889
|
+
# When at end-of-stream does not call the block:
|
|
890
|
+
#
|
|
891
|
+
# strio.eof? # => true
|
|
892
|
+
# strio.each_char {|char| fail 'Boo!' }
|
|
893
|
+
# strio.eof? # => true
|
|
155
894
|
#
|
|
156
|
-
# With no block given, returns
|
|
895
|
+
# With no block given, returns a new [Enumerator](rdoc-ref:Enumerator).
|
|
896
|
+
#
|
|
897
|
+
# Related: StringIO#each_byte, StringIO#each_codepoint, StringIO#each_line.
|
|
157
898
|
#
|
|
158
899
|
def each_char: () { (String arg0) -> untyped } -> self
|
|
159
900
|
| () -> ::Enumerator[String, self]
|
|
@@ -162,10 +903,43 @@ class StringIO
|
|
|
162
903
|
# rdoc-file=ext/stringio/stringio.c
|
|
163
904
|
# - each_codepoint {|codepoint| ... } -> self
|
|
164
905
|
# -->
|
|
165
|
-
# With a block given, calls the block with each
|
|
166
|
-
# stream;
|
|
906
|
+
# With a block given, calls the block with each successive codepoint from self;
|
|
907
|
+
# sets the position to end-of-stream; returns `self`.
|
|
908
|
+
#
|
|
909
|
+
# Each codepoint is the integer value for a character; returns self:
|
|
910
|
+
#
|
|
911
|
+
# codepoints = []
|
|
912
|
+
# strio = StringIO.new('hello')
|
|
913
|
+
# strio.each_codepoint {|codepoint| codepoints.push(codepoint) }
|
|
914
|
+
# strio.eof? # => true
|
|
915
|
+
# codepoints # => [104, 101, 108, 108, 111]
|
|
916
|
+
# codepoints = []
|
|
917
|
+
# strio = StringIO.new('тест')
|
|
918
|
+
# strio.each_codepoint {|codepoint| codepoints.push(codepoint) }
|
|
919
|
+
# codepoints # => [1090, 1077, 1089, 1090]
|
|
920
|
+
# codepoints = []
|
|
921
|
+
# strio = StringIO.new('こんにちは')
|
|
922
|
+
# strio.each_codepoint {|codepoint| codepoints.push(codepoint) }
|
|
923
|
+
# codepoints # => [12371, 12435, 12395, 12385, 12399]
|
|
924
|
+
#
|
|
925
|
+
# Position in the stream matters:
|
|
926
|
+
#
|
|
927
|
+
# codepoints = []
|
|
928
|
+
# strio = StringIO.new('こんにちは')
|
|
929
|
+
# strio.getc # => "こ"
|
|
930
|
+
# strio.pos # => 3
|
|
931
|
+
# strio.each_codepoint {|codepoint| codepoints.push(codepoint) }
|
|
932
|
+
# codepoints # => [12435, 12395, 12385, 12399]
|
|
167
933
|
#
|
|
168
|
-
#
|
|
934
|
+
# When at end-of-stream, the block is not called:
|
|
935
|
+
#
|
|
936
|
+
# strio.eof? # => true
|
|
937
|
+
# strio.each_codepoint {|codepoint| fail 'Boo!' }
|
|
938
|
+
# strio.eof? # => true
|
|
939
|
+
#
|
|
940
|
+
# With no block given, returns a new [Enumerator](rdoc-ref:Enumerator).
|
|
941
|
+
#
|
|
942
|
+
# Related: StringIO#each_byte, StringIO#each_char, StringIO#each_line.
|
|
169
943
|
#
|
|
170
944
|
def each_codepoint: () { (Integer arg0) -> untyped } -> self
|
|
171
945
|
| () -> ::Enumerator[Integer, self]
|
|
@@ -174,10 +948,18 @@ class StringIO
|
|
|
174
948
|
# rdoc-file=ext/stringio/stringio.c
|
|
175
949
|
# - eof? -> true or false
|
|
176
950
|
# -->
|
|
177
|
-
# Returns `
|
|
178
|
-
# [Position](rdoc-ref:IO@Position).
|
|
951
|
+
# Returns whether `self` is positioned at end-of-stream:
|
|
179
952
|
#
|
|
180
|
-
#
|
|
953
|
+
# strio = StringIO.new('foo')
|
|
954
|
+
# strio.pos # => 0
|
|
955
|
+
# strio.eof? # => false
|
|
956
|
+
# strio.read # => "foo"
|
|
957
|
+
# strio.pos # => 3
|
|
958
|
+
# strio.eof? # => true
|
|
959
|
+
# strio.close_read
|
|
960
|
+
# strio.eof? # Raises IOError: not opened for reading
|
|
961
|
+
#
|
|
962
|
+
# Related: StringIO#pos.
|
|
181
963
|
#
|
|
182
964
|
def eof: () -> bool
|
|
183
965
|
|
|
@@ -193,7 +975,7 @@ class StringIO
|
|
|
193
975
|
# rdoc-file=ext/stringio/stringio.c
|
|
194
976
|
# - fileno()
|
|
195
977
|
# -->
|
|
196
|
-
# Returns `nil
|
|
978
|
+
# Returns `nil`; for compatibility with IO.
|
|
197
979
|
#
|
|
198
980
|
def fileno: () -> nil
|
|
199
981
|
|
|
@@ -201,7 +983,7 @@ class StringIO
|
|
|
201
983
|
# rdoc-file=ext/stringio/stringio.c
|
|
202
984
|
# - flush()
|
|
203
985
|
# -->
|
|
204
|
-
# Returns
|
|
986
|
+
# Returns `self`; for compatibility with IO.
|
|
205
987
|
#
|
|
206
988
|
def flush: () -> self
|
|
207
989
|
|
|
@@ -209,25 +991,86 @@ class StringIO
|
|
|
209
991
|
# rdoc-file=ext/stringio/stringio.c
|
|
210
992
|
# - fsync()
|
|
211
993
|
# -->
|
|
212
|
-
# Returns 0
|
|
994
|
+
# Returns 0; for compatibility with IO.
|
|
213
995
|
#
|
|
214
996
|
def fsync: () -> Integer?
|
|
215
997
|
|
|
216
998
|
# <!--
|
|
217
999
|
# rdoc-file=ext/stringio/stringio.c
|
|
218
|
-
# - getbyte ->
|
|
1000
|
+
# - getbyte -> integer or nil
|
|
219
1001
|
# -->
|
|
220
|
-
# Reads and returns the next
|
|
221
|
-
#
|
|
1002
|
+
# Reads and returns the next integer byte (not character) from the stream:
|
|
1003
|
+
#
|
|
1004
|
+
# s = 'foo'
|
|
1005
|
+
# s.bytes # => [102, 111, 111]
|
|
1006
|
+
# strio = StringIO.new(s)
|
|
1007
|
+
# strio.getbyte # => 102
|
|
1008
|
+
# strio.getbyte # => 111
|
|
1009
|
+
# strio.getbyte # => 111
|
|
1010
|
+
#
|
|
1011
|
+
# Returns `nil` if at end-of-stream:
|
|
1012
|
+
#
|
|
1013
|
+
# strio.eof? # => true
|
|
1014
|
+
# strio.getbyte # => nil
|
|
1015
|
+
#
|
|
1016
|
+
# Returns a byte, not a character:
|
|
1017
|
+
#
|
|
1018
|
+
# s = 'Привет'
|
|
1019
|
+
# s.bytes
|
|
1020
|
+
# # => [208, 159, 209, 128, 208, 184, 208, 178, 208, 181, 209, 130]
|
|
1021
|
+
# strio = StringIO.new(s)
|
|
1022
|
+
# strio.getbyte # => 208
|
|
1023
|
+
# strio.getbyte # => 159
|
|
1024
|
+
#
|
|
1025
|
+
# s = 'こんにちは'
|
|
1026
|
+
# s.bytes
|
|
1027
|
+
# # => [227, 129, 147, 227, 130, 147, 227, 129, 171, 227, 129, 161, 227, 129, 175]
|
|
1028
|
+
# strio = StringIO.new(s)
|
|
1029
|
+
# strio.getbyte # => 227
|
|
1030
|
+
# strio.getbyte # => 129
|
|
1031
|
+
#
|
|
1032
|
+
# Related: #each_byte, #ungetbyte, #getc.
|
|
222
1033
|
#
|
|
223
1034
|
def getbyte: () -> Integer?
|
|
224
1035
|
|
|
225
1036
|
# <!--
|
|
226
1037
|
# rdoc-file=ext/stringio/stringio.c
|
|
227
|
-
# - getc -> character or nil
|
|
1038
|
+
# - getc -> character, byte, or nil
|
|
228
1039
|
# -->
|
|
229
|
-
# Reads and returns the next character from the stream
|
|
230
|
-
#
|
|
1040
|
+
# Reads and returns the next character (or byte; see below) from the stream:
|
|
1041
|
+
#
|
|
1042
|
+
# strio = StringIO.new('foo')
|
|
1043
|
+
# strio.getc # => "f"
|
|
1044
|
+
# strio.getc # => "o"
|
|
1045
|
+
# strio.getc # => "o"
|
|
1046
|
+
#
|
|
1047
|
+
# Returns `nil` if at end-of-stream:
|
|
1048
|
+
#
|
|
1049
|
+
# strio.eof? # => true
|
|
1050
|
+
# strio.getc # => nil
|
|
1051
|
+
#
|
|
1052
|
+
# Returns characters, not bytes:
|
|
1053
|
+
#
|
|
1054
|
+
# strio = StringIO.new('Привет')
|
|
1055
|
+
# strio.getc # => "П"
|
|
1056
|
+
# strio.getc # => "р"
|
|
1057
|
+
#
|
|
1058
|
+
# strio = StringIO.new('こんにちは')
|
|
1059
|
+
# strio.getc # => "こ"
|
|
1060
|
+
# strio.getc # => "ん"
|
|
1061
|
+
#
|
|
1062
|
+
# In each of the examples above, the stream is positioned at the beginning of a
|
|
1063
|
+
# character; in other cases that need not be true:
|
|
1064
|
+
#
|
|
1065
|
+
# strio = StringIO.new('こんにちは') # Five 3-byte characters.
|
|
1066
|
+
# strio.pos = 3 # => 3 # At beginning of second character; returns character.
|
|
1067
|
+
# strio.getc # => "ん"
|
|
1068
|
+
# strio.pos = 4 # => 4 # At second byte of second character; returns byte.
|
|
1069
|
+
# strio.getc # => "\x82"
|
|
1070
|
+
# strio.pos = 5 # => 5 # At third byte of second character; returns byte.
|
|
1071
|
+
# strio.getc # => "\x93"
|
|
1072
|
+
#
|
|
1073
|
+
# Related: #getbyte, #putc, #ungetc.
|
|
231
1074
|
#
|
|
232
1075
|
def getc: () -> String?
|
|
233
1076
|
|
|
@@ -237,26 +1080,131 @@ class StringIO
|
|
|
237
1080
|
# - gets(limit, chomp: false) -> string or nil
|
|
238
1081
|
# - gets(sep, limit, chomp: false) -> string or nil
|
|
239
1082
|
# -->
|
|
240
|
-
# Reads and returns a line from the stream;
|
|
241
|
-
#
|
|
1083
|
+
# Reads and returns a line from the stream; returns `nil` if at end-of-stream.
|
|
1084
|
+
#
|
|
1085
|
+
# Side effects:
|
|
1086
|
+
#
|
|
1087
|
+
# * Increments stream position by the number of bytes read.
|
|
1088
|
+
# * Assigns the return value to global variable `$_`.
|
|
1089
|
+
#
|
|
1090
|
+
# With no arguments given, reads a line using the default record separator
|
|
1091
|
+
# (global variable `$/`,* whose initial value is `"\n"`):
|
|
1092
|
+
#
|
|
1093
|
+
# strio = StringIO.new(TEXT)
|
|
1094
|
+
# strio.pos # => 0
|
|
1095
|
+
# strio.gets # => "First line\n"
|
|
1096
|
+
# strio.pos # => 11
|
|
1097
|
+
# $_ # => "First line\n"
|
|
1098
|
+
# strio.gets # => "Second line\n"
|
|
1099
|
+
# strio.read # => "\nFourth line\nFifth line\n"
|
|
1100
|
+
# strio.eof? # => true
|
|
1101
|
+
# strio.gets # => nil
|
|
1102
|
+
#
|
|
1103
|
+
# strio = StringIO.new('Привет') # Six 2-byte characters
|
|
1104
|
+
# strio.pos # => 0
|
|
1105
|
+
# strio.gets # => "Привет"
|
|
1106
|
+
# strio.pos # => 12
|
|
1107
|
+
#
|
|
1108
|
+
# **Argument `sep`**
|
|
1109
|
+
#
|
|
1110
|
+
# With only string argument `sep` given, reads a line using that string as the
|
|
1111
|
+
# record separator:
|
|
1112
|
+
#
|
|
1113
|
+
# strio = StringIO.new(TEXT)
|
|
1114
|
+
# strio.gets(' ') # => "First "
|
|
1115
|
+
# strio.gets(' ') # => "line\nSecond "
|
|
1116
|
+
# strio.gets(' ') # => "line\n\nFourth "
|
|
1117
|
+
#
|
|
1118
|
+
# **Argument `limit`**
|
|
1119
|
+
#
|
|
1120
|
+
# With only integer argument `limit` given, reads a line using the default
|
|
1121
|
+
# record separator; limits the size (in characters) of each line to the given
|
|
1122
|
+
# limit:
|
|
1123
|
+
#
|
|
1124
|
+
# strio = StringIO.new(TEXT)
|
|
1125
|
+
# strio.gets(10) # => "First line"
|
|
1126
|
+
# strio.gets(10) # => "\n"
|
|
1127
|
+
# strio.gets(10) # => "Second lin"
|
|
1128
|
+
# strio.gets(10) # => "e\n"
|
|
1129
|
+
#
|
|
1130
|
+
# **Arguments `sep` and `limit`**
|
|
1131
|
+
#
|
|
1132
|
+
# With arguments `sep` and `limit` both given, honors both:
|
|
1133
|
+
#
|
|
1134
|
+
# strio = StringIO.new(TEXT)
|
|
1135
|
+
# strio.gets(' ', 10) # => "First "
|
|
1136
|
+
# strio.gets(' ', 10) # => "line\nSecon"
|
|
1137
|
+
# strio.gets(' ', 10) # => "d "
|
|
1138
|
+
#
|
|
1139
|
+
# **Position**
|
|
1140
|
+
#
|
|
1141
|
+
# As stated above, method `gets` reads and returns the next line in the stream.
|
|
1142
|
+
#
|
|
1143
|
+
# In the examples above each `strio` object starts with its position at
|
|
1144
|
+
# beginning-of-stream; but in other cases the position may be anywhere:
|
|
1145
|
+
#
|
|
1146
|
+
# strio = StringIO.new(TEXT)
|
|
1147
|
+
# strio.pos = 12
|
|
1148
|
+
# strio.gets # => "econd line\n"
|
|
1149
|
+
#
|
|
1150
|
+
# The position need not be at a character boundary:
|
|
1151
|
+
#
|
|
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ивет"
|
|
1157
|
+
#
|
|
1158
|
+
# **Special Record Separators**
|
|
1159
|
+
#
|
|
1160
|
+
# Like some methods in class IO, method `gets` honors two special record
|
|
1161
|
+
# separators; see [Special Line
|
|
1162
|
+
# Separators](https://docs.ruby-lang.org/en/master/IO.html#class-IO-label-Specia
|
|
1163
|
+
# l+Line+Separator+Values):
|
|
1164
|
+
#
|
|
1165
|
+
# strio = StringIO.new(TEXT)
|
|
1166
|
+
# strio.gets('') # Read "paragraph" (up to empty line).
|
|
1167
|
+
# # => "First line\nSecond line\n\n"
|
|
1168
|
+
#
|
|
1169
|
+
# strio = StringIO.new(TEXT)
|
|
1170
|
+
# strio.gets(nil) # "Slurp": read all.
|
|
1171
|
+
# # => "First line\nSecond line\n\nFourth line\nFifth line\n"
|
|
1172
|
+
#
|
|
1173
|
+
# **Keyword Argument `chomp`**
|
|
1174
|
+
#
|
|
1175
|
+
# With keyword argument `chomp` given as `true` (the default is `false`),
|
|
1176
|
+
# removes the trailing newline (if any) from the returned line:
|
|
1177
|
+
#
|
|
1178
|
+
# strio = StringIO.new(TEXT)
|
|
1179
|
+
# strio.gets # => "First line\n"
|
|
1180
|
+
# strio.gets(chomp: true) # => "Second line"
|
|
1181
|
+
#
|
|
1182
|
+
# Related: #each_line, #readlines, [Kernel#puts](rdoc-ref:Kernel#puts).
|
|
242
1183
|
#
|
|
243
1184
|
def gets: (?String sep, ?Integer limit, ?chomp: boolish) -> String?
|
|
244
1185
|
|
|
245
1186
|
# <!--
|
|
246
1187
|
# rdoc-file=ext/stringio/stringio.c
|
|
247
|
-
# -
|
|
1188
|
+
# - internal_encoding -> nil
|
|
248
1189
|
# -->
|
|
249
|
-
# Returns
|
|
250
|
-
# Otherwise returns `nil`.
|
|
1190
|
+
# Returns `nil`; for compatibility with IO.
|
|
251
1191
|
#
|
|
252
1192
|
def internal_encoding: () -> Encoding
|
|
253
1193
|
|
|
254
1194
|
# <!--
|
|
255
1195
|
# rdoc-file=ext/stringio/stringio.c
|
|
256
|
-
# -
|
|
1196
|
+
# - external_encoding -> encoding or nil
|
|
257
1197
|
# -->
|
|
258
|
-
# Returns
|
|
259
|
-
#
|
|
1198
|
+
# Returns an Encoding object that represents the encoding of the string; see
|
|
1199
|
+
# [Encodings](rdoc-ref:StringIO@Encodings):
|
|
1200
|
+
#
|
|
1201
|
+
# strio = StringIO.new('foo')
|
|
1202
|
+
# strio.external_encoding # => #<Encoding:UTF-8>
|
|
1203
|
+
#
|
|
1204
|
+
# Returns `nil` if `self` has no string and is in write mode:
|
|
1205
|
+
#
|
|
1206
|
+
# strio = StringIO.new(nil, 'w+')
|
|
1207
|
+
# strio.external_encoding # => nil
|
|
260
1208
|
#
|
|
261
1209
|
def external_encoding: () -> Encoding
|
|
262
1210
|
|
|
@@ -264,7 +1212,7 @@ class StringIO
|
|
|
264
1212
|
# rdoc-file=ext/stringio/stringio.c
|
|
265
1213
|
# - isatty()
|
|
266
1214
|
# -->
|
|
267
|
-
# Returns `false
|
|
1215
|
+
# Returns `false`; for compatibility with IO.
|
|
268
1216
|
#
|
|
269
1217
|
def isatty: () -> bool
|
|
270
1218
|
|
|
@@ -273,7 +1221,7 @@ class StringIO
|
|
|
273
1221
|
# - lineno -> current_line_number
|
|
274
1222
|
# -->
|
|
275
1223
|
# Returns the current line number in `self`; see [Line
|
|
276
|
-
# Number](rdoc-ref:
|
|
1224
|
+
# Number](rdoc-ref:StringIO@Line+Number).
|
|
277
1225
|
#
|
|
278
1226
|
def lineno: () -> Integer
|
|
279
1227
|
|
|
@@ -282,7 +1230,7 @@ class StringIO
|
|
|
282
1230
|
# - lineno = new_line_number -> new_line_number
|
|
283
1231
|
# -->
|
|
284
1232
|
# Sets the current line number in `self` to the given `new_line_number`; see
|
|
285
|
-
# [Line Number](rdoc-ref:
|
|
1233
|
+
# [Line Number](rdoc-ref:StringIO@Line+Number).
|
|
286
1234
|
#
|
|
287
1235
|
def lineno=: (Integer arg0) -> Integer
|
|
288
1236
|
|
|
@@ -290,7 +1238,7 @@ class StringIO
|
|
|
290
1238
|
# rdoc-file=ext/stringio/stringio.c
|
|
291
1239
|
# - pid()
|
|
292
1240
|
# -->
|
|
293
|
-
# Returns `nil
|
|
1241
|
+
# Returns `nil`; for compatibility with IO.
|
|
294
1242
|
#
|
|
295
1243
|
def pid: () -> nil
|
|
296
1244
|
|
|
@@ -298,7 +1246,8 @@ class StringIO
|
|
|
298
1246
|
# rdoc-file=ext/stringio/stringio.c
|
|
299
1247
|
# - pos -> stream_position
|
|
300
1248
|
# -->
|
|
301
|
-
# Returns the current position (in bytes); see
|
|
1249
|
+
# Returns the current position (in bytes); see
|
|
1250
|
+
# [Position](rdoc-ref:StringIO@Position).
|
|
302
1251
|
#
|
|
303
1252
|
def pos: () -> Integer
|
|
304
1253
|
|
|
@@ -306,7 +1255,8 @@ class StringIO
|
|
|
306
1255
|
# rdoc-file=ext/stringio/stringio.c
|
|
307
1256
|
# - pos = new_position -> new_position
|
|
308
1257
|
# -->
|
|
309
|
-
# Sets the current position (in bytes); see
|
|
1258
|
+
# Sets the current position (in bytes); see
|
|
1259
|
+
# [Position](rdoc-ref:StringIO@Position).
|
|
310
1260
|
#
|
|
311
1261
|
def pos=: (Integer arg0) -> Integer
|
|
312
1262
|
|
|
@@ -391,8 +1341,8 @@ class StringIO
|
|
|
391
1341
|
# rdoc-file=ext/stringio/stringio.c
|
|
392
1342
|
# - seek(offset, whence = SEEK_SET) -> 0
|
|
393
1343
|
# -->
|
|
394
|
-
# Sets the
|
|
395
|
-
#
|
|
1344
|
+
# Sets the position to the given integer `offset` (in bytes), with respect to a
|
|
1345
|
+
# given constant `whence`; see [IO#seek](rdoc-ref:IO#seek).
|
|
396
1346
|
#
|
|
397
1347
|
def seek: (Integer amount, ?Integer whence) -> Integer
|
|
398
1348
|
|
|
@@ -432,7 +1382,7 @@ class StringIO
|
|
|
432
1382
|
# rdoc-file=ext/stringio/stringio.c
|
|
433
1383
|
# - string = other_string -> other_string
|
|
434
1384
|
# -->
|
|
435
|
-
#
|
|
1385
|
+
# Replaces the stored string with `other_string`, and sets the position to zero;
|
|
436
1386
|
# returns `other_string`:
|
|
437
1387
|
#
|
|
438
1388
|
# StringIO.open('foo') do |strio|
|
|
@@ -446,16 +1396,19 @@ class StringIO
|
|
|
446
1396
|
# "foo"
|
|
447
1397
|
# "bar"
|
|
448
1398
|
#
|
|
449
|
-
# Related: StringIO#string (returns the
|
|
1399
|
+
# Related: StringIO#string (returns the stored string).
|
|
450
1400
|
#
|
|
451
1401
|
def string=: (String str) -> String
|
|
452
1402
|
|
|
453
1403
|
# <!--
|
|
454
1404
|
# rdoc-file=ext/stringio/stringio.c
|
|
455
|
-
# -
|
|
456
|
-
# - strio.size -> integer
|
|
1405
|
+
# - size -> integer
|
|
457
1406
|
# -->
|
|
458
|
-
# Returns the
|
|
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.
|
|
459
1412
|
#
|
|
460
1413
|
def size: () -> Integer
|
|
461
1414
|
|
|
@@ -483,7 +1436,8 @@ class StringIO
|
|
|
483
1436
|
# rdoc-file=ext/stringio/stringio.c
|
|
484
1437
|
# - pos -> stream_position
|
|
485
1438
|
# -->
|
|
486
|
-
# Returns the current position (in bytes); see
|
|
1439
|
+
# Returns the current position (in bytes); see
|
|
1440
|
+
# [Position](rdoc-ref:StringIO@Position).
|
|
487
1441
|
#
|
|
488
1442
|
def tell: () -> Integer
|
|
489
1443
|
|
|
@@ -497,7 +1451,7 @@ class StringIO
|
|
|
497
1451
|
def truncate: (Integer) -> 0
|
|
498
1452
|
|
|
499
1453
|
# <!-- rdoc-file=ext/stringio/stringio.c -->
|
|
500
|
-
# Returns `false
|
|
1454
|
+
# Returns `false`; for compatibility with IO.
|
|
501
1455
|
#
|
|
502
1456
|
def tty?: () -> bool
|
|
503
1457
|
|
|
@@ -546,17 +1500,154 @@ class StringIO
|
|
|
546
1500
|
| () -> ::Enumerator[Integer, self]
|
|
547
1501
|
|
|
548
1502
|
# <!-- rdoc-file=ext/stringio/stringio.c -->
|
|
549
|
-
#
|
|
550
|
-
#
|
|
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.
|
|
551
1634
|
#
|
|
552
1635
|
def each_line: (?String sep, ?Integer limit, ?chomp: boolish) { (String) -> untyped } -> self
|
|
553
1636
|
| (?String sep, ?Integer limit, ?chomp: boolish) -> ::Enumerator[String, self]
|
|
554
1637
|
|
|
555
1638
|
# <!-- rdoc-file=ext/stringio/stringio.c -->
|
|
556
|
-
# Returns `
|
|
557
|
-
#
|
|
558
|
-
#
|
|
559
|
-
#
|
|
1639
|
+
# Returns whether `self` is positioned at end-of-stream:
|
|
1640
|
+
#
|
|
1641
|
+
# strio = StringIO.new('foo')
|
|
1642
|
+
# strio.pos # => 0
|
|
1643
|
+
# strio.eof? # => false
|
|
1644
|
+
# strio.read # => "foo"
|
|
1645
|
+
# strio.pos # => 3
|
|
1646
|
+
# strio.eof? # => true
|
|
1647
|
+
# strio.close_read
|
|
1648
|
+
# strio.eof? # Raises IOError: not opened for reading
|
|
1649
|
+
#
|
|
1650
|
+
# Related: StringIO#pos.
|
|
560
1651
|
#
|
|
561
1652
|
def eof?: () -> bool
|
|
562
1653
|
|