rbs 4.0.0.dev.4 → 4.1.0.pre.2
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 +1 -0
- data/.github/dependabot.yml +16 -14
- data/.github/workflows/bundle-update.yml +63 -0
- data/.github/workflows/c-check.yml +21 -11
- data/.github/workflows/comments.yml +5 -3
- data/.github/workflows/dependabot.yml +2 -2
- data/.github/workflows/jruby.yml +67 -0
- data/.github/workflows/milestone.yml +83 -0
- data/.github/workflows/ruby.yml +63 -24
- data/.github/workflows/rust.yml +184 -0
- data/.github/workflows/truffleruby.yml +54 -0
- data/.github/workflows/typecheck.yml +5 -2
- data/.github/workflows/wasm.yml +53 -0
- data/.github/workflows/windows.yml +8 -2
- data/.gitignore +11 -0
- data/.rubocop.yml +1 -1
- data/CHANGELOG.md +357 -0
- data/README.md +4 -4
- data/Rakefile +365 -33
- data/Steepfile +8 -0
- data/config.yml +450 -24
- data/core/array.rbs +443 -363
- data/core/basic_object.rbs +9 -8
- data/core/binding.rbs +0 -2
- data/core/builtin.rbs +9 -8
- data/core/class.rbs +11 -8
- data/core/comparable.rbs +55 -34
- data/core/complex.rbs +104 -78
- data/core/dir.rbs +61 -49
- data/core/encoding.rbs +12 -15
- data/core/enumerable.rbs +288 -196
- data/core/enumerator/arithmetic_sequence.rbs +70 -0
- data/core/enumerator/product.rbs +5 -5
- data/core/enumerator.rbs +91 -28
- data/core/errno.rbs +11 -2
- data/core/errors.rbs +58 -29
- data/core/exception.rbs +13 -13
- data/core/fiber.rbs +74 -54
- data/core/file.rbs +260 -1151
- data/core/file_constants.rbs +463 -0
- data/core/file_stat.rbs +534 -0
- data/core/file_test.rbs +3 -3
- data/core/float.rbs +257 -92
- data/core/gc.rbs +425 -281
- data/core/hash.rbs +1151 -829
- data/core/integer.rbs +156 -195
- data/core/io/buffer.rbs +53 -42
- data/core/io/wait.rbs +13 -35
- data/core/io.rbs +216 -150
- data/core/kernel.rbs +239 -163
- data/core/marshal.rbs +4 -4
- data/core/match_data.rbs +15 -13
- data/core/math.rbs +107 -66
- data/core/method.rbs +69 -33
- data/core/module.rbs +302 -150
- data/core/nil_class.rbs +7 -6
- data/core/numeric.rbs +77 -63
- data/core/object.rbs +9 -11
- data/core/object_space/weak_key_map.rbs +7 -7
- data/core/object_space.rbs +30 -23
- data/core/pathname.rbs +1322 -0
- data/core/proc.rbs +95 -58
- data/core/process.rbs +222 -202
- data/core/ractor.rbs +371 -515
- data/core/random.rbs +21 -3
- data/core/range.rbs +181 -79
- data/core/rational.rbs +60 -89
- data/core/rbs/ops.rbs +154 -0
- data/core/rbs/unnamed/argf.rbs +63 -56
- data/core/rbs/unnamed/env_class.rbs +19 -14
- data/core/rbs/unnamed/main_class.rbs +123 -0
- data/core/rbs/unnamed/random.rbs +11 -118
- data/core/regexp.rbs +258 -214
- data/core/ruby.rbs +53 -0
- data/core/ruby_vm.rbs +78 -34
- data/core/rubygems/config_file.rbs +5 -5
- data/core/rubygems/errors.rbs +4 -71
- data/core/rubygems/requirement.rbs +5 -5
- data/core/rubygems/rubygems.rbs +16 -82
- data/core/rubygems/version.rbs +2 -3
- data/core/set.rbs +493 -363
- data/core/signal.rbs +26 -16
- data/core/string.rbs +3234 -1285
- data/core/struct.rbs +43 -42
- data/core/symbol.rbs +41 -34
- data/core/thread.rbs +141 -73
- data/core/time.rbs +81 -50
- data/core/trace_point.rbs +41 -35
- data/core/true_class.rbs +2 -2
- data/core/unbound_method.rbs +24 -16
- data/core/warning.rbs +7 -7
- data/docs/CONTRIBUTING.md +2 -1
- data/docs/aliases.md +79 -0
- data/docs/collection.md +3 -3
- data/docs/config.md +171 -0
- data/docs/encoding.md +56 -0
- data/docs/gem.md +0 -1
- data/docs/inline.md +634 -0
- data/docs/rbs_by_example.md +20 -20
- data/docs/rust.md +96 -0
- data/docs/sigs.md +3 -3
- data/docs/syntax.md +48 -18
- data/docs/type_fingerprint.md +21 -0
- data/docs/wasm_serialization.md +80 -0
- data/exe/rbs +1 -1
- data/ext/rbs_extension/ast_translation.c +1441 -671
- data/ext/rbs_extension/ast_translation.h +7 -0
- data/ext/rbs_extension/class_constants.c +18 -2
- data/ext/rbs_extension/class_constants.h +9 -0
- data/ext/rbs_extension/extconf.rb +6 -1
- data/ext/rbs_extension/legacy_location.c +33 -56
- data/ext/rbs_extension/legacy_location.h +37 -0
- data/ext/rbs_extension/main.c +183 -39
- data/include/rbs/ast.h +597 -297
- data/include/rbs/defines.h +40 -0
- data/include/rbs/lexer.h +31 -11
- data/include/rbs/location.h +25 -44
- data/include/rbs/parser.h +6 -6
- data/include/rbs/serialize.h +39 -0
- data/include/rbs/string.h +0 -2
- data/include/rbs/util/rbs_allocator.h +34 -13
- data/include/rbs/util/rbs_assert.h +12 -1
- data/include/rbs/util/rbs_constant_pool.h +0 -3
- data/include/rbs/util/rbs_encoding.h +2 -0
- data/include/rbs/util/rbs_unescape.h +2 -1
- data/include/rbs.h +8 -0
- data/lib/rbs/annotate/rdoc_annotator.rb +27 -31
- data/lib/rbs/ast/annotation.rb +1 -1
- data/lib/rbs/ast/comment.rb +1 -1
- data/lib/rbs/ast/declarations.rb +10 -10
- data/lib/rbs/ast/members.rb +14 -14
- data/lib/rbs/ast/ruby/annotations.rb +335 -3
- data/lib/rbs/ast/ruby/comment_block.rb +30 -4
- data/lib/rbs/ast/ruby/declarations.rb +209 -4
- data/lib/rbs/ast/ruby/helpers/constant_helper.rb +4 -0
- data/lib/rbs/ast/ruby/helpers/location_helper.rb +1 -1
- data/lib/rbs/ast/ruby/members.rb +571 -22
- data/lib/rbs/ast/type_param.rb +24 -4
- data/lib/rbs/buffer.rb +66 -24
- data/lib/rbs/cli/diff.rb +16 -15
- data/lib/rbs/cli/validate.rb +38 -106
- data/lib/rbs/cli.rb +55 -24
- data/lib/rbs/collection/config/lockfile_generator.rb +28 -3
- data/lib/rbs/collection/sources/git.rb +7 -0
- data/lib/rbs/definition.rb +1 -1
- data/lib/rbs/definition_builder/ancestor_builder.rb +62 -9
- data/lib/rbs/definition_builder/method_builder.rb +32 -6
- data/lib/rbs/definition_builder.rb +147 -25
- data/lib/rbs/diff.rb +7 -1
- data/lib/rbs/environment.rb +235 -75
- data/lib/rbs/environment_loader.rb +0 -6
- data/lib/rbs/errors.rb +27 -18
- data/lib/rbs/inline_parser.rb +377 -15
- data/lib/rbs/location_aux.rb +1 -1
- data/lib/rbs/locator.rb +5 -1
- data/lib/rbs/method_type.rb +5 -3
- data/lib/rbs/namespace.rb +47 -11
- data/lib/rbs/parser_aux.rb +20 -7
- data/lib/rbs/prototype/helpers.rb +57 -0
- data/lib/rbs/prototype/rb.rb +3 -28
- data/lib/rbs/prototype/rbi.rb +3 -20
- data/lib/rbs/prototype/runtime.rb +10 -0
- data/lib/rbs/resolver/constant_resolver.rb +2 -2
- data/lib/rbs/resolver/type_name_resolver.rb +120 -44
- data/lib/rbs/rewriter.rb +70 -0
- data/lib/rbs/subtractor.rb +3 -1
- data/lib/rbs/test/type_check.rb +25 -3
- data/lib/rbs/type_name.rb +34 -14
- data/lib/rbs/types.rb +88 -78
- data/lib/rbs/unit_test/type_assertions.rb +44 -8
- data/lib/rbs/validator.rb +2 -2
- data/lib/rbs/version.rb +1 -1
- data/lib/rbs/wasm/deserializer.rb +213 -0
- data/lib/rbs/wasm/location.rb +61 -0
- data/lib/rbs/wasm/parser.rb +137 -0
- data/lib/rbs/wasm/runtime.rb +217 -0
- data/lib/rbs/wasm/serialization_schema.rb +110 -0
- data/lib/rbs.rb +13 -2
- data/lib/rdoc/discover.rb +1 -1
- data/lib/rdoc_plugin/parser.rb +1 -1
- data/rbs.gemspec +24 -6
- data/schema/typeParam.json +17 -1
- data/sig/annotate/rdoc_annotater.rbs +12 -9
- data/sig/ast/ruby/annotations.rbs +364 -4
- data/sig/ast/ruby/comment_block.rbs +8 -0
- data/sig/ast/ruby/declarations.rbs +102 -4
- data/sig/ast/ruby/members.rbs +128 -2
- data/sig/buffer.rbs +19 -1
- data/sig/cli/diff.rbs +5 -11
- data/sig/cli/validate.rbs +12 -8
- data/sig/cli.rbs +18 -18
- data/sig/collection/config/lockfile_generator.rbs +2 -0
- data/sig/definition.rbs +6 -1
- data/sig/definition_builder.rbs +2 -0
- data/sig/environment.rbs +70 -12
- data/sig/errors.rbs +13 -14
- data/sig/inline_parser.rbs +41 -2
- data/sig/locator.rbs +0 -2
- data/sig/manifest.yaml +0 -2
- data/sig/method_builder.rbs +3 -1
- data/sig/namespace.rbs +20 -0
- data/sig/parser.rbs +41 -13
- data/sig/prototype/helpers.rbs +2 -0
- data/sig/resolver/type_name_resolver.rbs +36 -10
- data/sig/rewriter.rbs +45 -0
- data/sig/source.rbs +3 -3
- data/sig/type_param.rbs +13 -8
- data/sig/typename.rbs +15 -0
- data/sig/types.rbs +6 -7
- data/sig/unit_test/spy.rbs +0 -8
- data/sig/unit_test/type_assertions.rbs +15 -0
- data/sig/wasm/deserializer.rbs +66 -0
- data/sig/wasm/serialization_schema.rbs +13 -0
- data/src/ast.c +443 -162
- data/src/lexer.c +1415 -1313
- data/src/lexer.re +4 -0
- data/src/lexstate.c +63 -37
- data/src/location.c +7 -47
- data/src/parser.c +1032 -521
- data/src/serialize.c +958 -0
- data/src/string.c +0 -48
- data/src/util/rbs_allocator.c +89 -74
- data/src/util/rbs_assert.c +1 -1
- data/src/util/rbs_buffer.c +2 -2
- data/src/util/rbs_constant_pool.c +10 -14
- data/src/util/rbs_encoding.c +4 -8
- data/src/util/rbs_unescape.c +56 -20
- data/stdlib/abbrev/0/array.rbs +1 -1
- data/stdlib/bigdecimal/0/big_decimal.rbs +116 -98
- data/stdlib/bigdecimal-math/0/big_math.rbs +169 -8
- data/stdlib/cgi/0/core.rbs +9 -393
- data/stdlib/cgi/0/manifest.yaml +1 -0
- data/stdlib/cgi-escape/0/escape.rbs +171 -0
- data/stdlib/coverage/0/coverage.rbs +7 -4
- data/stdlib/csv/0/csv.rbs +5 -5
- data/stdlib/date/0/date.rbs +92 -79
- data/stdlib/date/0/date_time.rbs +25 -24
- data/stdlib/delegate/0/delegator.rbs +10 -7
- data/stdlib/did_you_mean/0/did_you_mean.rbs +17 -16
- data/stdlib/digest/0/digest.rbs +111 -1
- data/stdlib/erb/0/erb.rbs +748 -347
- data/stdlib/etc/0/etc.rbs +73 -54
- data/stdlib/fileutils/0/fileutils.rbs +179 -160
- data/stdlib/forwardable/0/forwardable.rbs +13 -10
- data/stdlib/io-console/0/io-console.rbs +2 -2
- data/stdlib/json/0/json.rbs +223 -142
- data/stdlib/monitor/0/monitor.rbs +3 -3
- data/stdlib/net-http/0/net-http.rbs +162 -134
- data/stdlib/objspace/0/objspace.rbs +17 -34
- data/stdlib/open-uri/0/open-uri.rbs +48 -8
- data/stdlib/open3/0/open3.rbs +469 -10
- data/stdlib/openssl/0/openssl.rbs +482 -364
- data/stdlib/optparse/0/optparse.rbs +26 -17
- data/stdlib/pathname/0/pathname.rbs +11 -1381
- data/stdlib/pp/0/pp.rbs +9 -8
- data/stdlib/prettyprint/0/prettyprint.rbs +7 -7
- data/stdlib/pstore/0/pstore.rbs +35 -30
- data/stdlib/psych/0/psych.rbs +65 -12
- data/stdlib/psych/0/store.rbs +2 -4
- data/stdlib/pty/0/pty.rbs +9 -6
- data/stdlib/random-formatter/0/random-formatter.rbs +277 -0
- data/stdlib/rdoc/0/code_object.rbs +2 -1
- data/stdlib/rdoc/0/parser.rbs +1 -1
- data/stdlib/rdoc/0/rdoc.rbs +1 -1
- data/stdlib/rdoc/0/store.rbs +1 -1
- data/stdlib/resolv/0/resolv.rbs +26 -69
- data/stdlib/ripper/0/ripper.rbs +22 -19
- data/stdlib/securerandom/0/manifest.yaml +2 -0
- data/stdlib/securerandom/0/securerandom.rbs +7 -20
- data/stdlib/shellwords/0/shellwords.rbs +3 -3
- data/stdlib/singleton/0/singleton.rbs +3 -0
- data/stdlib/socket/0/addrinfo.rbs +7 -7
- data/stdlib/socket/0/basic_socket.rbs +3 -3
- data/stdlib/socket/0/ip_socket.rbs +10 -8
- data/stdlib/socket/0/socket.rbs +23 -10
- data/stdlib/socket/0/tcp_server.rbs +1 -1
- data/stdlib/socket/0/tcp_socket.rbs +11 -3
- data/stdlib/socket/0/udp_socket.rbs +1 -1
- data/stdlib/socket/0/unix_server.rbs +1 -1
- data/stdlib/stringio/0/stringio.rbs +1209 -95
- data/stdlib/strscan/0/string_scanner.rbs +101 -80
- data/stdlib/tempfile/0/tempfile.rbs +25 -21
- data/stdlib/time/0/time.rbs +8 -6
- data/stdlib/timeout/0/timeout.rbs +63 -7
- data/stdlib/tsort/0/cyclic.rbs +4 -1
- data/stdlib/tsort/0/interfaces.rbs +8 -8
- data/stdlib/tsort/0/tsort.rbs +16 -15
- data/stdlib/uri/0/common.rbs +42 -20
- data/stdlib/uri/0/file.rbs +3 -3
- data/stdlib/uri/0/generic.rbs +26 -18
- data/stdlib/uri/0/http.rbs +2 -2
- data/stdlib/uri/0/ldap.rbs +2 -2
- data/stdlib/uri/0/mailto.rbs +3 -3
- data/stdlib/uri/0/rfc2396_parser.rbs +12 -12
- data/stdlib/zlib/0/deflate.rbs +4 -3
- data/stdlib/zlib/0/gzip_reader.rbs +8 -8
- data/stdlib/zlib/0/gzip_writer.rbs +14 -12
- data/stdlib/zlib/0/inflate.rbs +1 -1
- data/stdlib/zlib/0/need_dict.rbs +1 -1
- data/stdlib/zlib/0/zstream.rbs +1 -0
- data/wasm/README.md +59 -0
- data/wasm/rbs_wasm.c +411 -0
- metadata +56 -8
- data/.vscode/extensions.json +0 -5
- data/.vscode/settings.json +0 -19
|
@@ -1,54 +1,567 @@
|
|
|
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
|
+
# * <strong>Read/write mode</strong>: whether the stream may be read, written,
|
|
10
|
+
# appended to, 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
|
+
# * <strong>Open/closed</strong>: whether the stream is open or closed, for
|
|
22
|
+
# reading or 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
|
+
# <code>'r'</code>: read-only | No |Anywhere| Error
|
|
45
|
+
# <code>'w'</code>: write-only | Yes | Error |Anywhere
|
|
46
|
+
# <code>'a'</code>: append-only | No | Error |End only
|
|
47
|
+
# <code>'r+'</code>: read/write | No |Anywhere|Anywhere
|
|
48
|
+
# <code>'w+'</code>: read-write | Yes |Anywhere|Anywhere
|
|
49
|
+
# <code>'a+'</code>: 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
|
+
# #### <code>'r'</code>: Read-Only
|
|
57
|
+
# Mode specified as one of:
|
|
58
|
+
# * String: <code>'r'</code>.
|
|
59
|
+
# * Constant: <code>File::RDONLY</code>.
|
|
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
|
+
# #### <code>'w'</code>: Write-Only
|
|
75
|
+
# Mode specified as one of:
|
|
76
|
+
# * String: <code>'w'</code>.
|
|
77
|
+
# * Constant: <code>File::WRONLY</code>.
|
|
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
|
+
# #### <code>'a'</code>: Append-Only
|
|
100
|
+
# Mode specified as one of:
|
|
101
|
+
# * String: <code>'a'</code>.
|
|
102
|
+
# * Constant: <code>File::WRONLY | File::APPEND</code>.
|
|
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
|
+
# #### <code>'r+'</code>: Read/Write
|
|
121
|
+
# Mode specified as one of:
|
|
122
|
+
# * String: <code>'r+'</code>.
|
|
123
|
+
# * Constant: <code>File::RDRW</code>.
|
|
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
|
+
# #### <code>'w+'</code>: Read/Write (Initially Clear)
|
|
149
|
+
# Mode specified as one of:
|
|
150
|
+
# * String: <code>'w+'</code>.
|
|
151
|
+
# * Constant: <code>File::RDWR | File::TRUNC</code>.
|
|
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
|
+
# #### <code>'a+'</code>: Read/Append
|
|
181
|
+
# Mode specified as one of:
|
|
182
|
+
# * String: <code>'a+'</code>.
|
|
183
|
+
# * Constant: <code>File::RDWR | File::APPEND</code>.
|
|
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
|
+
# * <code>'t'</code>: Text;
|
|
212
|
+
# initializes the encoding as Encoding::UTF_8.
|
|
213
|
+
# * <code>'b'</code>: 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: <code>Encoding::UTF_8</code>.
|
|
235
|
+
# * Binary: <code>Encoding::ASCII_8BIT</code>.
|
|
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: <code>"\n"</code>) 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:
|
|
17
518
|
#
|
|
18
|
-
#
|
|
19
|
-
#
|
|
519
|
+
# strio = StringIO.new
|
|
520
|
+
# strio.string # => ""
|
|
521
|
+
# strio.closed_read? # => false
|
|
522
|
+
# strio.closed_write? # => false
|
|
523
|
+
# strio.close
|
|
524
|
+
#
|
|
525
|
+
# If `string` is frozen, the default `mode` is <code>'r'</code>:
|
|
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:
|
|
535
|
+
#
|
|
536
|
+
# StringIO.new('foo', 'w+')
|
|
537
|
+
# StringIO.new('foo', File::RDONLY)
|
|
25
538
|
#
|
|
26
|
-
# Related: StringIO.open (
|
|
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
|
-
#
|
|
549
|
+
# Creates new StringIO instance by calling <code>StringIO.new(string,
|
|
550
|
+
# mode)</code>.
|
|
35
551
|
#
|
|
36
|
-
#
|
|
37
|
-
# Modes](rdoc-ref:File@Access+Modes).
|
|
38
|
-
#
|
|
39
|
-
# With no block, returns the new instance:
|
|
552
|
+
# With no block given, returns the new instance:
|
|
40
553
|
#
|
|
41
554
|
# strio = StringIO.open # => #<StringIO>
|
|
42
555
|
#
|
|
43
|
-
# With a block, calls the block with the new instance and returns the
|
|
44
|
-
# value; closes the instance on block exit
|
|
556
|
+
# With a block given, calls the block with the new instance and returns the
|
|
557
|
+
# block's value; closes the instance on block exit:
|
|
45
558
|
#
|
|
46
|
-
# StringIO.open {|strio|
|
|
47
|
-
# # => #<StringIO>
|
|
559
|
+
# StringIO.open('foo') {|strio| strio.string.upcase } # => "FOO"
|
|
48
560
|
#
|
|
49
561
|
# Related: StringIO.new.
|
|
50
562
|
#
|
|
51
|
-
def self.open:
|
|
563
|
+
def self.open: (?String string, ?String? mode) -> StringIO
|
|
564
|
+
| [U] (?String string, ?String? mode) { (StringIO arg) -> U } -> U
|
|
52
565
|
|
|
53
566
|
def <<: (untyped arg0) -> self
|
|
54
567
|
|
|
@@ -57,7 +570,7 @@ class StringIO
|
|
|
57
570
|
# - binmode -> self
|
|
58
571
|
# -->
|
|
59
572
|
# Sets the data mode in `self` to binary mode; see [Data
|
|
60
|
-
# Mode](rdoc-ref:
|
|
573
|
+
# Mode](rdoc-ref:StringIO@Data+Mode).
|
|
61
574
|
#
|
|
62
575
|
def binmode: () -> self
|
|
63
576
|
|
|
@@ -65,11 +578,16 @@ class StringIO
|
|
|
65
578
|
# rdoc-file=ext/stringio/stringio.c
|
|
66
579
|
# - close -> nil
|
|
67
580
|
# -->
|
|
68
|
-
# Closes `self` for both reading and writing
|
|
581
|
+
# Closes `self` for both reading and writing; returns `nil`:
|
|
69
582
|
#
|
|
70
|
-
#
|
|
583
|
+
# strio = StringIO.new
|
|
584
|
+
# strio.closed? # => false
|
|
585
|
+
# strio.close # => nil
|
|
586
|
+
# strio.closed? # => true
|
|
587
|
+
# strio.read # Raises IOError: not opened for reading
|
|
588
|
+
# strio.write # Raises IOError: not opened for writing
|
|
71
589
|
#
|
|
72
|
-
# Related: StringIO#close_read, StringIO#close_write.
|
|
590
|
+
# Related: StringIO#close_read, StringIO#close_write, StringIO.closed?.
|
|
73
591
|
#
|
|
74
592
|
def close: () -> nil
|
|
75
593
|
|
|
@@ -77,9 +595,15 @@ class StringIO
|
|
|
77
595
|
# rdoc-file=ext/stringio/stringio.c
|
|
78
596
|
# - close_read -> nil
|
|
79
597
|
# -->
|
|
80
|
-
# Closes `self` for reading; closed-write setting remains unchanged
|
|
598
|
+
# Closes `self` for reading; closed-write setting remains unchanged; returns
|
|
599
|
+
# `nil`:
|
|
81
600
|
#
|
|
82
|
-
#
|
|
601
|
+
# strio = StringIO.new
|
|
602
|
+
# strio.closed_read? # => false
|
|
603
|
+
# strio.close_read # => nil
|
|
604
|
+
# strio.closed_read? # => true
|
|
605
|
+
# strio.closed_write? # => false
|
|
606
|
+
# strio.read # Raises IOError: not opened for reading
|
|
83
607
|
#
|
|
84
608
|
# Related: StringIO#close, StringIO#close_write.
|
|
85
609
|
#
|
|
@@ -89,11 +613,17 @@ class StringIO
|
|
|
89
613
|
# rdoc-file=ext/stringio/stringio.c
|
|
90
614
|
# - close_write -> nil
|
|
91
615
|
# -->
|
|
92
|
-
# Closes `self` for writing; closed-read setting remains unchanged
|
|
616
|
+
# Closes `self` for writing; closed-read setting remains unchanged; returns
|
|
617
|
+
# `nil`:
|
|
93
618
|
#
|
|
94
|
-
#
|
|
619
|
+
# strio = StringIO.new
|
|
620
|
+
# strio.closed_write? # => false
|
|
621
|
+
# strio.close_write # => nil
|
|
622
|
+
# strio.closed_write? # => true
|
|
623
|
+
# strio.closed_read? # => false
|
|
624
|
+
# strio.write('foo') # Raises IOError: not opened for writing
|
|
95
625
|
#
|
|
96
|
-
# Related: StringIO#close, StringIO#close_read
|
|
626
|
+
# Related: StringIO#close, StringIO#close_read, StringIO#closed_write?.
|
|
97
627
|
#
|
|
98
628
|
def close_write: () -> nil
|
|
99
629
|
|
|
@@ -101,8 +631,16 @@ class StringIO
|
|
|
101
631
|
# rdoc-file=ext/stringio/stringio.c
|
|
102
632
|
# - closed? -> true or false
|
|
103
633
|
# -->
|
|
104
|
-
# Returns
|
|
105
|
-
#
|
|
634
|
+
# Returns whether `self` is closed for both reading and writing:
|
|
635
|
+
#
|
|
636
|
+
# strio = StringIO.new
|
|
637
|
+
# strio.closed? # => false # Open for reading and writing.
|
|
638
|
+
# strio.close_read
|
|
639
|
+
# strio.closed? # => false # Still open for writing.
|
|
640
|
+
# strio.close_write
|
|
641
|
+
# strio.closed? # => true # Now closed for both.
|
|
642
|
+
#
|
|
643
|
+
# Related: StringIO.closed_read?, StringIO.closed_write?.
|
|
106
644
|
#
|
|
107
645
|
def closed?: () -> bool
|
|
108
646
|
|
|
@@ -110,7 +648,14 @@ class StringIO
|
|
|
110
648
|
# rdoc-file=ext/stringio/stringio.c
|
|
111
649
|
# - closed_read? -> true or false
|
|
112
650
|
# -->
|
|
113
|
-
# Returns
|
|
651
|
+
# Returns whether `self` is closed for reading:
|
|
652
|
+
#
|
|
653
|
+
# strio = StringIO.new
|
|
654
|
+
# strio.closed_read? # => false
|
|
655
|
+
# strio.close_read
|
|
656
|
+
# strio.closed_read? # => true
|
|
657
|
+
#
|
|
658
|
+
# Related: StringIO#closed?, StringIO#closed_write?, StringIO#close_read.
|
|
114
659
|
#
|
|
115
660
|
def closed_read?: () -> bool
|
|
116
661
|
|
|
@@ -118,7 +663,14 @@ class StringIO
|
|
|
118
663
|
# rdoc-file=ext/stringio/stringio.c
|
|
119
664
|
# - closed_write? -> true or false
|
|
120
665
|
# -->
|
|
121
|
-
# Returns
|
|
666
|
+
# Returns whether `self` is closed for writing:
|
|
667
|
+
#
|
|
668
|
+
# strio = StringIO.new
|
|
669
|
+
# strio.closed_write? # => false
|
|
670
|
+
# strio.close_write
|
|
671
|
+
# strio.closed_write? # => true
|
|
672
|
+
#
|
|
673
|
+
# Related: StringIO#close_write, StringIO#closed?, StringIO#closed_read?.
|
|
122
674
|
#
|
|
123
675
|
def closed_write?: () -> bool
|
|
124
676
|
|
|
@@ -128,8 +680,137 @@ class StringIO
|
|
|
128
680
|
# - each_line(limit, chomp: false) {|line| ... } -> self
|
|
129
681
|
# - each_line(sep, limit, chomp: false) {|line| ... } -> self
|
|
130
682
|
# -->
|
|
131
|
-
#
|
|
132
|
-
#
|
|
683
|
+
# With a block given calls the block with each remaining line (see "Position"
|
|
684
|
+
# below) in the stream;
|
|
685
|
+
# returns `self`.
|
|
686
|
+
# Leaves stream position at end-of-stream.
|
|
687
|
+
# **No Arguments**
|
|
688
|
+
# With no arguments given,
|
|
689
|
+
# reads lines using the default record separator
|
|
690
|
+
# (global variable <code>$/</code>, whose initial value is <code>"\n"</code>).
|
|
691
|
+
# strio = StringIO.new(TEXT)
|
|
692
|
+
# strio.each_line {|line| p line }
|
|
693
|
+
# strio.eof? # => true
|
|
694
|
+
#
|
|
695
|
+
# Output:
|
|
696
|
+
# "First line\n"
|
|
697
|
+
# "Second line\n"
|
|
698
|
+
# "\n"
|
|
699
|
+
# "Fourth line\n"
|
|
700
|
+
# "Fifth line\n"
|
|
701
|
+
#
|
|
702
|
+
# <strong>Argument `sep`</strong>
|
|
703
|
+
# With only string argument `sep` given,
|
|
704
|
+
# reads lines using that string as the record separator:
|
|
705
|
+
# strio = StringIO.new(TEXT)
|
|
706
|
+
# strio.each_line(' ') {|line| p line }
|
|
707
|
+
#
|
|
708
|
+
# Output:
|
|
709
|
+
# "First "
|
|
710
|
+
# "line\nSecond "
|
|
711
|
+
# "line\n\nFourth "
|
|
712
|
+
# "line\nFifth "
|
|
713
|
+
# "line\n"
|
|
714
|
+
#
|
|
715
|
+
# <strong>Argument `limit`</strong>
|
|
716
|
+
# With only integer argument `limit` given,
|
|
717
|
+
# reads lines using the default record separator;
|
|
718
|
+
# also limits the size (in characters) of each line to the given limit:
|
|
719
|
+
# strio = StringIO.new(TEXT)
|
|
720
|
+
# strio.each_line(10) {|line| p line }
|
|
721
|
+
#
|
|
722
|
+
# Output:
|
|
723
|
+
# "First line"
|
|
724
|
+
# "\n"
|
|
725
|
+
# "Second lin"
|
|
726
|
+
# "e\n"
|
|
727
|
+
# "\n"
|
|
728
|
+
# "Fourth lin"
|
|
729
|
+
# "e\n"
|
|
730
|
+
# "Fifth line"
|
|
731
|
+
# "\n"
|
|
732
|
+
#
|
|
733
|
+
# <strong>Arguments `sep` and `limit`</strong>
|
|
734
|
+
# With arguments `sep` and `limit` both given,
|
|
735
|
+
# honors both:
|
|
736
|
+
# strio = StringIO.new(TEXT)
|
|
737
|
+
# strio.each_line(' ', 10) {|line| p line }
|
|
738
|
+
#
|
|
739
|
+
# Output:
|
|
740
|
+
# "First "
|
|
741
|
+
# "line\nSecon"
|
|
742
|
+
# "d "
|
|
743
|
+
# "line\n\nFour"
|
|
744
|
+
# "th "
|
|
745
|
+
# "line\nFifth"
|
|
746
|
+
# " "
|
|
747
|
+
# "line\n"
|
|
748
|
+
#
|
|
749
|
+
# **Position**
|
|
750
|
+
# As stated above, method `each` *remaining* line in the stream.
|
|
751
|
+
# In the examples above each `strio` object starts with its position at
|
|
752
|
+
# beginning-of-stream;
|
|
753
|
+
# but in other cases the position may be anywhere (see StringIO#pos):
|
|
754
|
+
# strio = StringIO.new(TEXT)
|
|
755
|
+
# strio.pos = 30 # Set stream position to character 30.
|
|
756
|
+
# strio.each_line {|line| p line }
|
|
757
|
+
#
|
|
758
|
+
# Output:
|
|
759
|
+
# " line\n"
|
|
760
|
+
# "Fifth line\n"
|
|
761
|
+
#
|
|
762
|
+
# In all the examples above, the stream position is at the beginning of a
|
|
763
|
+
# character;
|
|
764
|
+
# in other cases, that need not be so:
|
|
765
|
+
# s = 'こんにちは' # Five 3-byte characters.
|
|
766
|
+
# strio = StringIO.new(s)
|
|
767
|
+
# strio.pos = 3 # At beginning of second character.
|
|
768
|
+
# strio.each_line {|line| p line }
|
|
769
|
+
# strio.pos = 4 # At second byte of second character.
|
|
770
|
+
# strio.each_line {|line| p line }
|
|
771
|
+
# strio.pos = 5 # At third byte of second character.
|
|
772
|
+
# strio.each_line {|line| p line }
|
|
773
|
+
#
|
|
774
|
+
# Output:
|
|
775
|
+
# "んにちは"
|
|
776
|
+
# "\x82\x93にちは"
|
|
777
|
+
# "\x93にちは"
|
|
778
|
+
#
|
|
779
|
+
# **Special Record Separators**
|
|
780
|
+
# Like some methods in class `IO`, StringIO.each honors two special record
|
|
781
|
+
# separators;
|
|
782
|
+
# see [Special Line
|
|
783
|
+
# Separators](https://docs.ruby-lang.org/en/master/IO.html#class-IO-label-Specia
|
|
784
|
+
# l+Line+Separator+Values).
|
|
785
|
+
# strio = StringIO.new(TEXT)
|
|
786
|
+
# strio.each_line('') {|line| p line } # Read as paragraphs (separated by blank lines).
|
|
787
|
+
#
|
|
788
|
+
# Output:
|
|
789
|
+
# "First line\nSecond line\n\n"
|
|
790
|
+
# "Fourth line\nFifth line\n"
|
|
791
|
+
#
|
|
792
|
+
# strio = StringIO.new(TEXT)
|
|
793
|
+
# strio.each_line(nil) {|line| p line } # "Slurp"; read it all.
|
|
794
|
+
#
|
|
795
|
+
# Output:
|
|
796
|
+
# "First line\nSecond line\n\nFourth line\nFifth line\n"
|
|
797
|
+
#
|
|
798
|
+
# <strong>Keyword Argument `chomp`</strong>
|
|
799
|
+
# With keyword argument `chomp` given as `true` (the default is `false`),
|
|
800
|
+
# removes trailing newline (if any) from each line:
|
|
801
|
+
# strio = StringIO.new(TEXT)
|
|
802
|
+
# strio.each_line(chomp: true) {|line| p line }
|
|
803
|
+
#
|
|
804
|
+
# Output:
|
|
805
|
+
# "First line"
|
|
806
|
+
# "Second line"
|
|
807
|
+
# ""
|
|
808
|
+
# "Fourth line"
|
|
809
|
+
# "Fifth line"
|
|
810
|
+
#
|
|
811
|
+
# With no block given, returns a new
|
|
812
|
+
# [Enumerator](https://docs.ruby-lang.org/en/master/Enumerator.html).
|
|
813
|
+
# Related: StringIO.each_byte, StringIO.each_char, StringIO.each_codepoint.
|
|
133
814
|
#
|
|
134
815
|
def each: (?String sep, ?Integer limit, ?chomp: boolish) { (String) -> untyped } -> self
|
|
135
816
|
| (?String sep, ?Integer limit, ?chomp: boolish) -> ::Enumerator[String, self]
|
|
@@ -139,21 +820,83 @@ class StringIO
|
|
|
139
820
|
# - each_byte {|byte| ... } -> self
|
|
140
821
|
# -->
|
|
141
822
|
# With a block given, calls the block with each remaining byte in the stream;
|
|
142
|
-
#
|
|
823
|
+
# positions the stream at end-of-file; returns `self`:
|
|
824
|
+
#
|
|
825
|
+
# bytes = []
|
|
826
|
+
# strio = StringIO.new('hello') # Five 1-byte characters.
|
|
827
|
+
# strio.each_byte {|byte| bytes.push(byte) }
|
|
828
|
+
# strio.eof? # => true
|
|
829
|
+
# bytes # => [104, 101, 108, 108, 111]
|
|
830
|
+
# bytes = []
|
|
831
|
+
# strio = StringIO.new('тест') # Four 2-byte characters.
|
|
832
|
+
# strio.each_byte {|byte| bytes.push(byte) }
|
|
833
|
+
# bytes # => [209, 130, 208, 181, 209, 129, 209, 130]
|
|
834
|
+
# bytes = []
|
|
835
|
+
# strio = StringIO.new('こんにちは') # Five 3-byte characters.
|
|
836
|
+
# strio.each_byte {|byte| bytes.push(byte) }
|
|
837
|
+
# bytes # => [227, 129, 147, 227, 130, 147, 227, 129, 171, 227, 129, 161, 227, 129, 175]
|
|
838
|
+
#
|
|
839
|
+
# The position in the stream matters:
|
|
143
840
|
#
|
|
144
|
-
#
|
|
841
|
+
# bytes = []
|
|
842
|
+
# strio = StringIO.new('こんにちは')
|
|
843
|
+
# strio.getc # => "こ"
|
|
844
|
+
# strio.pos # => 3 # 3-byte character was read.
|
|
845
|
+
# strio.each_byte {|byte| bytes.push(byte) }
|
|
846
|
+
# bytes # => [227, 130, 147, 227, 129, 171, 227, 129, 161, 227, 129, 175]
|
|
847
|
+
#
|
|
848
|
+
# If at end-of-file, does not call the block:
|
|
849
|
+
#
|
|
850
|
+
# strio.eof? # => true
|
|
851
|
+
# strio.each_byte {|byte| fail 'Boo!' }
|
|
852
|
+
# strio.eof? # => true
|
|
853
|
+
#
|
|
854
|
+
# With no block given, returns a new [Enumerator](rdoc-ref:Enumerator).
|
|
855
|
+
#
|
|
856
|
+
# Related: StringIO#each_char, StringIO#each_codepoint, StringIO#each_line.
|
|
145
857
|
#
|
|
146
858
|
def each_byte: () { (Integer arg0) -> untyped } -> self
|
|
147
859
|
| () -> ::Enumerator[Integer, self]
|
|
148
860
|
|
|
149
861
|
# <!--
|
|
150
862
|
# rdoc-file=ext/stringio/stringio.c
|
|
151
|
-
# - each_char {|
|
|
863
|
+
# - each_char {|char| ... } -> self
|
|
152
864
|
# -->
|
|
153
865
|
# With a block given, calls the block with each remaining character in the
|
|
154
|
-
# stream;
|
|
866
|
+
# stream; positions the stream at end-of-file; returns `self`:
|
|
867
|
+
#
|
|
868
|
+
# chars = []
|
|
869
|
+
# strio = StringIO.new('hello')
|
|
870
|
+
# strio.each_char {|char| chars.push(char) }
|
|
871
|
+
# strio.eof? # => true
|
|
872
|
+
# chars # => ["h", "e", "l", "l", "o"]
|
|
873
|
+
# chars = []
|
|
874
|
+
# strio = StringIO.new('тест')
|
|
875
|
+
# strio.each_char {|char| chars.push(char) }
|
|
876
|
+
# chars # => ["т", "е", "с", "т"]
|
|
877
|
+
# chars = []
|
|
878
|
+
# strio = StringIO.new('こんにちは')
|
|
879
|
+
# strio.each_char {|char| chars.push(char) }
|
|
880
|
+
# chars # => ["こ", "ん", "に", "ち", "は"]
|
|
155
881
|
#
|
|
156
|
-
#
|
|
882
|
+
# Stream position matters:
|
|
883
|
+
#
|
|
884
|
+
# chars = []
|
|
885
|
+
# strio = StringIO.new('こんにちは')
|
|
886
|
+
# strio.getc # => "こ"
|
|
887
|
+
# strio.pos # => 3 # 3-byte character was read.
|
|
888
|
+
# strio.each_char {|char| chars.push(char) }
|
|
889
|
+
# chars # => ["ん", "に", "ち", "は"]
|
|
890
|
+
#
|
|
891
|
+
# When at end-of-stream does not call the block:
|
|
892
|
+
#
|
|
893
|
+
# strio.eof? # => true
|
|
894
|
+
# strio.each_char {|char| fail 'Boo!' }
|
|
895
|
+
# strio.eof? # => true
|
|
896
|
+
#
|
|
897
|
+
# With no block given, returns a new [Enumerator](rdoc-ref:Enumerator).
|
|
898
|
+
#
|
|
899
|
+
# Related: StringIO#each_byte, StringIO#each_codepoint, StringIO#each_line.
|
|
157
900
|
#
|
|
158
901
|
def each_char: () { (String arg0) -> untyped } -> self
|
|
159
902
|
| () -> ::Enumerator[String, self]
|
|
@@ -162,10 +905,43 @@ class StringIO
|
|
|
162
905
|
# rdoc-file=ext/stringio/stringio.c
|
|
163
906
|
# - each_codepoint {|codepoint| ... } -> self
|
|
164
907
|
# -->
|
|
165
|
-
# With a block given, calls the block with each
|
|
166
|
-
# stream;
|
|
908
|
+
# With a block given, calls the block with each successive codepoint from self;
|
|
909
|
+
# sets the position to end-of-stream; returns `self`.
|
|
910
|
+
#
|
|
911
|
+
# Each codepoint is the integer value for a character; returns self:
|
|
912
|
+
#
|
|
913
|
+
# codepoints = []
|
|
914
|
+
# strio = StringIO.new('hello')
|
|
915
|
+
# strio.each_codepoint {|codepoint| codepoints.push(codepoint) }
|
|
916
|
+
# strio.eof? # => true
|
|
917
|
+
# codepoints # => [104, 101, 108, 108, 111]
|
|
918
|
+
# codepoints = []
|
|
919
|
+
# strio = StringIO.new('тест')
|
|
920
|
+
# strio.each_codepoint {|codepoint| codepoints.push(codepoint) }
|
|
921
|
+
# codepoints # => [1090, 1077, 1089, 1090]
|
|
922
|
+
# codepoints = []
|
|
923
|
+
# strio = StringIO.new('こんにちは')
|
|
924
|
+
# strio.each_codepoint {|codepoint| codepoints.push(codepoint) }
|
|
925
|
+
# codepoints # => [12371, 12435, 12395, 12385, 12399]
|
|
926
|
+
#
|
|
927
|
+
# Position in the stream matters:
|
|
928
|
+
#
|
|
929
|
+
# codepoints = []
|
|
930
|
+
# strio = StringIO.new('こんにちは')
|
|
931
|
+
# strio.getc # => "こ"
|
|
932
|
+
# strio.pos # => 3
|
|
933
|
+
# strio.each_codepoint {|codepoint| codepoints.push(codepoint) }
|
|
934
|
+
# codepoints # => [12435, 12395, 12385, 12399]
|
|
935
|
+
#
|
|
936
|
+
# When at end-of-stream, the block is not called:
|
|
167
937
|
#
|
|
168
|
-
#
|
|
938
|
+
# strio.eof? # => true
|
|
939
|
+
# strio.each_codepoint {|codepoint| fail 'Boo!' }
|
|
940
|
+
# strio.eof? # => true
|
|
941
|
+
#
|
|
942
|
+
# With no block given, returns a new [Enumerator](rdoc-ref:Enumerator).
|
|
943
|
+
#
|
|
944
|
+
# Related: StringIO#each_byte, StringIO#each_char, StringIO#each_line.
|
|
169
945
|
#
|
|
170
946
|
def each_codepoint: () { (Integer arg0) -> untyped } -> self
|
|
171
947
|
| () -> ::Enumerator[Integer, self]
|
|
@@ -174,10 +950,18 @@ class StringIO
|
|
|
174
950
|
# rdoc-file=ext/stringio/stringio.c
|
|
175
951
|
# - eof? -> true or false
|
|
176
952
|
# -->
|
|
177
|
-
# Returns `
|
|
178
|
-
#
|
|
953
|
+
# Returns whether `self` is positioned at end-of-stream:
|
|
954
|
+
#
|
|
955
|
+
# strio = StringIO.new('foo')
|
|
956
|
+
# strio.pos # => 0
|
|
957
|
+
# strio.eof? # => false
|
|
958
|
+
# strio.read # => "foo"
|
|
959
|
+
# strio.pos # => 3
|
|
960
|
+
# strio.eof? # => true
|
|
961
|
+
# strio.close_read
|
|
962
|
+
# strio.eof? # Raises IOError: not opened for reading
|
|
179
963
|
#
|
|
180
|
-
#
|
|
964
|
+
# Related: StringIO#pos.
|
|
181
965
|
#
|
|
182
966
|
def eof: () -> bool
|
|
183
967
|
|
|
@@ -187,13 +971,13 @@ class StringIO
|
|
|
187
971
|
# -->
|
|
188
972
|
# Raises NotImplementedError.
|
|
189
973
|
#
|
|
190
|
-
def fcntl: (
|
|
974
|
+
def fcntl: (*untyped) -> bot
|
|
191
975
|
|
|
192
976
|
# <!--
|
|
193
977
|
# rdoc-file=ext/stringio/stringio.c
|
|
194
978
|
# - fileno()
|
|
195
979
|
# -->
|
|
196
|
-
# Returns `nil
|
|
980
|
+
# Returns `nil`; for compatibility with IO.
|
|
197
981
|
#
|
|
198
982
|
def fileno: () -> nil
|
|
199
983
|
|
|
@@ -201,7 +985,7 @@ class StringIO
|
|
|
201
985
|
# rdoc-file=ext/stringio/stringio.c
|
|
202
986
|
# - flush()
|
|
203
987
|
# -->
|
|
204
|
-
# Returns
|
|
988
|
+
# Returns `self`; for compatibility with IO.
|
|
205
989
|
#
|
|
206
990
|
def flush: () -> self
|
|
207
991
|
|
|
@@ -209,25 +993,86 @@ class StringIO
|
|
|
209
993
|
# rdoc-file=ext/stringio/stringio.c
|
|
210
994
|
# - fsync()
|
|
211
995
|
# -->
|
|
212
|
-
# Returns 0
|
|
996
|
+
# Returns 0; for compatibility with IO.
|
|
213
997
|
#
|
|
214
|
-
def fsync: () -> Integer
|
|
998
|
+
def fsync: () -> Integer
|
|
215
999
|
|
|
216
1000
|
# <!--
|
|
217
1001
|
# rdoc-file=ext/stringio/stringio.c
|
|
218
|
-
# - getbyte ->
|
|
1002
|
+
# - getbyte -> integer or nil
|
|
219
1003
|
# -->
|
|
220
|
-
# Reads and returns the next
|
|
221
|
-
#
|
|
1004
|
+
# Reads and returns the next integer byte (not character) from the stream:
|
|
1005
|
+
#
|
|
1006
|
+
# s = 'foo'
|
|
1007
|
+
# s.bytes # => [102, 111, 111]
|
|
1008
|
+
# strio = StringIO.new(s)
|
|
1009
|
+
# strio.getbyte # => 102
|
|
1010
|
+
# strio.getbyte # => 111
|
|
1011
|
+
# strio.getbyte # => 111
|
|
1012
|
+
#
|
|
1013
|
+
# Returns `nil` if at end-of-stream:
|
|
1014
|
+
#
|
|
1015
|
+
# strio.eof? # => true
|
|
1016
|
+
# strio.getbyte # => nil
|
|
1017
|
+
#
|
|
1018
|
+
# Returns a byte, not a character:
|
|
1019
|
+
#
|
|
1020
|
+
# s = 'Привет'
|
|
1021
|
+
# s.bytes
|
|
1022
|
+
# # => [208, 159, 209, 128, 208, 184, 208, 178, 208, 181, 209, 130]
|
|
1023
|
+
# strio = StringIO.new(s)
|
|
1024
|
+
# strio.getbyte # => 208
|
|
1025
|
+
# strio.getbyte # => 159
|
|
1026
|
+
#
|
|
1027
|
+
# s = 'こんにちは'
|
|
1028
|
+
# s.bytes
|
|
1029
|
+
# # => [227, 129, 147, 227, 130, 147, 227, 129, 171, 227, 129, 161, 227, 129, 175]
|
|
1030
|
+
# strio = StringIO.new(s)
|
|
1031
|
+
# strio.getbyte # => 227
|
|
1032
|
+
# strio.getbyte # => 129
|
|
1033
|
+
#
|
|
1034
|
+
# Related: #each_byte, #ungetbyte, #getc.
|
|
222
1035
|
#
|
|
223
1036
|
def getbyte: () -> Integer?
|
|
224
1037
|
|
|
225
1038
|
# <!--
|
|
226
1039
|
# rdoc-file=ext/stringio/stringio.c
|
|
227
|
-
# - getc -> character or nil
|
|
1040
|
+
# - getc -> character, byte, or nil
|
|
228
1041
|
# -->
|
|
229
|
-
# Reads and returns the next character from the stream
|
|
230
|
-
#
|
|
1042
|
+
# Reads and returns the next character (or byte; see below) from the stream:
|
|
1043
|
+
#
|
|
1044
|
+
# strio = StringIO.new('foo')
|
|
1045
|
+
# strio.getc # => "f"
|
|
1046
|
+
# strio.getc # => "o"
|
|
1047
|
+
# strio.getc # => "o"
|
|
1048
|
+
#
|
|
1049
|
+
# Returns `nil` if at end-of-stream:
|
|
1050
|
+
#
|
|
1051
|
+
# strio.eof? # => true
|
|
1052
|
+
# strio.getc # => nil
|
|
1053
|
+
#
|
|
1054
|
+
# Returns characters, not bytes:
|
|
1055
|
+
#
|
|
1056
|
+
# strio = StringIO.new('Привет')
|
|
1057
|
+
# strio.getc # => "П"
|
|
1058
|
+
# strio.getc # => "р"
|
|
1059
|
+
#
|
|
1060
|
+
# strio = StringIO.new('こんにちは')
|
|
1061
|
+
# strio.getc # => "こ"
|
|
1062
|
+
# strio.getc # => "ん"
|
|
1063
|
+
#
|
|
1064
|
+
# In each of the examples above, the stream is positioned at the beginning of a
|
|
1065
|
+
# character; in other cases that need not be true:
|
|
1066
|
+
#
|
|
1067
|
+
# strio = StringIO.new('こんにちは') # Five 3-byte characters.
|
|
1068
|
+
# strio.pos = 3 # => 3 # At beginning of second character; returns character.
|
|
1069
|
+
# strio.getc # => "ん"
|
|
1070
|
+
# strio.pos = 4 # => 4 # At second byte of second character; returns byte.
|
|
1071
|
+
# strio.getc # => "\x82"
|
|
1072
|
+
# strio.pos = 5 # => 5 # At third byte of second character; returns byte.
|
|
1073
|
+
# strio.getc # => "\x93"
|
|
1074
|
+
#
|
|
1075
|
+
# Related: #getbyte, #putc, #ungetc.
|
|
231
1076
|
#
|
|
232
1077
|
def getc: () -> String?
|
|
233
1078
|
|
|
@@ -237,34 +1082,139 @@ class StringIO
|
|
|
237
1082
|
# - gets(limit, chomp: false) -> string or nil
|
|
238
1083
|
# - gets(sep, limit, chomp: false) -> string or nil
|
|
239
1084
|
# -->
|
|
240
|
-
# Reads and returns a line from the stream;
|
|
241
|
-
#
|
|
1085
|
+
# Reads and returns a line from the stream; returns `nil` if at end-of-stream.
|
|
1086
|
+
#
|
|
1087
|
+
# Side effects:
|
|
1088
|
+
#
|
|
1089
|
+
# * Increments stream position by the number of bytes read.
|
|
1090
|
+
# * Assigns the return value to global variable <code>$_</code>.
|
|
1091
|
+
#
|
|
1092
|
+
# With no arguments given, reads a line using the default record separator
|
|
1093
|
+
# (global variable <code>$/</code>,* whose initial value is <code>"\n"</code>):
|
|
1094
|
+
#
|
|
1095
|
+
# strio = StringIO.new(TEXT)
|
|
1096
|
+
# strio.pos # => 0
|
|
1097
|
+
# strio.gets # => "First line\n"
|
|
1098
|
+
# strio.pos # => 11
|
|
1099
|
+
# $_ # => "First line\n"
|
|
1100
|
+
# strio.gets # => "Second line\n"
|
|
1101
|
+
# strio.read # => "\nFourth line\nFifth line\n"
|
|
1102
|
+
# strio.eof? # => true
|
|
1103
|
+
# strio.gets # => nil
|
|
1104
|
+
#
|
|
1105
|
+
# strio = StringIO.new('Привет') # Six 2-byte characters
|
|
1106
|
+
# strio.pos # => 0
|
|
1107
|
+
# strio.gets # => "Привет"
|
|
1108
|
+
# strio.pos # => 12
|
|
1109
|
+
#
|
|
1110
|
+
# <strong>Argument `sep`</strong>
|
|
1111
|
+
#
|
|
1112
|
+
# With only string argument `sep` given, reads a line using that string as the
|
|
1113
|
+
# record separator:
|
|
1114
|
+
#
|
|
1115
|
+
# strio = StringIO.new(TEXT)
|
|
1116
|
+
# strio.gets(' ') # => "First "
|
|
1117
|
+
# strio.gets(' ') # => "line\nSecond "
|
|
1118
|
+
# strio.gets(' ') # => "line\n\nFourth "
|
|
1119
|
+
#
|
|
1120
|
+
# <strong>Argument `limit`</strong>
|
|
1121
|
+
#
|
|
1122
|
+
# With only integer argument `limit` given, reads a line using the default
|
|
1123
|
+
# record separator; limits the size (in characters) of each line to the given
|
|
1124
|
+
# limit:
|
|
1125
|
+
#
|
|
1126
|
+
# strio = StringIO.new(TEXT)
|
|
1127
|
+
# strio.gets(10) # => "First line"
|
|
1128
|
+
# strio.gets(10) # => "\n"
|
|
1129
|
+
# strio.gets(10) # => "Second lin"
|
|
1130
|
+
# strio.gets(10) # => "e\n"
|
|
1131
|
+
#
|
|
1132
|
+
# <strong>Arguments `sep` and `limit`</strong>
|
|
1133
|
+
#
|
|
1134
|
+
# With arguments `sep` and `limit` both given, honors both:
|
|
1135
|
+
#
|
|
1136
|
+
# strio = StringIO.new(TEXT)
|
|
1137
|
+
# strio.gets(' ', 10) # => "First "
|
|
1138
|
+
# strio.gets(' ', 10) # => "line\nSecon"
|
|
1139
|
+
# strio.gets(' ', 10) # => "d "
|
|
1140
|
+
#
|
|
1141
|
+
# **Position**
|
|
1142
|
+
#
|
|
1143
|
+
# As stated above, method `gets` reads and returns the next line in the stream.
|
|
1144
|
+
#
|
|
1145
|
+
# In the examples above each `strio` object starts with its position at
|
|
1146
|
+
# beginning-of-stream; but in other cases the position may be anywhere:
|
|
1147
|
+
#
|
|
1148
|
+
# strio = StringIO.new(TEXT)
|
|
1149
|
+
# strio.pos = 12
|
|
1150
|
+
# strio.gets # => "econd line\n"
|
|
1151
|
+
#
|
|
1152
|
+
# The position need not be at a character boundary:
|
|
1153
|
+
#
|
|
1154
|
+
# strio = StringIO.new('Привет') # Six 2-byte characters.
|
|
1155
|
+
# strio.pos = 2 # At beginning of second character.
|
|
1156
|
+
# strio.gets # => "ривет"
|
|
1157
|
+
# strio.pos = 3 # In middle of second character.
|
|
1158
|
+
# strio.gets # => "\x80ивет"
|
|
1159
|
+
#
|
|
1160
|
+
# **Special Record Separators**
|
|
1161
|
+
#
|
|
1162
|
+
# Like some methods in class IO, method `gets` honors two special record
|
|
1163
|
+
# separators; see [Special Line
|
|
1164
|
+
# Separators](https://docs.ruby-lang.org/en/master/IO.html#class-IO-label-Specia
|
|
1165
|
+
# l+Line+Separator+Values):
|
|
1166
|
+
#
|
|
1167
|
+
# strio = StringIO.new(TEXT)
|
|
1168
|
+
# strio.gets('') # Read "paragraph" (up to empty line).
|
|
1169
|
+
# # => "First line\nSecond line\n\n"
|
|
1170
|
+
#
|
|
1171
|
+
# strio = StringIO.new(TEXT)
|
|
1172
|
+
# strio.gets(nil) # "Slurp": read all.
|
|
1173
|
+
# # => "First line\nSecond line\n\nFourth line\nFifth line\n"
|
|
1174
|
+
#
|
|
1175
|
+
# <strong>Keyword Argument `chomp`</strong>
|
|
1176
|
+
#
|
|
1177
|
+
# With keyword argument `chomp` given as `true` (the default is `false`),
|
|
1178
|
+
# removes the trailing newline (if any) from the returned line:
|
|
1179
|
+
#
|
|
1180
|
+
# strio = StringIO.new(TEXT)
|
|
1181
|
+
# strio.gets # => "First line\n"
|
|
1182
|
+
# strio.gets(chomp: true) # => "Second line"
|
|
1183
|
+
#
|
|
1184
|
+
# Related: #each_line, #readlines, [Kernel#puts](rdoc-ref:Kernel#puts).
|
|
242
1185
|
#
|
|
243
1186
|
def gets: (?String sep, ?Integer limit, ?chomp: boolish) -> String?
|
|
244
1187
|
|
|
245
1188
|
# <!--
|
|
246
1189
|
# rdoc-file=ext/stringio/stringio.c
|
|
247
|
-
# -
|
|
1190
|
+
# - internal_encoding -> nil
|
|
248
1191
|
# -->
|
|
249
|
-
# Returns
|
|
250
|
-
# Otherwise returns `nil`.
|
|
1192
|
+
# Returns `nil`; for compatibility with IO.
|
|
251
1193
|
#
|
|
252
|
-
def internal_encoding: () ->
|
|
1194
|
+
def internal_encoding: () -> nil
|
|
253
1195
|
|
|
254
1196
|
# <!--
|
|
255
1197
|
# rdoc-file=ext/stringio/stringio.c
|
|
256
|
-
# -
|
|
1198
|
+
# - external_encoding -> encoding or nil
|
|
257
1199
|
# -->
|
|
258
|
-
# Returns
|
|
259
|
-
#
|
|
1200
|
+
# Returns an Encoding object that represents the encoding of the string; see
|
|
1201
|
+
# [Encodings](rdoc-ref:StringIO@Encodings):
|
|
1202
|
+
#
|
|
1203
|
+
# strio = StringIO.new('foo')
|
|
1204
|
+
# strio.external_encoding # => #<Encoding:UTF-8>
|
|
260
1205
|
#
|
|
261
|
-
|
|
1206
|
+
# Returns `nil` if `self` has no string and is in write mode:
|
|
1207
|
+
#
|
|
1208
|
+
# strio = StringIO.new(nil, 'w+')
|
|
1209
|
+
# strio.external_encoding # => nil
|
|
1210
|
+
#
|
|
1211
|
+
def external_encoding: () -> Encoding?
|
|
262
1212
|
|
|
263
1213
|
# <!--
|
|
264
1214
|
# rdoc-file=ext/stringio/stringio.c
|
|
265
1215
|
# - isatty()
|
|
266
1216
|
# -->
|
|
267
|
-
# Returns `false
|
|
1217
|
+
# Returns `false`; for compatibility with IO.
|
|
268
1218
|
#
|
|
269
1219
|
def isatty: () -> bool
|
|
270
1220
|
|
|
@@ -273,7 +1223,7 @@ class StringIO
|
|
|
273
1223
|
# - lineno -> current_line_number
|
|
274
1224
|
# -->
|
|
275
1225
|
# Returns the current line number in `self`; see [Line
|
|
276
|
-
# Number](rdoc-ref:
|
|
1226
|
+
# Number](rdoc-ref:StringIO@Line+Number).
|
|
277
1227
|
#
|
|
278
1228
|
def lineno: () -> Integer
|
|
279
1229
|
|
|
@@ -282,7 +1232,7 @@ class StringIO
|
|
|
282
1232
|
# - lineno = new_line_number -> new_line_number
|
|
283
1233
|
# -->
|
|
284
1234
|
# Sets the current line number in `self` to the given `new_line_number`; see
|
|
285
|
-
# [Line Number](rdoc-ref:
|
|
1235
|
+
# [Line Number](rdoc-ref:StringIO@Line+Number).
|
|
286
1236
|
#
|
|
287
1237
|
def lineno=: (Integer arg0) -> Integer
|
|
288
1238
|
|
|
@@ -290,7 +1240,7 @@ class StringIO
|
|
|
290
1240
|
# rdoc-file=ext/stringio/stringio.c
|
|
291
1241
|
# - pid()
|
|
292
1242
|
# -->
|
|
293
|
-
# Returns `nil
|
|
1243
|
+
# Returns `nil`; for compatibility with IO.
|
|
294
1244
|
#
|
|
295
1245
|
def pid: () -> nil
|
|
296
1246
|
|
|
@@ -298,7 +1248,8 @@ class StringIO
|
|
|
298
1248
|
# rdoc-file=ext/stringio/stringio.c
|
|
299
1249
|
# - pos -> stream_position
|
|
300
1250
|
# -->
|
|
301
|
-
# Returns the current position (in bytes); see
|
|
1251
|
+
# Returns the current position (in bytes); see
|
|
1252
|
+
# [Position](rdoc-ref:StringIO@Position).
|
|
302
1253
|
#
|
|
303
1254
|
def pos: () -> Integer
|
|
304
1255
|
|
|
@@ -306,7 +1257,8 @@ class StringIO
|
|
|
306
1257
|
# rdoc-file=ext/stringio/stringio.c
|
|
307
1258
|
# - pos = new_position -> new_position
|
|
308
1259
|
# -->
|
|
309
|
-
# Sets the current position (in bytes); see
|
|
1260
|
+
# Sets the current position (in bytes); see
|
|
1261
|
+
# [Position](rdoc-ref:StringIO@Position).
|
|
310
1262
|
#
|
|
311
1263
|
def pos=: (Integer arg0) -> Integer
|
|
312
1264
|
|
|
@@ -330,9 +1282,18 @@ class StringIO
|
|
|
330
1282
|
# -->
|
|
331
1283
|
# See IO#read.
|
|
332
1284
|
#
|
|
333
|
-
def read: (?int? length, ?string outbuf) -> String?
|
|
1285
|
+
def read: (?int? length, ?string? outbuf) -> String?
|
|
1286
|
+
|
|
1287
|
+
# <!--
|
|
1288
|
+
# rdoc-file=ext/stringio/stringio.c
|
|
1289
|
+
# - pread(maxlen, offset) -> string
|
|
1290
|
+
# - pread(maxlen, offset, out_string) -> string
|
|
1291
|
+
# -->
|
|
1292
|
+
# See IO#pread.
|
|
1293
|
+
#
|
|
1294
|
+
def pread: (Integer maxlen, Integer offset, ?String? outbuf) -> String
|
|
334
1295
|
|
|
335
|
-
def read_nonblock: (int len, ?string buf) -> String
|
|
1296
|
+
def read_nonblock: (int len, ?string? buf, ?exception: bool) -> String?
|
|
336
1297
|
|
|
337
1298
|
def readbyte: () -> Integer
|
|
338
1299
|
|
|
@@ -350,7 +1311,7 @@ class StringIO
|
|
|
350
1311
|
#
|
|
351
1312
|
def readlines: (?String sep, ?Integer limit, ?chomp: boolish) -> ::Array[String]
|
|
352
1313
|
|
|
353
|
-
def readpartial: (int maxlen, ?string outbuf) -> String
|
|
1314
|
+
def readpartial: (int maxlen, ?string? outbuf) -> String
|
|
354
1315
|
|
|
355
1316
|
# <!--
|
|
356
1317
|
# rdoc-file=ext/stringio/stringio.c
|
|
@@ -391,8 +1352,8 @@ class StringIO
|
|
|
391
1352
|
# rdoc-file=ext/stringio/stringio.c
|
|
392
1353
|
# - seek(offset, whence = SEEK_SET) -> 0
|
|
393
1354
|
# -->
|
|
394
|
-
# Sets the
|
|
395
|
-
#
|
|
1355
|
+
# Sets the position to the given integer `offset` (in bytes), with respect to a
|
|
1356
|
+
# given constant `whence`; see [IO#seek](rdoc-ref:IO#seek).
|
|
396
1357
|
#
|
|
397
1358
|
def seek: (Integer amount, ?Integer whence) -> Integer
|
|
398
1359
|
|
|
@@ -407,6 +1368,16 @@ class StringIO
|
|
|
407
1368
|
def set_encoding: (?String | Encoding ext_or_ext_int_enc) -> self
|
|
408
1369
|
| (?String | Encoding ext_or_ext_int_enc, ?String | Encoding int_enc) -> self
|
|
409
1370
|
|
|
1371
|
+
# <!--
|
|
1372
|
+
# rdoc-file=ext/stringio/stringio.c
|
|
1373
|
+
# - strio.set_encoding_by_bom => strio or nil
|
|
1374
|
+
# -->
|
|
1375
|
+
# Sets the encoding according to the BOM (Byte Order Mark) in the string.
|
|
1376
|
+
#
|
|
1377
|
+
# Returns `self` if the BOM is found, otherwise +nil.
|
|
1378
|
+
#
|
|
1379
|
+
def set_encoding_by_bom: () -> Encoding?
|
|
1380
|
+
|
|
410
1381
|
# <!--
|
|
411
1382
|
# rdoc-file=ext/stringio/stringio.c
|
|
412
1383
|
# - string -> string
|
|
@@ -432,7 +1403,7 @@ class StringIO
|
|
|
432
1403
|
# rdoc-file=ext/stringio/stringio.c
|
|
433
1404
|
# - string = other_string -> other_string
|
|
434
1405
|
# -->
|
|
435
|
-
#
|
|
1406
|
+
# Replaces the stored string with `other_string`, and sets the position to zero;
|
|
436
1407
|
# returns `other_string`:
|
|
437
1408
|
#
|
|
438
1409
|
# StringIO.open('foo') do |strio|
|
|
@@ -446,16 +1417,19 @@ class StringIO
|
|
|
446
1417
|
# "foo"
|
|
447
1418
|
# "bar"
|
|
448
1419
|
#
|
|
449
|
-
# Related: StringIO#string (returns the
|
|
1420
|
+
# Related: StringIO#string (returns the stored string).
|
|
450
1421
|
#
|
|
451
1422
|
def string=: (String str) -> String
|
|
452
1423
|
|
|
453
1424
|
# <!--
|
|
454
1425
|
# rdoc-file=ext/stringio/stringio.c
|
|
455
|
-
# -
|
|
456
|
-
# - strio.size -> integer
|
|
1426
|
+
# - size -> integer
|
|
457
1427
|
# -->
|
|
458
|
-
# Returns the
|
|
1428
|
+
# Returns the number of bytes in the string in `self`:
|
|
1429
|
+
#
|
|
1430
|
+
# StringIO.new('hello').size # => 5 # Five 1-byte characters.
|
|
1431
|
+
# StringIO.new('тест').size # => 8 # Four 2-byte characters.
|
|
1432
|
+
# StringIO.new('こんにちは').size # => 15 # Five 3-byte characters.
|
|
459
1433
|
#
|
|
460
1434
|
def size: () -> Integer
|
|
461
1435
|
|
|
@@ -475,7 +1449,7 @@ class StringIO
|
|
|
475
1449
|
#
|
|
476
1450
|
def sync=: (boolish) -> bool
|
|
477
1451
|
|
|
478
|
-
def sysread: (Integer maxlen, String outbuf) -> String
|
|
1452
|
+
def sysread: (Integer maxlen, ?String? outbuf) -> String
|
|
479
1453
|
|
|
480
1454
|
def syswrite: (String arg0) -> Integer
|
|
481
1455
|
|
|
@@ -483,7 +1457,8 @@ class StringIO
|
|
|
483
1457
|
# rdoc-file=ext/stringio/stringio.c
|
|
484
1458
|
# - pos -> stream_position
|
|
485
1459
|
# -->
|
|
486
|
-
# Returns the current position (in bytes); see
|
|
1460
|
+
# Returns the current position (in bytes); see
|
|
1461
|
+
# [Position](rdoc-ref:StringIO@Position).
|
|
487
1462
|
#
|
|
488
1463
|
def tell: () -> Integer
|
|
489
1464
|
|
|
@@ -497,7 +1472,7 @@ class StringIO
|
|
|
497
1472
|
def truncate: (Integer) -> 0
|
|
498
1473
|
|
|
499
1474
|
# <!-- rdoc-file=ext/stringio/stringio.c -->
|
|
500
|
-
# Returns `false
|
|
1475
|
+
# Returns `false`; for compatibility with IO.
|
|
501
1476
|
#
|
|
502
1477
|
def tty?: () -> bool
|
|
503
1478
|
|
|
@@ -517,7 +1492,7 @@ class StringIO
|
|
|
517
1492
|
# Pushes back ("unshifts") a character or integer onto the stream; see
|
|
518
1493
|
# [Character IO](rdoc-ref:IO@Character+IO).
|
|
519
1494
|
#
|
|
520
|
-
def ungetc: (String arg0) -> nil
|
|
1495
|
+
def ungetc: (String | Integer arg0) -> nil
|
|
521
1496
|
|
|
522
1497
|
# <!--
|
|
523
1498
|
# rdoc-file=ext/stringio/stringio.c
|
|
@@ -530,6 +1505,8 @@ class StringIO
|
|
|
530
1505
|
#
|
|
531
1506
|
def write: (*_ToS) -> Integer
|
|
532
1507
|
|
|
1508
|
+
def write_nonblock: (_ToS s, ?exception: bool) -> Integer
|
|
1509
|
+
|
|
533
1510
|
# This is a deprecated alias for #each_byte.
|
|
534
1511
|
#
|
|
535
1512
|
def bytes: () { (Integer arg0) -> untyped } -> self
|
|
@@ -546,17 +1523,154 @@ class StringIO
|
|
|
546
1523
|
| () -> ::Enumerator[Integer, self]
|
|
547
1524
|
|
|
548
1525
|
# <!-- rdoc-file=ext/stringio/stringio.c -->
|
|
549
|
-
#
|
|
550
|
-
#
|
|
1526
|
+
# With a block given calls the block with each remaining line (see "Position"
|
|
1527
|
+
# below) in the stream;
|
|
1528
|
+
# returns `self`.
|
|
1529
|
+
# Leaves stream position at end-of-stream.
|
|
1530
|
+
# **No Arguments**
|
|
1531
|
+
# With no arguments given,
|
|
1532
|
+
# reads lines using the default record separator
|
|
1533
|
+
# (global variable <code>$/</code>, whose initial value is <code>"\n"</code>).
|
|
1534
|
+
# strio = StringIO.new(TEXT)
|
|
1535
|
+
# strio.each_line {|line| p line }
|
|
1536
|
+
# strio.eof? # => true
|
|
1537
|
+
#
|
|
1538
|
+
# Output:
|
|
1539
|
+
# "First line\n"
|
|
1540
|
+
# "Second line\n"
|
|
1541
|
+
# "\n"
|
|
1542
|
+
# "Fourth line\n"
|
|
1543
|
+
# "Fifth line\n"
|
|
1544
|
+
#
|
|
1545
|
+
# <strong>Argument `sep`</strong>
|
|
1546
|
+
# With only string argument `sep` given,
|
|
1547
|
+
# reads lines using that string as the record separator:
|
|
1548
|
+
# strio = StringIO.new(TEXT)
|
|
1549
|
+
# strio.each_line(' ') {|line| p line }
|
|
1550
|
+
#
|
|
1551
|
+
# Output:
|
|
1552
|
+
# "First "
|
|
1553
|
+
# "line\nSecond "
|
|
1554
|
+
# "line\n\nFourth "
|
|
1555
|
+
# "line\nFifth "
|
|
1556
|
+
# "line\n"
|
|
1557
|
+
#
|
|
1558
|
+
# <strong>Argument `limit`</strong>
|
|
1559
|
+
# With only integer argument `limit` given,
|
|
1560
|
+
# reads lines using the default record separator;
|
|
1561
|
+
# also limits the size (in characters) of each line to the given limit:
|
|
1562
|
+
# strio = StringIO.new(TEXT)
|
|
1563
|
+
# strio.each_line(10) {|line| p line }
|
|
1564
|
+
#
|
|
1565
|
+
# Output:
|
|
1566
|
+
# "First line"
|
|
1567
|
+
# "\n"
|
|
1568
|
+
# "Second lin"
|
|
1569
|
+
# "e\n"
|
|
1570
|
+
# "\n"
|
|
1571
|
+
# "Fourth lin"
|
|
1572
|
+
# "e\n"
|
|
1573
|
+
# "Fifth line"
|
|
1574
|
+
# "\n"
|
|
1575
|
+
#
|
|
1576
|
+
# <strong>Arguments `sep` and `limit`</strong>
|
|
1577
|
+
# With arguments `sep` and `limit` both given,
|
|
1578
|
+
# honors both:
|
|
1579
|
+
# strio = StringIO.new(TEXT)
|
|
1580
|
+
# strio.each_line(' ', 10) {|line| p line }
|
|
1581
|
+
#
|
|
1582
|
+
# Output:
|
|
1583
|
+
# "First "
|
|
1584
|
+
# "line\nSecon"
|
|
1585
|
+
# "d "
|
|
1586
|
+
# "line\n\nFour"
|
|
1587
|
+
# "th "
|
|
1588
|
+
# "line\nFifth"
|
|
1589
|
+
# " "
|
|
1590
|
+
# "line\n"
|
|
1591
|
+
#
|
|
1592
|
+
# **Position**
|
|
1593
|
+
# As stated above, method `each` *remaining* line in the stream.
|
|
1594
|
+
# In the examples above each `strio` object starts with its position at
|
|
1595
|
+
# beginning-of-stream;
|
|
1596
|
+
# but in other cases the position may be anywhere (see StringIO#pos):
|
|
1597
|
+
# strio = StringIO.new(TEXT)
|
|
1598
|
+
# strio.pos = 30 # Set stream position to character 30.
|
|
1599
|
+
# strio.each_line {|line| p line }
|
|
1600
|
+
#
|
|
1601
|
+
# Output:
|
|
1602
|
+
# " line\n"
|
|
1603
|
+
# "Fifth line\n"
|
|
1604
|
+
#
|
|
1605
|
+
# In all the examples above, the stream position is at the beginning of a
|
|
1606
|
+
# character;
|
|
1607
|
+
# in other cases, that need not be so:
|
|
1608
|
+
# s = 'こんにちは' # Five 3-byte characters.
|
|
1609
|
+
# strio = StringIO.new(s)
|
|
1610
|
+
# strio.pos = 3 # At beginning of second character.
|
|
1611
|
+
# strio.each_line {|line| p line }
|
|
1612
|
+
# strio.pos = 4 # At second byte of second character.
|
|
1613
|
+
# strio.each_line {|line| p line }
|
|
1614
|
+
# strio.pos = 5 # At third byte of second character.
|
|
1615
|
+
# strio.each_line {|line| p line }
|
|
1616
|
+
#
|
|
1617
|
+
# Output:
|
|
1618
|
+
# "んにちは"
|
|
1619
|
+
# "\x82\x93にちは"
|
|
1620
|
+
# "\x93にちは"
|
|
1621
|
+
#
|
|
1622
|
+
# **Special Record Separators**
|
|
1623
|
+
# Like some methods in class `IO`, StringIO.each honors two special record
|
|
1624
|
+
# separators;
|
|
1625
|
+
# see [Special Line
|
|
1626
|
+
# Separators](https://docs.ruby-lang.org/en/master/IO.html#class-IO-label-Specia
|
|
1627
|
+
# l+Line+Separator+Values).
|
|
1628
|
+
# strio = StringIO.new(TEXT)
|
|
1629
|
+
# strio.each_line('') {|line| p line } # Read as paragraphs (separated by blank lines).
|
|
1630
|
+
#
|
|
1631
|
+
# Output:
|
|
1632
|
+
# "First line\nSecond line\n\n"
|
|
1633
|
+
# "Fourth line\nFifth line\n"
|
|
1634
|
+
#
|
|
1635
|
+
# strio = StringIO.new(TEXT)
|
|
1636
|
+
# strio.each_line(nil) {|line| p line } # "Slurp"; read it all.
|
|
1637
|
+
#
|
|
1638
|
+
# Output:
|
|
1639
|
+
# "First line\nSecond line\n\nFourth line\nFifth line\n"
|
|
1640
|
+
#
|
|
1641
|
+
# <strong>Keyword Argument `chomp`</strong>
|
|
1642
|
+
# With keyword argument `chomp` given as `true` (the default is `false`),
|
|
1643
|
+
# removes trailing newline (if any) from each line:
|
|
1644
|
+
# strio = StringIO.new(TEXT)
|
|
1645
|
+
# strio.each_line(chomp: true) {|line| p line }
|
|
1646
|
+
#
|
|
1647
|
+
# Output:
|
|
1648
|
+
# "First line"
|
|
1649
|
+
# "Second line"
|
|
1650
|
+
# ""
|
|
1651
|
+
# "Fourth line"
|
|
1652
|
+
# "Fifth line"
|
|
1653
|
+
#
|
|
1654
|
+
# With no block given, returns a new
|
|
1655
|
+
# [Enumerator](https://docs.ruby-lang.org/en/master/Enumerator.html).
|
|
1656
|
+
# Related: StringIO.each_byte, StringIO.each_char, StringIO.each_codepoint.
|
|
551
1657
|
#
|
|
552
1658
|
def each_line: (?String sep, ?Integer limit, ?chomp: boolish) { (String) -> untyped } -> self
|
|
553
1659
|
| (?String sep, ?Integer limit, ?chomp: boolish) -> ::Enumerator[String, self]
|
|
554
1660
|
|
|
555
1661
|
# <!-- rdoc-file=ext/stringio/stringio.c -->
|
|
556
|
-
# Returns `
|
|
557
|
-
#
|
|
558
|
-
#
|
|
559
|
-
#
|
|
1662
|
+
# Returns whether `self` is positioned at end-of-stream:
|
|
1663
|
+
#
|
|
1664
|
+
# strio = StringIO.new('foo')
|
|
1665
|
+
# strio.pos # => 0
|
|
1666
|
+
# strio.eof? # => false
|
|
1667
|
+
# strio.read # => "foo"
|
|
1668
|
+
# strio.pos # => 3
|
|
1669
|
+
# strio.eof? # => true
|
|
1670
|
+
# strio.close_read
|
|
1671
|
+
# strio.eof? # Raises IOError: not opened for reading
|
|
1672
|
+
#
|
|
1673
|
+
# Related: StringIO#pos.
|
|
560
1674
|
#
|
|
561
1675
|
def eof?: () -> bool
|
|
562
1676
|
|