rbs 4.0.0.dev.4 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/dependabot.yml +14 -14
- data/.github/workflows/bundle-update.yml +60 -0
- data/.github/workflows/c-check.yml +18 -11
- data/.github/workflows/comments.yml +5 -3
- data/.github/workflows/dependabot.yml +2 -2
- data/.github/workflows/ruby.yml +27 -34
- data/.github/workflows/rust.yml +95 -0
- data/.github/workflows/typecheck.yml +2 -2
- data/.github/workflows/windows.yml +2 -2
- data/.rubocop.yml +1 -1
- data/CHANGELOG.md +323 -0
- data/README.md +1 -1
- data/Rakefile +43 -33
- data/Steepfile +1 -0
- data/config.yml +426 -24
- data/core/array.rbs +307 -227
- data/core/basic_object.rbs +9 -8
- data/core/binding.rbs +0 -2
- data/core/builtin.rbs +2 -2
- data/core/class.rbs +6 -5
- 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 +179 -87
- data/core/enumerator/arithmetic_sequence.rbs +70 -0
- data/core/enumerator.rbs +65 -2
- 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 +280 -177
- data/core/file_test.rbs +3 -3
- data/core/float.rbs +257 -92
- data/core/gc.rbs +425 -281
- data/core/hash.rbs +1045 -739
- data/core/integer.rbs +135 -137
- data/core/io/buffer.rbs +53 -42
- data/core/io/wait.rbs +13 -35
- data/core/io.rbs +192 -144
- data/core/kernel.rbs +216 -155
- 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 +244 -106
- data/core/nil_class.rbs +7 -6
- data/core/numeric.rbs +74 -63
- data/core/object.rbs +9 -11
- 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 +159 -57
- data/core/rational.rbs +60 -89
- data/core/rbs/unnamed/argf.rbs +60 -53
- 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 +38 -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 +490 -360
- data/core/signal.rbs +26 -16
- data/core/string.rbs +3234 -1285
- data/core/struct.rbs +27 -26
- data/core/symbol.rbs +41 -34
- data/core/thread.rbs +135 -67
- 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/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 +576 -0
- data/docs/sigs.md +3 -3
- data/docs/syntax.md +46 -16
- data/docs/type_fingerprint.md +21 -0
- data/exe/rbs +1 -1
- data/ext/rbs_extension/ast_translation.c +544 -116
- data/ext/rbs_extension/ast_translation.h +3 -0
- data/ext/rbs_extension/class_constants.c +16 -2
- data/ext/rbs_extension/class_constants.h +8 -0
- data/ext/rbs_extension/extconf.rb +5 -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 +44 -35
- data/include/rbs/ast.h +448 -173
- data/include/rbs/defines.h +27 -0
- data/include/rbs/lexer.h +30 -11
- data/include/rbs/location.h +25 -44
- data/include/rbs/parser.h +6 -6
- 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/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 +293 -3
- data/lib/rbs/ast/ruby/comment_block.rb +24 -0
- data/lib/rbs/ast/ruby/declarations.rb +198 -3
- data/lib/rbs/ast/ruby/helpers/constant_helper.rb +4 -0
- data/lib/rbs/ast/ruby/members.rb +532 -22
- data/lib/rbs/ast/type_param.rb +24 -4
- data/lib/rbs/buffer.rb +20 -15
- data/lib/rbs/cli/diff.rb +16 -15
- data/lib/rbs/cli/validate.rb +38 -106
- data/lib/rbs/cli.rb +52 -19
- data/lib/rbs/collection/config/lockfile_generator.rb +14 -2
- data/lib/rbs/collection/sources/git.rb +1 -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 +20 -0
- data/lib/rbs/definition_builder.rb +147 -25
- data/lib/rbs/diff.rb +7 -1
- data/lib/rbs/environment.rb +227 -74
- data/lib/rbs/environment_loader.rb +0 -6
- data/lib/rbs/errors.rb +27 -18
- data/lib/rbs/inline_parser.rb +342 -6
- 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/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 +8 -0
- data/lib/rbs/resolver/constant_resolver.rb +2 -2
- data/lib/rbs/resolver/type_name_resolver.rb +116 -38
- data/lib/rbs/subtractor.rb +3 -1
- data/lib/rbs/test/type_check.rb +19 -2
- data/lib/rbs/type_name.rb +1 -1
- data/lib/rbs/types.rb +88 -78
- data/lib/rbs/unit_test/type_assertions.rb +35 -8
- data/lib/rbs/validator.rb +2 -2
- data/lib/rbs/version.rb +1 -1
- data/lib/rbs.rb +1 -2
- data/lib/rdoc/discover.rb +1 -1
- data/lib/rdoc_plugin/parser.rb +1 -1
- data/rbs.gemspec +4 -3
- data/rust/.gitignore +1 -0
- data/rust/Cargo.lock +378 -0
- data/rust/Cargo.toml +7 -0
- data/rust/ruby-rbs/Cargo.toml +22 -0
- data/rust/ruby-rbs/build.rs +764 -0
- data/rust/ruby-rbs/examples/locations.rs +60 -0
- data/rust/ruby-rbs/src/lib.rs +1 -0
- data/rust/ruby-rbs/src/node/mod.rs +742 -0
- data/rust/ruby-rbs/tests/sanity.rs +47 -0
- data/rust/ruby-rbs/vendor/rbs/config.yml +1 -0
- data/rust/ruby-rbs-sys/Cargo.toml +23 -0
- data/rust/ruby-rbs-sys/build.rs +204 -0
- data/rust/ruby-rbs-sys/src/lib.rs +50 -0
- data/rust/ruby-rbs-sys/vendor/rbs/include +1 -0
- data/rust/ruby-rbs-sys/vendor/rbs/src +1 -0
- data/rust/ruby-rbs-sys/wrapper.h +1 -0
- data/schema/typeParam.json +17 -1
- data/sig/ast/ruby/annotations.rbs +315 -4
- data/sig/ast/ruby/comment_block.rbs +8 -0
- data/sig/ast/ruby/declarations.rbs +102 -4
- data/sig/ast/ruby/members.rbs +108 -2
- data/sig/cli/diff.rbs +5 -11
- data/sig/cli/validate.rbs +12 -8
- data/sig/cli.rbs +18 -18
- 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 +39 -2
- data/sig/locator.rbs +0 -2
- data/sig/manifest.yaml +0 -1
- data/sig/method_builder.rbs +3 -1
- data/sig/parser.rbs +31 -13
- data/sig/prototype/helpers.rbs +2 -0
- data/sig/resolver/type_name_resolver.rbs +35 -7
- data/sig/source.rbs +3 -3
- data/sig/type_param.rbs +13 -8
- data/sig/types.rbs +6 -7
- data/sig/unit_test/spy.rbs +0 -8
- data/sig/unit_test/type_assertions.rbs +11 -0
- data/src/ast.c +410 -153
- data/src/lexer.c +1392 -1313
- data/src/lexer.re +3 -0
- data/src/lexstate.c +58 -37
- data/src/location.c +8 -48
- data/src/parser.c +977 -516
- data/src/string.c +0 -48
- data/src/util/rbs_allocator.c +89 -71
- 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/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/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 +110 -0
- data/stdlib/erb/0/erb.rbs +748 -347
- data/stdlib/etc/0/etc.rbs +55 -50
- data/stdlib/fileutils/0/fileutils.rbs +158 -139
- data/stdlib/forwardable/0/forwardable.rbs +13 -10
- data/stdlib/io-console/0/io-console.rbs +2 -2
- data/stdlib/json/0/json.rbs +217 -136
- 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 +475 -357
- 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 +25 -68
- 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 +2 -2
- 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 +1177 -85
- data/stdlib/strscan/0/string_scanner.rbs +27 -25
- 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 +3 -0
- data/stdlib/tsort/0/tsort.rbs +7 -6
- 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 +6 -6
- 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
- metadata +50 -6
|
@@ -1,50 +1,562 @@
|
|
|
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:
|
|
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 <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:
|
|
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 <code>StringIO.new(string,
|
|
550
|
+
# mode)</code>.
|
|
38
551
|
#
|
|
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
|
#
|
|
@@ -57,7 +569,7 @@ class StringIO
|
|
|
57
569
|
# - binmode -> self
|
|
58
570
|
# -->
|
|
59
571
|
# Sets the data mode in `self` to binary mode; see [Data
|
|
60
|
-
# Mode](rdoc-ref:
|
|
572
|
+
# Mode](rdoc-ref:StringIO@Data+Mode).
|
|
61
573
|
#
|
|
62
574
|
def binmode: () -> self
|
|
63
575
|
|
|
@@ -65,11 +577,16 @@ class StringIO
|
|
|
65
577
|
# rdoc-file=ext/stringio/stringio.c
|
|
66
578
|
# - close -> nil
|
|
67
579
|
# -->
|
|
68
|
-
# Closes `self` for both reading and writing
|
|
580
|
+
# Closes `self` for both reading and writing; returns `nil`:
|
|
69
581
|
#
|
|
70
|
-
#
|
|
582
|
+
# strio = StringIO.new
|
|
583
|
+
# strio.closed? # => false
|
|
584
|
+
# strio.close # => nil
|
|
585
|
+
# strio.closed? # => true
|
|
586
|
+
# strio.read # Raises IOError: not opened for reading
|
|
587
|
+
# strio.write # Raises IOError: not opened for writing
|
|
71
588
|
#
|
|
72
|
-
# Related: StringIO#close_read, StringIO#close_write.
|
|
589
|
+
# Related: StringIO#close_read, StringIO#close_write, StringIO.closed?.
|
|
73
590
|
#
|
|
74
591
|
def close: () -> nil
|
|
75
592
|
|
|
@@ -77,9 +594,15 @@ class StringIO
|
|
|
77
594
|
# rdoc-file=ext/stringio/stringio.c
|
|
78
595
|
# - close_read -> nil
|
|
79
596
|
# -->
|
|
80
|
-
# Closes `self` for reading; closed-write setting remains unchanged
|
|
597
|
+
# Closes `self` for reading; closed-write setting remains unchanged; returns
|
|
598
|
+
# `nil`:
|
|
81
599
|
#
|
|
82
|
-
#
|
|
600
|
+
# strio = StringIO.new
|
|
601
|
+
# strio.closed_read? # => false
|
|
602
|
+
# strio.close_read # => nil
|
|
603
|
+
# strio.closed_read? # => true
|
|
604
|
+
# strio.closed_write? # => false
|
|
605
|
+
# strio.read # Raises IOError: not opened for reading
|
|
83
606
|
#
|
|
84
607
|
# Related: StringIO#close, StringIO#close_write.
|
|
85
608
|
#
|
|
@@ -89,11 +612,17 @@ class StringIO
|
|
|
89
612
|
# rdoc-file=ext/stringio/stringio.c
|
|
90
613
|
# - close_write -> nil
|
|
91
614
|
# -->
|
|
92
|
-
# Closes `self` for writing; closed-read setting remains unchanged
|
|
615
|
+
# Closes `self` for writing; closed-read setting remains unchanged; returns
|
|
616
|
+
# `nil`:
|
|
93
617
|
#
|
|
94
|
-
#
|
|
618
|
+
# strio = StringIO.new
|
|
619
|
+
# strio.closed_write? # => false
|
|
620
|
+
# strio.close_write # => nil
|
|
621
|
+
# strio.closed_write? # => true
|
|
622
|
+
# strio.closed_read? # => false
|
|
623
|
+
# strio.write('foo') # Raises IOError: not opened for writing
|
|
95
624
|
#
|
|
96
|
-
# Related: StringIO#close, StringIO#close_read
|
|
625
|
+
# Related: StringIO#close, StringIO#close_read, StringIO#closed_write?.
|
|
97
626
|
#
|
|
98
627
|
def close_write: () -> nil
|
|
99
628
|
|
|
@@ -101,8 +630,16 @@ class StringIO
|
|
|
101
630
|
# rdoc-file=ext/stringio/stringio.c
|
|
102
631
|
# - closed? -> true or false
|
|
103
632
|
# -->
|
|
104
|
-
# Returns
|
|
105
|
-
#
|
|
633
|
+
# Returns whether `self` is closed for both reading and writing:
|
|
634
|
+
#
|
|
635
|
+
# strio = StringIO.new
|
|
636
|
+
# strio.closed? # => false # Open for reading and writing.
|
|
637
|
+
# strio.close_read
|
|
638
|
+
# strio.closed? # => false # Still open for writing.
|
|
639
|
+
# strio.close_write
|
|
640
|
+
# strio.closed? # => true # Now closed for both.
|
|
641
|
+
#
|
|
642
|
+
# Related: StringIO.closed_read?, StringIO.closed_write?.
|
|
106
643
|
#
|
|
107
644
|
def closed?: () -> bool
|
|
108
645
|
|
|
@@ -110,7 +647,14 @@ class StringIO
|
|
|
110
647
|
# rdoc-file=ext/stringio/stringio.c
|
|
111
648
|
# - closed_read? -> true or false
|
|
112
649
|
# -->
|
|
113
|
-
# Returns
|
|
650
|
+
# Returns whether `self` is closed for reading:
|
|
651
|
+
#
|
|
652
|
+
# strio = StringIO.new
|
|
653
|
+
# strio.closed_read? # => false
|
|
654
|
+
# strio.close_read
|
|
655
|
+
# strio.closed_read? # => true
|
|
656
|
+
#
|
|
657
|
+
# Related: StringIO#closed?, StringIO#closed_write?, StringIO#close_read.
|
|
114
658
|
#
|
|
115
659
|
def closed_read?: () -> bool
|
|
116
660
|
|
|
@@ -118,7 +662,14 @@ class StringIO
|
|
|
118
662
|
# rdoc-file=ext/stringio/stringio.c
|
|
119
663
|
# - closed_write? -> true or false
|
|
120
664
|
# -->
|
|
121
|
-
# Returns
|
|
665
|
+
# Returns whether `self` is closed for writing:
|
|
666
|
+
#
|
|
667
|
+
# strio = StringIO.new
|
|
668
|
+
# strio.closed_write? # => false
|
|
669
|
+
# strio.close_write
|
|
670
|
+
# strio.closed_write? # => true
|
|
671
|
+
#
|
|
672
|
+
# Related: StringIO#close_write, StringIO#closed?, StringIO#closed_read?.
|
|
122
673
|
#
|
|
123
674
|
def closed_write?: () -> bool
|
|
124
675
|
|
|
@@ -128,8 +679,137 @@ class StringIO
|
|
|
128
679
|
# - each_line(limit, chomp: false) {|line| ... } -> self
|
|
129
680
|
# - each_line(sep, limit, chomp: false) {|line| ... } -> self
|
|
130
681
|
# -->
|
|
131
|
-
#
|
|
132
|
-
#
|
|
682
|
+
# With a block given calls the block with each remaining line (see "Position"
|
|
683
|
+
# below) in the stream;
|
|
684
|
+
# returns `self`.
|
|
685
|
+
# Leaves stream position at end-of-stream.
|
|
686
|
+
# **No Arguments**
|
|
687
|
+
# With no arguments given,
|
|
688
|
+
# reads lines using the default record separator
|
|
689
|
+
# (global variable <code>$/</code>, whose initial value is <code>"\n"</code>).
|
|
690
|
+
# strio = StringIO.new(TEXT)
|
|
691
|
+
# strio.each_line {|line| p line }
|
|
692
|
+
# strio.eof? # => true
|
|
693
|
+
#
|
|
694
|
+
# Output:
|
|
695
|
+
# "First line\n"
|
|
696
|
+
# "Second line\n"
|
|
697
|
+
# "\n"
|
|
698
|
+
# "Fourth line\n"
|
|
699
|
+
# "Fifth line\n"
|
|
700
|
+
#
|
|
701
|
+
# <strong>Argument `sep`</strong>
|
|
702
|
+
# With only string argument `sep` given,
|
|
703
|
+
# reads lines using that string as the record separator:
|
|
704
|
+
# strio = StringIO.new(TEXT)
|
|
705
|
+
# strio.each_line(' ') {|line| p line }
|
|
706
|
+
#
|
|
707
|
+
# Output:
|
|
708
|
+
# "First "
|
|
709
|
+
# "line\nSecond "
|
|
710
|
+
# "line\n\nFourth "
|
|
711
|
+
# "line\nFifth "
|
|
712
|
+
# "line\n"
|
|
713
|
+
#
|
|
714
|
+
# <strong>Argument `limit`</strong>
|
|
715
|
+
# With only integer argument `limit` given,
|
|
716
|
+
# reads lines using the default record separator;
|
|
717
|
+
# also limits the size (in characters) of each line to the given limit:
|
|
718
|
+
# strio = StringIO.new(TEXT)
|
|
719
|
+
# strio.each_line(10) {|line| p line }
|
|
720
|
+
#
|
|
721
|
+
# Output:
|
|
722
|
+
# "First line"
|
|
723
|
+
# "\n"
|
|
724
|
+
# "Second lin"
|
|
725
|
+
# "e\n"
|
|
726
|
+
# "\n"
|
|
727
|
+
# "Fourth lin"
|
|
728
|
+
# "e\n"
|
|
729
|
+
# "Fifth line"
|
|
730
|
+
# "\n"
|
|
731
|
+
#
|
|
732
|
+
# <strong>Arguments `sep` and `limit`</strong>
|
|
733
|
+
# With arguments `sep` and `limit` both given,
|
|
734
|
+
# honors both:
|
|
735
|
+
# strio = StringIO.new(TEXT)
|
|
736
|
+
# strio.each_line(' ', 10) {|line| p line }
|
|
737
|
+
#
|
|
738
|
+
# Output:
|
|
739
|
+
# "First "
|
|
740
|
+
# "line\nSecon"
|
|
741
|
+
# "d "
|
|
742
|
+
# "line\n\nFour"
|
|
743
|
+
# "th "
|
|
744
|
+
# "line\nFifth"
|
|
745
|
+
# " "
|
|
746
|
+
# "line\n"
|
|
747
|
+
#
|
|
748
|
+
# **Position**
|
|
749
|
+
# As stated above, method `each` *remaining* line in the stream.
|
|
750
|
+
# In the examples above each `strio` object starts with its position at
|
|
751
|
+
# beginning-of-stream;
|
|
752
|
+
# but in other cases the position may be anywhere (see StringIO#pos):
|
|
753
|
+
# strio = StringIO.new(TEXT)
|
|
754
|
+
# strio.pos = 30 # Set stream position to character 30.
|
|
755
|
+
# strio.each_line {|line| p line }
|
|
756
|
+
#
|
|
757
|
+
# Output:
|
|
758
|
+
# " line\n"
|
|
759
|
+
# "Fifth line\n"
|
|
760
|
+
#
|
|
761
|
+
# In all the examples above, the stream position is at the beginning of a
|
|
762
|
+
# character;
|
|
763
|
+
# in other cases, that need not be so:
|
|
764
|
+
# s = 'こんにちは' # Five 3-byte characters.
|
|
765
|
+
# strio = StringIO.new(s)
|
|
766
|
+
# strio.pos = 3 # At beginning of second character.
|
|
767
|
+
# strio.each_line {|line| p line }
|
|
768
|
+
# strio.pos = 4 # At second byte of second character.
|
|
769
|
+
# strio.each_line {|line| p line }
|
|
770
|
+
# strio.pos = 5 # At third byte of second character.
|
|
771
|
+
# strio.each_line {|line| p line }
|
|
772
|
+
#
|
|
773
|
+
# Output:
|
|
774
|
+
# "んにちは"
|
|
775
|
+
# "\x82\x93にちは"
|
|
776
|
+
# "\x93にちは"
|
|
777
|
+
#
|
|
778
|
+
# **Special Record Separators**
|
|
779
|
+
# Like some methods in class `IO`, StringIO.each honors two special record
|
|
780
|
+
# separators;
|
|
781
|
+
# see [Special Line
|
|
782
|
+
# Separators](https://docs.ruby-lang.org/en/master/IO.html#class-IO-label-Specia
|
|
783
|
+
# l+Line+Separator+Values).
|
|
784
|
+
# strio = StringIO.new(TEXT)
|
|
785
|
+
# strio.each_line('') {|line| p line } # Read as paragraphs (separated by blank lines).
|
|
786
|
+
#
|
|
787
|
+
# Output:
|
|
788
|
+
# "First line\nSecond line\n\n"
|
|
789
|
+
# "Fourth line\nFifth line\n"
|
|
790
|
+
#
|
|
791
|
+
# strio = StringIO.new(TEXT)
|
|
792
|
+
# strio.each_line(nil) {|line| p line } # "Slurp"; read it all.
|
|
793
|
+
#
|
|
794
|
+
# Output:
|
|
795
|
+
# "First line\nSecond line\n\nFourth line\nFifth line\n"
|
|
796
|
+
#
|
|
797
|
+
# <strong>Keyword Argument `chomp`</strong>
|
|
798
|
+
# With keyword argument `chomp` given as `true` (the default is `false`),
|
|
799
|
+
# removes trailing newline (if any) from each line:
|
|
800
|
+
# strio = StringIO.new(TEXT)
|
|
801
|
+
# strio.each_line(chomp: true) {|line| p line }
|
|
802
|
+
#
|
|
803
|
+
# Output:
|
|
804
|
+
# "First line"
|
|
805
|
+
# "Second line"
|
|
806
|
+
# ""
|
|
807
|
+
# "Fourth line"
|
|
808
|
+
# "Fifth line"
|
|
809
|
+
#
|
|
810
|
+
# With no block given, returns a new
|
|
811
|
+
# [Enumerator](https://docs.ruby-lang.org/en/master/Enumerator.html).
|
|
812
|
+
# Related: StringIO.each_byte, StringIO.each_char, StringIO.each_codepoint.
|
|
133
813
|
#
|
|
134
814
|
def each: (?String sep, ?Integer limit, ?chomp: boolish) { (String) -> untyped } -> self
|
|
135
815
|
| (?String sep, ?Integer limit, ?chomp: boolish) -> ::Enumerator[String, self]
|
|
@@ -139,21 +819,83 @@ class StringIO
|
|
|
139
819
|
# - each_byte {|byte| ... } -> self
|
|
140
820
|
# -->
|
|
141
821
|
# With a block given, calls the block with each remaining byte in the stream;
|
|
142
|
-
#
|
|
822
|
+
# positions the stream at end-of-file; returns `self`:
|
|
823
|
+
#
|
|
824
|
+
# bytes = []
|
|
825
|
+
# strio = StringIO.new('hello') # Five 1-byte characters.
|
|
826
|
+
# strio.each_byte {|byte| bytes.push(byte) }
|
|
827
|
+
# strio.eof? # => true
|
|
828
|
+
# bytes # => [104, 101, 108, 108, 111]
|
|
829
|
+
# bytes = []
|
|
830
|
+
# strio = StringIO.new('тест') # Four 2-byte characters.
|
|
831
|
+
# strio.each_byte {|byte| bytes.push(byte) }
|
|
832
|
+
# bytes # => [209, 130, 208, 181, 209, 129, 209, 130]
|
|
833
|
+
# bytes = []
|
|
834
|
+
# strio = StringIO.new('こんにちは') # Five 3-byte characters.
|
|
835
|
+
# strio.each_byte {|byte| bytes.push(byte) }
|
|
836
|
+
# bytes # => [227, 129, 147, 227, 130, 147, 227, 129, 171, 227, 129, 161, 227, 129, 175]
|
|
143
837
|
#
|
|
144
|
-
#
|
|
838
|
+
# The position in the stream matters:
|
|
839
|
+
#
|
|
840
|
+
# bytes = []
|
|
841
|
+
# strio = StringIO.new('こんにちは')
|
|
842
|
+
# strio.getc # => "こ"
|
|
843
|
+
# strio.pos # => 3 # 3-byte character was read.
|
|
844
|
+
# strio.each_byte {|byte| bytes.push(byte) }
|
|
845
|
+
# bytes # => [227, 130, 147, 227, 129, 171, 227, 129, 161, 227, 129, 175]
|
|
846
|
+
#
|
|
847
|
+
# If at end-of-file, does not call the block:
|
|
848
|
+
#
|
|
849
|
+
# strio.eof? # => true
|
|
850
|
+
# strio.each_byte {|byte| fail 'Boo!' }
|
|
851
|
+
# strio.eof? # => true
|
|
852
|
+
#
|
|
853
|
+
# With no block given, returns a new [Enumerator](rdoc-ref:Enumerator).
|
|
854
|
+
#
|
|
855
|
+
# Related: StringIO#each_char, StringIO#each_codepoint, StringIO#each_line.
|
|
145
856
|
#
|
|
146
857
|
def each_byte: () { (Integer arg0) -> untyped } -> self
|
|
147
858
|
| () -> ::Enumerator[Integer, self]
|
|
148
859
|
|
|
149
860
|
# <!--
|
|
150
861
|
# rdoc-file=ext/stringio/stringio.c
|
|
151
|
-
# - each_char {|
|
|
862
|
+
# - each_char {|char| ... } -> self
|
|
152
863
|
# -->
|
|
153
864
|
# With a block given, calls the block with each remaining character in the
|
|
154
|
-
# stream;
|
|
865
|
+
# stream; positions the stream at end-of-file; returns `self`:
|
|
866
|
+
#
|
|
867
|
+
# chars = []
|
|
868
|
+
# strio = StringIO.new('hello')
|
|
869
|
+
# strio.each_char {|char| chars.push(char) }
|
|
870
|
+
# strio.eof? # => true
|
|
871
|
+
# chars # => ["h", "e", "l", "l", "o"]
|
|
872
|
+
# chars = []
|
|
873
|
+
# strio = StringIO.new('тест')
|
|
874
|
+
# strio.each_char {|char| chars.push(char) }
|
|
875
|
+
# chars # => ["т", "е", "с", "т"]
|
|
876
|
+
# chars = []
|
|
877
|
+
# strio = StringIO.new('こんにちは')
|
|
878
|
+
# strio.each_char {|char| chars.push(char) }
|
|
879
|
+
# chars # => ["こ", "ん", "に", "ち", "は"]
|
|
880
|
+
#
|
|
881
|
+
# Stream position matters:
|
|
882
|
+
#
|
|
883
|
+
# chars = []
|
|
884
|
+
# strio = StringIO.new('こんにちは')
|
|
885
|
+
# strio.getc # => "こ"
|
|
886
|
+
# strio.pos # => 3 # 3-byte character was read.
|
|
887
|
+
# strio.each_char {|char| chars.push(char) }
|
|
888
|
+
# chars # => ["ん", "に", "ち", "は"]
|
|
889
|
+
#
|
|
890
|
+
# When at end-of-stream does not call the block:
|
|
891
|
+
#
|
|
892
|
+
# strio.eof? # => true
|
|
893
|
+
# strio.each_char {|char| fail 'Boo!' }
|
|
894
|
+
# strio.eof? # => true
|
|
155
895
|
#
|
|
156
|
-
# With no block given, returns
|
|
896
|
+
# With no block given, returns a new [Enumerator](rdoc-ref:Enumerator).
|
|
897
|
+
#
|
|
898
|
+
# Related: StringIO#each_byte, StringIO#each_codepoint, StringIO#each_line.
|
|
157
899
|
#
|
|
158
900
|
def each_char: () { (String arg0) -> untyped } -> self
|
|
159
901
|
| () -> ::Enumerator[String, self]
|
|
@@ -162,10 +904,43 @@ class StringIO
|
|
|
162
904
|
# rdoc-file=ext/stringio/stringio.c
|
|
163
905
|
# - each_codepoint {|codepoint| ... } -> self
|
|
164
906
|
# -->
|
|
165
|
-
# With a block given, calls the block with each
|
|
166
|
-
# stream;
|
|
907
|
+
# With a block given, calls the block with each successive codepoint from self;
|
|
908
|
+
# sets the position to end-of-stream; returns `self`.
|
|
909
|
+
#
|
|
910
|
+
# Each codepoint is the integer value for a character; returns self:
|
|
911
|
+
#
|
|
912
|
+
# codepoints = []
|
|
913
|
+
# strio = StringIO.new('hello')
|
|
914
|
+
# strio.each_codepoint {|codepoint| codepoints.push(codepoint) }
|
|
915
|
+
# strio.eof? # => true
|
|
916
|
+
# codepoints # => [104, 101, 108, 108, 111]
|
|
917
|
+
# codepoints = []
|
|
918
|
+
# strio = StringIO.new('тест')
|
|
919
|
+
# strio.each_codepoint {|codepoint| codepoints.push(codepoint) }
|
|
920
|
+
# codepoints # => [1090, 1077, 1089, 1090]
|
|
921
|
+
# codepoints = []
|
|
922
|
+
# strio = StringIO.new('こんにちは')
|
|
923
|
+
# strio.each_codepoint {|codepoint| codepoints.push(codepoint) }
|
|
924
|
+
# codepoints # => [12371, 12435, 12395, 12385, 12399]
|
|
925
|
+
#
|
|
926
|
+
# Position in the stream matters:
|
|
927
|
+
#
|
|
928
|
+
# codepoints = []
|
|
929
|
+
# strio = StringIO.new('こんにちは')
|
|
930
|
+
# strio.getc # => "こ"
|
|
931
|
+
# strio.pos # => 3
|
|
932
|
+
# strio.each_codepoint {|codepoint| codepoints.push(codepoint) }
|
|
933
|
+
# codepoints # => [12435, 12395, 12385, 12399]
|
|
167
934
|
#
|
|
168
|
-
#
|
|
935
|
+
# When at end-of-stream, the block is not called:
|
|
936
|
+
#
|
|
937
|
+
# strio.eof? # => true
|
|
938
|
+
# strio.each_codepoint {|codepoint| fail 'Boo!' }
|
|
939
|
+
# strio.eof? # => true
|
|
940
|
+
#
|
|
941
|
+
# With no block given, returns a new [Enumerator](rdoc-ref:Enumerator).
|
|
942
|
+
#
|
|
943
|
+
# Related: StringIO#each_byte, StringIO#each_char, StringIO#each_line.
|
|
169
944
|
#
|
|
170
945
|
def each_codepoint: () { (Integer arg0) -> untyped } -> self
|
|
171
946
|
| () -> ::Enumerator[Integer, self]
|
|
@@ -174,10 +949,18 @@ class StringIO
|
|
|
174
949
|
# rdoc-file=ext/stringio/stringio.c
|
|
175
950
|
# - eof? -> true or false
|
|
176
951
|
# -->
|
|
177
|
-
# Returns `
|
|
178
|
-
# [Position](rdoc-ref:IO@Position).
|
|
952
|
+
# Returns whether `self` is positioned at end-of-stream:
|
|
179
953
|
#
|
|
180
|
-
#
|
|
954
|
+
# strio = StringIO.new('foo')
|
|
955
|
+
# strio.pos # => 0
|
|
956
|
+
# strio.eof? # => false
|
|
957
|
+
# strio.read # => "foo"
|
|
958
|
+
# strio.pos # => 3
|
|
959
|
+
# strio.eof? # => true
|
|
960
|
+
# strio.close_read
|
|
961
|
+
# strio.eof? # Raises IOError: not opened for reading
|
|
962
|
+
#
|
|
963
|
+
# Related: StringIO#pos.
|
|
181
964
|
#
|
|
182
965
|
def eof: () -> bool
|
|
183
966
|
|
|
@@ -193,7 +976,7 @@ class StringIO
|
|
|
193
976
|
# rdoc-file=ext/stringio/stringio.c
|
|
194
977
|
# - fileno()
|
|
195
978
|
# -->
|
|
196
|
-
# Returns `nil
|
|
979
|
+
# Returns `nil`; for compatibility with IO.
|
|
197
980
|
#
|
|
198
981
|
def fileno: () -> nil
|
|
199
982
|
|
|
@@ -201,7 +984,7 @@ class StringIO
|
|
|
201
984
|
# rdoc-file=ext/stringio/stringio.c
|
|
202
985
|
# - flush()
|
|
203
986
|
# -->
|
|
204
|
-
# Returns
|
|
987
|
+
# Returns `self`; for compatibility with IO.
|
|
205
988
|
#
|
|
206
989
|
def flush: () -> self
|
|
207
990
|
|
|
@@ -209,25 +992,86 @@ class StringIO
|
|
|
209
992
|
# rdoc-file=ext/stringio/stringio.c
|
|
210
993
|
# - fsync()
|
|
211
994
|
# -->
|
|
212
|
-
# Returns 0
|
|
995
|
+
# Returns 0; for compatibility with IO.
|
|
213
996
|
#
|
|
214
997
|
def fsync: () -> Integer?
|
|
215
998
|
|
|
216
999
|
# <!--
|
|
217
1000
|
# rdoc-file=ext/stringio/stringio.c
|
|
218
|
-
# - getbyte ->
|
|
1001
|
+
# - getbyte -> integer or nil
|
|
219
1002
|
# -->
|
|
220
|
-
# Reads and returns the next
|
|
221
|
-
#
|
|
1003
|
+
# Reads and returns the next integer byte (not character) from the stream:
|
|
1004
|
+
#
|
|
1005
|
+
# s = 'foo'
|
|
1006
|
+
# s.bytes # => [102, 111, 111]
|
|
1007
|
+
# strio = StringIO.new(s)
|
|
1008
|
+
# strio.getbyte # => 102
|
|
1009
|
+
# strio.getbyte # => 111
|
|
1010
|
+
# strio.getbyte # => 111
|
|
1011
|
+
#
|
|
1012
|
+
# Returns `nil` if at end-of-stream:
|
|
1013
|
+
#
|
|
1014
|
+
# strio.eof? # => true
|
|
1015
|
+
# strio.getbyte # => nil
|
|
1016
|
+
#
|
|
1017
|
+
# Returns a byte, not a character:
|
|
1018
|
+
#
|
|
1019
|
+
# s = 'Привет'
|
|
1020
|
+
# s.bytes
|
|
1021
|
+
# # => [208, 159, 209, 128, 208, 184, 208, 178, 208, 181, 209, 130]
|
|
1022
|
+
# strio = StringIO.new(s)
|
|
1023
|
+
# strio.getbyte # => 208
|
|
1024
|
+
# strio.getbyte # => 159
|
|
1025
|
+
#
|
|
1026
|
+
# s = 'こんにちは'
|
|
1027
|
+
# s.bytes
|
|
1028
|
+
# # => [227, 129, 147, 227, 130, 147, 227, 129, 171, 227, 129, 161, 227, 129, 175]
|
|
1029
|
+
# strio = StringIO.new(s)
|
|
1030
|
+
# strio.getbyte # => 227
|
|
1031
|
+
# strio.getbyte # => 129
|
|
1032
|
+
#
|
|
1033
|
+
# Related: #each_byte, #ungetbyte, #getc.
|
|
222
1034
|
#
|
|
223
1035
|
def getbyte: () -> Integer?
|
|
224
1036
|
|
|
225
1037
|
# <!--
|
|
226
1038
|
# rdoc-file=ext/stringio/stringio.c
|
|
227
|
-
# - getc -> character or nil
|
|
1039
|
+
# - getc -> character, byte, or nil
|
|
228
1040
|
# -->
|
|
229
|
-
# Reads and returns the next character from the stream
|
|
230
|
-
#
|
|
1041
|
+
# Reads and returns the next character (or byte; see below) from the stream:
|
|
1042
|
+
#
|
|
1043
|
+
# strio = StringIO.new('foo')
|
|
1044
|
+
# strio.getc # => "f"
|
|
1045
|
+
# strio.getc # => "o"
|
|
1046
|
+
# strio.getc # => "o"
|
|
1047
|
+
#
|
|
1048
|
+
# Returns `nil` if at end-of-stream:
|
|
1049
|
+
#
|
|
1050
|
+
# strio.eof? # => true
|
|
1051
|
+
# strio.getc # => nil
|
|
1052
|
+
#
|
|
1053
|
+
# Returns characters, not bytes:
|
|
1054
|
+
#
|
|
1055
|
+
# strio = StringIO.new('Привет')
|
|
1056
|
+
# strio.getc # => "П"
|
|
1057
|
+
# strio.getc # => "р"
|
|
1058
|
+
#
|
|
1059
|
+
# strio = StringIO.new('こんにちは')
|
|
1060
|
+
# strio.getc # => "こ"
|
|
1061
|
+
# strio.getc # => "ん"
|
|
1062
|
+
#
|
|
1063
|
+
# In each of the examples above, the stream is positioned at the beginning of a
|
|
1064
|
+
# character; in other cases that need not be true:
|
|
1065
|
+
#
|
|
1066
|
+
# strio = StringIO.new('こんにちは') # Five 3-byte characters.
|
|
1067
|
+
# strio.pos = 3 # => 3 # At beginning of second character; returns character.
|
|
1068
|
+
# strio.getc # => "ん"
|
|
1069
|
+
# strio.pos = 4 # => 4 # At second byte of second character; returns byte.
|
|
1070
|
+
# strio.getc # => "\x82"
|
|
1071
|
+
# strio.pos = 5 # => 5 # At third byte of second character; returns byte.
|
|
1072
|
+
# strio.getc # => "\x93"
|
|
1073
|
+
#
|
|
1074
|
+
# Related: #getbyte, #putc, #ungetc.
|
|
231
1075
|
#
|
|
232
1076
|
def getc: () -> String?
|
|
233
1077
|
|
|
@@ -237,26 +1081,131 @@ class StringIO
|
|
|
237
1081
|
# - gets(limit, chomp: false) -> string or nil
|
|
238
1082
|
# - gets(sep, limit, chomp: false) -> string or nil
|
|
239
1083
|
# -->
|
|
240
|
-
# Reads and returns a line from the stream;
|
|
241
|
-
#
|
|
1084
|
+
# Reads and returns a line from the stream; returns `nil` if at end-of-stream.
|
|
1085
|
+
#
|
|
1086
|
+
# Side effects:
|
|
1087
|
+
#
|
|
1088
|
+
# * Increments stream position by the number of bytes read.
|
|
1089
|
+
# * Assigns the return value to global variable <code>$_</code>.
|
|
1090
|
+
#
|
|
1091
|
+
# With no arguments given, reads a line using the default record separator
|
|
1092
|
+
# (global variable <code>$/</code>,* whose initial value is <code>"\n"</code>):
|
|
1093
|
+
#
|
|
1094
|
+
# strio = StringIO.new(TEXT)
|
|
1095
|
+
# strio.pos # => 0
|
|
1096
|
+
# strio.gets # => "First line\n"
|
|
1097
|
+
# strio.pos # => 11
|
|
1098
|
+
# $_ # => "First line\n"
|
|
1099
|
+
# strio.gets # => "Second line\n"
|
|
1100
|
+
# strio.read # => "\nFourth line\nFifth line\n"
|
|
1101
|
+
# strio.eof? # => true
|
|
1102
|
+
# strio.gets # => nil
|
|
1103
|
+
#
|
|
1104
|
+
# strio = StringIO.new('Привет') # Six 2-byte characters
|
|
1105
|
+
# strio.pos # => 0
|
|
1106
|
+
# strio.gets # => "Привет"
|
|
1107
|
+
# strio.pos # => 12
|
|
1108
|
+
#
|
|
1109
|
+
# <strong>Argument `sep`</strong>
|
|
1110
|
+
#
|
|
1111
|
+
# With only string argument `sep` given, reads a line using that string as the
|
|
1112
|
+
# record separator:
|
|
1113
|
+
#
|
|
1114
|
+
# strio = StringIO.new(TEXT)
|
|
1115
|
+
# strio.gets(' ') # => "First "
|
|
1116
|
+
# strio.gets(' ') # => "line\nSecond "
|
|
1117
|
+
# strio.gets(' ') # => "line\n\nFourth "
|
|
1118
|
+
#
|
|
1119
|
+
# <strong>Argument `limit`</strong>
|
|
1120
|
+
#
|
|
1121
|
+
# With only integer argument `limit` given, reads a line using the default
|
|
1122
|
+
# record separator; limits the size (in characters) of each line to the given
|
|
1123
|
+
# limit:
|
|
1124
|
+
#
|
|
1125
|
+
# strio = StringIO.new(TEXT)
|
|
1126
|
+
# strio.gets(10) # => "First line"
|
|
1127
|
+
# strio.gets(10) # => "\n"
|
|
1128
|
+
# strio.gets(10) # => "Second lin"
|
|
1129
|
+
# strio.gets(10) # => "e\n"
|
|
1130
|
+
#
|
|
1131
|
+
# <strong>Arguments `sep` and `limit`</strong>
|
|
1132
|
+
#
|
|
1133
|
+
# With arguments `sep` and `limit` both given, honors both:
|
|
1134
|
+
#
|
|
1135
|
+
# strio = StringIO.new(TEXT)
|
|
1136
|
+
# strio.gets(' ', 10) # => "First "
|
|
1137
|
+
# strio.gets(' ', 10) # => "line\nSecon"
|
|
1138
|
+
# strio.gets(' ', 10) # => "d "
|
|
1139
|
+
#
|
|
1140
|
+
# **Position**
|
|
1141
|
+
#
|
|
1142
|
+
# As stated above, method `gets` reads and returns the next line in the stream.
|
|
1143
|
+
#
|
|
1144
|
+
# In the examples above each `strio` object starts with its position at
|
|
1145
|
+
# beginning-of-stream; but in other cases the position may be anywhere:
|
|
1146
|
+
#
|
|
1147
|
+
# strio = StringIO.new(TEXT)
|
|
1148
|
+
# strio.pos = 12
|
|
1149
|
+
# strio.gets # => "econd line\n"
|
|
1150
|
+
#
|
|
1151
|
+
# The position need not be at a character boundary:
|
|
1152
|
+
#
|
|
1153
|
+
# strio = StringIO.new('Привет') # Six 2-byte characters.
|
|
1154
|
+
# strio.pos = 2 # At beginning of second character.
|
|
1155
|
+
# strio.gets # => "ривет"
|
|
1156
|
+
# strio.pos = 3 # In middle of second character.
|
|
1157
|
+
# strio.gets # => "\x80ивет"
|
|
1158
|
+
#
|
|
1159
|
+
# **Special Record Separators**
|
|
1160
|
+
#
|
|
1161
|
+
# Like some methods in class IO, method `gets` honors two special record
|
|
1162
|
+
# separators; see [Special Line
|
|
1163
|
+
# Separators](https://docs.ruby-lang.org/en/master/IO.html#class-IO-label-Specia
|
|
1164
|
+
# l+Line+Separator+Values):
|
|
1165
|
+
#
|
|
1166
|
+
# strio = StringIO.new(TEXT)
|
|
1167
|
+
# strio.gets('') # Read "paragraph" (up to empty line).
|
|
1168
|
+
# # => "First line\nSecond line\n\n"
|
|
1169
|
+
#
|
|
1170
|
+
# strio = StringIO.new(TEXT)
|
|
1171
|
+
# strio.gets(nil) # "Slurp": read all.
|
|
1172
|
+
# # => "First line\nSecond line\n\nFourth line\nFifth line\n"
|
|
1173
|
+
#
|
|
1174
|
+
# <strong>Keyword Argument `chomp`</strong>
|
|
1175
|
+
#
|
|
1176
|
+
# With keyword argument `chomp` given as `true` (the default is `false`),
|
|
1177
|
+
# removes the trailing newline (if any) from the returned line:
|
|
1178
|
+
#
|
|
1179
|
+
# strio = StringIO.new(TEXT)
|
|
1180
|
+
# strio.gets # => "First line\n"
|
|
1181
|
+
# strio.gets(chomp: true) # => "Second line"
|
|
1182
|
+
#
|
|
1183
|
+
# Related: #each_line, #readlines, [Kernel#puts](rdoc-ref:Kernel#puts).
|
|
242
1184
|
#
|
|
243
1185
|
def gets: (?String sep, ?Integer limit, ?chomp: boolish) -> String?
|
|
244
1186
|
|
|
245
1187
|
# <!--
|
|
246
1188
|
# rdoc-file=ext/stringio/stringio.c
|
|
247
|
-
# -
|
|
1189
|
+
# - internal_encoding -> nil
|
|
248
1190
|
# -->
|
|
249
|
-
# Returns
|
|
250
|
-
# Otherwise returns `nil`.
|
|
1191
|
+
# Returns `nil`; for compatibility with IO.
|
|
251
1192
|
#
|
|
252
1193
|
def internal_encoding: () -> Encoding
|
|
253
1194
|
|
|
254
1195
|
# <!--
|
|
255
1196
|
# rdoc-file=ext/stringio/stringio.c
|
|
256
|
-
# -
|
|
1197
|
+
# - external_encoding -> encoding or nil
|
|
257
1198
|
# -->
|
|
258
|
-
# Returns
|
|
259
|
-
#
|
|
1199
|
+
# Returns an Encoding object that represents the encoding of the string; see
|
|
1200
|
+
# [Encodings](rdoc-ref:StringIO@Encodings):
|
|
1201
|
+
#
|
|
1202
|
+
# strio = StringIO.new('foo')
|
|
1203
|
+
# strio.external_encoding # => #<Encoding:UTF-8>
|
|
1204
|
+
#
|
|
1205
|
+
# Returns `nil` if `self` has no string and is in write mode:
|
|
1206
|
+
#
|
|
1207
|
+
# strio = StringIO.new(nil, 'w+')
|
|
1208
|
+
# strio.external_encoding # => nil
|
|
260
1209
|
#
|
|
261
1210
|
def external_encoding: () -> Encoding
|
|
262
1211
|
|
|
@@ -264,7 +1213,7 @@ class StringIO
|
|
|
264
1213
|
# rdoc-file=ext/stringio/stringio.c
|
|
265
1214
|
# - isatty()
|
|
266
1215
|
# -->
|
|
267
|
-
# Returns `false
|
|
1216
|
+
# Returns `false`; for compatibility with IO.
|
|
268
1217
|
#
|
|
269
1218
|
def isatty: () -> bool
|
|
270
1219
|
|
|
@@ -273,7 +1222,7 @@ class StringIO
|
|
|
273
1222
|
# - lineno -> current_line_number
|
|
274
1223
|
# -->
|
|
275
1224
|
# Returns the current line number in `self`; see [Line
|
|
276
|
-
# Number](rdoc-ref:
|
|
1225
|
+
# Number](rdoc-ref:StringIO@Line+Number).
|
|
277
1226
|
#
|
|
278
1227
|
def lineno: () -> Integer
|
|
279
1228
|
|
|
@@ -282,7 +1231,7 @@ class StringIO
|
|
|
282
1231
|
# - lineno = new_line_number -> new_line_number
|
|
283
1232
|
# -->
|
|
284
1233
|
# Sets the current line number in `self` to the given `new_line_number`; see
|
|
285
|
-
# [Line Number](rdoc-ref:
|
|
1234
|
+
# [Line Number](rdoc-ref:StringIO@Line+Number).
|
|
286
1235
|
#
|
|
287
1236
|
def lineno=: (Integer arg0) -> Integer
|
|
288
1237
|
|
|
@@ -290,7 +1239,7 @@ class StringIO
|
|
|
290
1239
|
# rdoc-file=ext/stringio/stringio.c
|
|
291
1240
|
# - pid()
|
|
292
1241
|
# -->
|
|
293
|
-
# Returns `nil
|
|
1242
|
+
# Returns `nil`; for compatibility with IO.
|
|
294
1243
|
#
|
|
295
1244
|
def pid: () -> nil
|
|
296
1245
|
|
|
@@ -298,7 +1247,8 @@ class StringIO
|
|
|
298
1247
|
# rdoc-file=ext/stringio/stringio.c
|
|
299
1248
|
# - pos -> stream_position
|
|
300
1249
|
# -->
|
|
301
|
-
# Returns the current position (in bytes); see
|
|
1250
|
+
# Returns the current position (in bytes); see
|
|
1251
|
+
# [Position](rdoc-ref:StringIO@Position).
|
|
302
1252
|
#
|
|
303
1253
|
def pos: () -> Integer
|
|
304
1254
|
|
|
@@ -306,7 +1256,8 @@ class StringIO
|
|
|
306
1256
|
# rdoc-file=ext/stringio/stringio.c
|
|
307
1257
|
# - pos = new_position -> new_position
|
|
308
1258
|
# -->
|
|
309
|
-
# Sets the current position (in bytes); see
|
|
1259
|
+
# Sets the current position (in bytes); see
|
|
1260
|
+
# [Position](rdoc-ref:StringIO@Position).
|
|
310
1261
|
#
|
|
311
1262
|
def pos=: (Integer arg0) -> Integer
|
|
312
1263
|
|
|
@@ -391,8 +1342,8 @@ class StringIO
|
|
|
391
1342
|
# rdoc-file=ext/stringio/stringio.c
|
|
392
1343
|
# - seek(offset, whence = SEEK_SET) -> 0
|
|
393
1344
|
# -->
|
|
394
|
-
# Sets the
|
|
395
|
-
#
|
|
1345
|
+
# Sets the position to the given integer `offset` (in bytes), with respect to a
|
|
1346
|
+
# given constant `whence`; see [IO#seek](rdoc-ref:IO#seek).
|
|
396
1347
|
#
|
|
397
1348
|
def seek: (Integer amount, ?Integer whence) -> Integer
|
|
398
1349
|
|
|
@@ -432,7 +1383,7 @@ class StringIO
|
|
|
432
1383
|
# rdoc-file=ext/stringio/stringio.c
|
|
433
1384
|
# - string = other_string -> other_string
|
|
434
1385
|
# -->
|
|
435
|
-
#
|
|
1386
|
+
# Replaces the stored string with `other_string`, and sets the position to zero;
|
|
436
1387
|
# returns `other_string`:
|
|
437
1388
|
#
|
|
438
1389
|
# StringIO.open('foo') do |strio|
|
|
@@ -446,16 +1397,19 @@ class StringIO
|
|
|
446
1397
|
# "foo"
|
|
447
1398
|
# "bar"
|
|
448
1399
|
#
|
|
449
|
-
# Related: StringIO#string (returns the
|
|
1400
|
+
# Related: StringIO#string (returns the stored string).
|
|
450
1401
|
#
|
|
451
1402
|
def string=: (String str) -> String
|
|
452
1403
|
|
|
453
1404
|
# <!--
|
|
454
1405
|
# rdoc-file=ext/stringio/stringio.c
|
|
455
|
-
# -
|
|
456
|
-
# - strio.size -> integer
|
|
1406
|
+
# - size -> integer
|
|
457
1407
|
# -->
|
|
458
|
-
# Returns the
|
|
1408
|
+
# Returns the number of bytes in the string in `self`:
|
|
1409
|
+
#
|
|
1410
|
+
# StringIO.new('hello').size # => 5 # Five 1-byte characters.
|
|
1411
|
+
# StringIO.new('тест').size # => 8 # Four 2-byte characters.
|
|
1412
|
+
# StringIO.new('こんにちは').size # => 15 # Five 3-byte characters.
|
|
459
1413
|
#
|
|
460
1414
|
def size: () -> Integer
|
|
461
1415
|
|
|
@@ -483,7 +1437,8 @@ class StringIO
|
|
|
483
1437
|
# rdoc-file=ext/stringio/stringio.c
|
|
484
1438
|
# - pos -> stream_position
|
|
485
1439
|
# -->
|
|
486
|
-
# Returns the current position (in bytes); see
|
|
1440
|
+
# Returns the current position (in bytes); see
|
|
1441
|
+
# [Position](rdoc-ref:StringIO@Position).
|
|
487
1442
|
#
|
|
488
1443
|
def tell: () -> Integer
|
|
489
1444
|
|
|
@@ -497,7 +1452,7 @@ class StringIO
|
|
|
497
1452
|
def truncate: (Integer) -> 0
|
|
498
1453
|
|
|
499
1454
|
# <!-- rdoc-file=ext/stringio/stringio.c -->
|
|
500
|
-
# Returns `false
|
|
1455
|
+
# Returns `false`; for compatibility with IO.
|
|
501
1456
|
#
|
|
502
1457
|
def tty?: () -> bool
|
|
503
1458
|
|
|
@@ -546,17 +1501,154 @@ class StringIO
|
|
|
546
1501
|
| () -> ::Enumerator[Integer, self]
|
|
547
1502
|
|
|
548
1503
|
# <!-- rdoc-file=ext/stringio/stringio.c -->
|
|
549
|
-
#
|
|
550
|
-
#
|
|
1504
|
+
# With a block given calls the block with each remaining line (see "Position"
|
|
1505
|
+
# below) in the stream;
|
|
1506
|
+
# returns `self`.
|
|
1507
|
+
# Leaves stream position at end-of-stream.
|
|
1508
|
+
# **No Arguments**
|
|
1509
|
+
# With no arguments given,
|
|
1510
|
+
# reads lines using the default record separator
|
|
1511
|
+
# (global variable <code>$/</code>, whose initial value is <code>"\n"</code>).
|
|
1512
|
+
# strio = StringIO.new(TEXT)
|
|
1513
|
+
# strio.each_line {|line| p line }
|
|
1514
|
+
# strio.eof? # => true
|
|
1515
|
+
#
|
|
1516
|
+
# Output:
|
|
1517
|
+
# "First line\n"
|
|
1518
|
+
# "Second line\n"
|
|
1519
|
+
# "\n"
|
|
1520
|
+
# "Fourth line\n"
|
|
1521
|
+
# "Fifth line\n"
|
|
1522
|
+
#
|
|
1523
|
+
# <strong>Argument `sep`</strong>
|
|
1524
|
+
# With only string argument `sep` given,
|
|
1525
|
+
# reads lines using that string as the record separator:
|
|
1526
|
+
# strio = StringIO.new(TEXT)
|
|
1527
|
+
# strio.each_line(' ') {|line| p line }
|
|
1528
|
+
#
|
|
1529
|
+
# Output:
|
|
1530
|
+
# "First "
|
|
1531
|
+
# "line\nSecond "
|
|
1532
|
+
# "line\n\nFourth "
|
|
1533
|
+
# "line\nFifth "
|
|
1534
|
+
# "line\n"
|
|
1535
|
+
#
|
|
1536
|
+
# <strong>Argument `limit`</strong>
|
|
1537
|
+
# With only integer argument `limit` given,
|
|
1538
|
+
# reads lines using the default record separator;
|
|
1539
|
+
# also limits the size (in characters) of each line to the given limit:
|
|
1540
|
+
# strio = StringIO.new(TEXT)
|
|
1541
|
+
# strio.each_line(10) {|line| p line }
|
|
1542
|
+
#
|
|
1543
|
+
# Output:
|
|
1544
|
+
# "First line"
|
|
1545
|
+
# "\n"
|
|
1546
|
+
# "Second lin"
|
|
1547
|
+
# "e\n"
|
|
1548
|
+
# "\n"
|
|
1549
|
+
# "Fourth lin"
|
|
1550
|
+
# "e\n"
|
|
1551
|
+
# "Fifth line"
|
|
1552
|
+
# "\n"
|
|
1553
|
+
#
|
|
1554
|
+
# <strong>Arguments `sep` and `limit`</strong>
|
|
1555
|
+
# With arguments `sep` and `limit` both given,
|
|
1556
|
+
# honors both:
|
|
1557
|
+
# strio = StringIO.new(TEXT)
|
|
1558
|
+
# strio.each_line(' ', 10) {|line| p line }
|
|
1559
|
+
#
|
|
1560
|
+
# Output:
|
|
1561
|
+
# "First "
|
|
1562
|
+
# "line\nSecon"
|
|
1563
|
+
# "d "
|
|
1564
|
+
# "line\n\nFour"
|
|
1565
|
+
# "th "
|
|
1566
|
+
# "line\nFifth"
|
|
1567
|
+
# " "
|
|
1568
|
+
# "line\n"
|
|
1569
|
+
#
|
|
1570
|
+
# **Position**
|
|
1571
|
+
# As stated above, method `each` *remaining* line in the stream.
|
|
1572
|
+
# In the examples above each `strio` object starts with its position at
|
|
1573
|
+
# beginning-of-stream;
|
|
1574
|
+
# but in other cases the position may be anywhere (see StringIO#pos):
|
|
1575
|
+
# strio = StringIO.new(TEXT)
|
|
1576
|
+
# strio.pos = 30 # Set stream position to character 30.
|
|
1577
|
+
# strio.each_line {|line| p line }
|
|
1578
|
+
#
|
|
1579
|
+
# Output:
|
|
1580
|
+
# " line\n"
|
|
1581
|
+
# "Fifth line\n"
|
|
1582
|
+
#
|
|
1583
|
+
# In all the examples above, the stream position is at the beginning of a
|
|
1584
|
+
# character;
|
|
1585
|
+
# in other cases, that need not be so:
|
|
1586
|
+
# s = 'こんにちは' # Five 3-byte characters.
|
|
1587
|
+
# strio = StringIO.new(s)
|
|
1588
|
+
# strio.pos = 3 # At beginning of second character.
|
|
1589
|
+
# strio.each_line {|line| p line }
|
|
1590
|
+
# strio.pos = 4 # At second byte of second character.
|
|
1591
|
+
# strio.each_line {|line| p line }
|
|
1592
|
+
# strio.pos = 5 # At third byte of second character.
|
|
1593
|
+
# strio.each_line {|line| p line }
|
|
1594
|
+
#
|
|
1595
|
+
# Output:
|
|
1596
|
+
# "んにちは"
|
|
1597
|
+
# "\x82\x93にちは"
|
|
1598
|
+
# "\x93にちは"
|
|
1599
|
+
#
|
|
1600
|
+
# **Special Record Separators**
|
|
1601
|
+
# Like some methods in class `IO`, StringIO.each honors two special record
|
|
1602
|
+
# separators;
|
|
1603
|
+
# see [Special Line
|
|
1604
|
+
# Separators](https://docs.ruby-lang.org/en/master/IO.html#class-IO-label-Specia
|
|
1605
|
+
# l+Line+Separator+Values).
|
|
1606
|
+
# strio = StringIO.new(TEXT)
|
|
1607
|
+
# strio.each_line('') {|line| p line } # Read as paragraphs (separated by blank lines).
|
|
1608
|
+
#
|
|
1609
|
+
# Output:
|
|
1610
|
+
# "First line\nSecond line\n\n"
|
|
1611
|
+
# "Fourth line\nFifth line\n"
|
|
1612
|
+
#
|
|
1613
|
+
# strio = StringIO.new(TEXT)
|
|
1614
|
+
# strio.each_line(nil) {|line| p line } # "Slurp"; read it all.
|
|
1615
|
+
#
|
|
1616
|
+
# Output:
|
|
1617
|
+
# "First line\nSecond line\n\nFourth line\nFifth line\n"
|
|
1618
|
+
#
|
|
1619
|
+
# <strong>Keyword Argument `chomp`</strong>
|
|
1620
|
+
# With keyword argument `chomp` given as `true` (the default is `false`),
|
|
1621
|
+
# removes trailing newline (if any) from each line:
|
|
1622
|
+
# strio = StringIO.new(TEXT)
|
|
1623
|
+
# strio.each_line(chomp: true) {|line| p line }
|
|
1624
|
+
#
|
|
1625
|
+
# Output:
|
|
1626
|
+
# "First line"
|
|
1627
|
+
# "Second line"
|
|
1628
|
+
# ""
|
|
1629
|
+
# "Fourth line"
|
|
1630
|
+
# "Fifth line"
|
|
1631
|
+
#
|
|
1632
|
+
# With no block given, returns a new
|
|
1633
|
+
# [Enumerator](https://docs.ruby-lang.org/en/master/Enumerator.html).
|
|
1634
|
+
# Related: StringIO.each_byte, StringIO.each_char, StringIO.each_codepoint.
|
|
551
1635
|
#
|
|
552
1636
|
def each_line: (?String sep, ?Integer limit, ?chomp: boolish) { (String) -> untyped } -> self
|
|
553
1637
|
| (?String sep, ?Integer limit, ?chomp: boolish) -> ::Enumerator[String, self]
|
|
554
1638
|
|
|
555
1639
|
# <!-- rdoc-file=ext/stringio/stringio.c -->
|
|
556
|
-
# Returns `
|
|
557
|
-
#
|
|
558
|
-
#
|
|
559
|
-
#
|
|
1640
|
+
# Returns whether `self` is positioned at end-of-stream:
|
|
1641
|
+
#
|
|
1642
|
+
# strio = StringIO.new('foo')
|
|
1643
|
+
# strio.pos # => 0
|
|
1644
|
+
# strio.eof? # => false
|
|
1645
|
+
# strio.read # => "foo"
|
|
1646
|
+
# strio.pos # => 3
|
|
1647
|
+
# strio.eof? # => true
|
|
1648
|
+
# strio.close_read
|
|
1649
|
+
# strio.eof? # Raises IOError: not opened for reading
|
|
1650
|
+
#
|
|
1651
|
+
# Related: StringIO#pos.
|
|
560
1652
|
#
|
|
561
1653
|
def eof?: () -> bool
|
|
562
1654
|
|