rbs 4.0.0.dev.5 → 4.0.1.dev.1
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/workflows/c-check.yml +8 -4
- data/.github/workflows/comments.yml +3 -1
- data/.github/workflows/dependabot.yml +1 -1
- data/.github/workflows/ruby.yml +10 -0
- data/.github/workflows/rust.yml +95 -0
- data/CHANGELOG.md +323 -0
- data/Rakefile +12 -29
- data/Steepfile +1 -0
- data/config.yml +393 -37
- data/core/array.rbs +218 -188
- data/core/basic_object.rbs +9 -8
- data/core/class.rbs +6 -5
- data/core/comparable.rbs +45 -31
- data/core/complex.rbs +52 -40
- data/core/dir.rbs +57 -45
- data/core/encoding.rbs +5 -5
- data/core/enumerable.rbs +96 -91
- data/core/enumerator.rbs +4 -3
- data/core/errno.rbs +3 -2
- data/core/errors.rbs +31 -29
- data/core/exception.rbs +12 -12
- data/core/fiber.rbs +36 -36
- data/core/file.rbs +186 -113
- data/core/file_test.rbs +2 -2
- data/core/float.rbs +41 -32
- data/core/gc.rbs +78 -70
- data/core/hash.rbs +70 -60
- data/core/integer.rbs +32 -28
- data/core/io/buffer.rbs +36 -36
- data/core/io/wait.rbs +7 -7
- data/core/io.rbs +120 -135
- data/core/kernel.rbs +189 -139
- data/core/marshal.rbs +3 -3
- data/core/match_data.rbs +14 -12
- data/core/math.rbs +69 -67
- data/core/method.rbs +6 -6
- data/core/module.rbs +146 -85
- data/core/nil_class.rbs +4 -3
- data/core/numeric.rbs +35 -32
- data/core/object.rbs +6 -8
- data/core/object_space.rbs +11 -10
- data/core/pathname.rbs +131 -81
- data/core/proc.rbs +65 -33
- data/core/process.rbs +219 -201
- data/core/ractor.rbs +15 -11
- data/core/random.rbs +4 -3
- data/core/range.rbs +52 -47
- data/core/rational.rbs +5 -5
- data/core/rbs/unnamed/argf.rbs +58 -51
- data/core/rbs/unnamed/env_class.rbs +18 -13
- data/core/rbs/unnamed/main_class.rbs +123 -0
- data/core/rbs/unnamed/random.rbs +7 -5
- data/core/regexp.rbs +236 -197
- data/core/ruby.rbs +1 -1
- data/core/ruby_vm.rbs +32 -30
- data/core/rubygems/config_file.rbs +5 -5
- data/core/rubygems/errors.rbs +1 -1
- data/core/rubygems/requirement.rbs +5 -5
- data/core/rubygems/rubygems.rbs +5 -3
- data/core/set.rbs +17 -16
- data/core/signal.rbs +2 -2
- data/core/string.rbs +311 -292
- data/core/struct.rbs +26 -25
- data/core/symbol.rbs +25 -24
- data/core/thread.rbs +40 -34
- data/core/time.rbs +47 -42
- data/core/trace_point.rbs +34 -31
- data/core/true_class.rbs +2 -2
- data/core/unbound_method.rbs +10 -10
- data/core/warning.rbs +7 -7
- data/docs/collection.md +1 -1
- data/docs/config.md +171 -0
- data/docs/inline.md +110 -4
- data/docs/syntax.md +13 -12
- data/ext/rbs_extension/ast_translation.c +489 -135
- data/ext/rbs_extension/class_constants.c +8 -0
- data/ext/rbs_extension/class_constants.h +4 -0
- data/ext/rbs_extension/legacy_location.c +28 -51
- data/ext/rbs_extension/legacy_location.h +37 -0
- data/ext/rbs_extension/main.c +12 -20
- data/include/rbs/ast.h +423 -195
- data/include/rbs/lexer.h +2 -2
- data/include/rbs/location.h +25 -44
- data/include/rbs/parser.h +2 -2
- data/include/rbs/util/rbs_constant_pool.h +0 -3
- data/include/rbs.h +8 -0
- data/lib/rbs/ast/ruby/annotations.rb +157 -4
- data/lib/rbs/ast/ruby/members.rb +374 -22
- data/lib/rbs/cli/validate.rb +5 -60
- data/lib/rbs/collection/config/lockfile_generator.rb +6 -2
- data/lib/rbs/definition_builder.rb +60 -27
- data/lib/rbs/errors.rb +0 -11
- data/lib/rbs/inline_parser.rb +1 -1
- data/lib/rbs/parser_aux.rb +20 -7
- data/lib/rbs/prototype/helpers.rb +57 -0
- data/lib/rbs/prototype/rb.rb +1 -26
- data/lib/rbs/prototype/rbi.rb +1 -20
- data/lib/rbs/test/type_check.rb +3 -0
- data/lib/rbs/types.rb +62 -52
- data/lib/rbs/unit_test/type_assertions.rb +35 -8
- data/lib/rbs/version.rb +1 -1
- data/lib/rbs.rb +0 -1
- data/rbs.gemspec +1 -1
- 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/sig/ast/ruby/annotations.rbs +191 -4
- data/sig/ast/ruby/members.rbs +21 -1
- data/sig/cli/validate.rbs +1 -6
- data/sig/definition_builder.rbs +2 -0
- data/sig/errors.rbs +0 -8
- data/sig/method_types.rbs +1 -1
- data/sig/parser.rbs +17 -13
- data/sig/prototype/helpers.rbs +2 -0
- data/sig/types.rbs +10 -11
- data/sig/unit_test/spy.rbs +0 -8
- data/sig/unit_test/type_assertions.rbs +11 -0
- data/src/ast.c +339 -161
- data/src/lexstate.c +1 -1
- data/src/location.c +7 -47
- data/src/parser.c +674 -480
- data/src/util/rbs_constant_pool.c +0 -4
- data/stdlib/bigdecimal/0/big_decimal.rbs +16 -16
- data/stdlib/cgi-escape/0/escape.rbs +4 -4
- data/stdlib/coverage/0/coverage.rbs +4 -3
- data/stdlib/date/0/date.rbs +33 -28
- data/stdlib/date/0/date_time.rbs +24 -23
- data/stdlib/did_you_mean/0/did_you_mean.rbs +17 -16
- data/stdlib/erb/0/erb.rbs +64 -53
- data/stdlib/etc/0/etc.rbs +55 -50
- data/stdlib/fileutils/0/fileutils.rbs +138 -125
- data/stdlib/forwardable/0/forwardable.rbs +10 -10
- data/stdlib/io-console/0/io-console.rbs +2 -2
- data/stdlib/json/0/json.rbs +135 -108
- data/stdlib/monitor/0/monitor.rbs +3 -3
- data/stdlib/net-http/0/net-http.rbs +159 -134
- data/stdlib/objspace/0/objspace.rbs +8 -7
- data/stdlib/open-uri/0/open-uri.rbs +8 -8
- data/stdlib/open3/0/open3.rbs +36 -35
- data/stdlib/openssl/0/openssl.rbs +144 -129
- data/stdlib/optparse/0/optparse.rbs +18 -14
- data/stdlib/pathname/0/pathname.rbs +2 -2
- 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 +61 -8
- data/stdlib/psych/0/store.rbs +2 -4
- data/stdlib/pty/0/pty.rbs +9 -6
- data/stdlib/random-formatter/0/random-formatter.rbs +2 -2
- data/stdlib/ripper/0/ripper.rbs +20 -17
- data/stdlib/securerandom/0/securerandom.rbs +1 -1
- data/stdlib/shellwords/0/shellwords.rbs +2 -2
- 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 +10 -9
- data/stdlib/socket/0/tcp_server.rbs +1 -1
- data/stdlib/socket/0/tcp_socket.rbs +1 -1
- data/stdlib/socket/0/udp_socket.rbs +1 -1
- data/stdlib/socket/0/unix_server.rbs +1 -1
- data/stdlib/stringio/0/stringio.rbs +55 -54
- data/stdlib/strscan/0/string_scanner.rbs +46 -44
- data/stdlib/tempfile/0/tempfile.rbs +24 -20
- data/stdlib/time/0/time.rbs +7 -5
- data/stdlib/tsort/0/tsort.rbs +7 -6
- data/stdlib/uri/0/common.rbs +26 -18
- data/stdlib/uri/0/file.rbs +2 -2
- data/stdlib/uri/0/generic.rbs +2 -2
- 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 +6 -5
- data/stdlib/zlib/0/deflate.rbs +4 -3
- data/stdlib/zlib/0/gzip_reader.rbs +4 -4
- 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
- metadata +23 -5
- data/.github/workflows/valgrind.yml +0 -42
data/core/string.rbs
CHANGED
|
@@ -16,8 +16,8 @@
|
|
|
16
16
|
# * Method #String.
|
|
17
17
|
#
|
|
18
18
|
# Some `String` methods modify `self`. Typically, a method whose name ends with
|
|
19
|
-
#
|
|
20
|
-
# (without the
|
|
19
|
+
# <code>!</code> modifies `self` and returns `self`; often, a similarly named
|
|
20
|
+
# method (without the <code>!</code>) returns a new string.
|
|
21
21
|
#
|
|
22
22
|
# In general, if both bang and non-bang versions of a method exist, the bang
|
|
23
23
|
# method mutates and the non-bang method does not. However, a method without a
|
|
@@ -48,7 +48,7 @@
|
|
|
48
48
|
# The examples in this section mostly use the String#sub and String#gsub
|
|
49
49
|
# methods; the principles illustrated apply to all four substitution methods.
|
|
50
50
|
#
|
|
51
|
-
#
|
|
51
|
+
# <strong>Argument `pattern`</strong>
|
|
52
52
|
#
|
|
53
53
|
# Argument `pattern` is commonly a regular expression:
|
|
54
54
|
#
|
|
@@ -65,7 +65,7 @@
|
|
|
65
65
|
#
|
|
66
66
|
# 'THX1138'.gsub('\d+', '00') # => "THX1138"
|
|
67
67
|
#
|
|
68
|
-
#
|
|
68
|
+
# <strong>`String` `replacement`</strong>
|
|
69
69
|
#
|
|
70
70
|
# If `replacement` is a string, that string determines the replacing string that
|
|
71
71
|
# is substituted for the matched text.
|
|
@@ -74,41 +74,48 @@
|
|
|
74
74
|
#
|
|
75
75
|
# `String` `replacement` may contain back-references to the pattern's captures:
|
|
76
76
|
#
|
|
77
|
-
# *
|
|
78
|
-
# *
|
|
77
|
+
# * <code>\n</code> (*n* is a non-negative integer) refers to <code>$n</code>.
|
|
78
|
+
# * <code>\k<name></code> refers to the named capture `name`.
|
|
79
79
|
#
|
|
80
80
|
# See Regexp for details.
|
|
81
81
|
#
|
|
82
82
|
# Note that within the string `replacement`, a character combination such as
|
|
83
|
-
#
|
|
84
|
-
# you may refer to some special match variables using these
|
|
85
|
-
#
|
|
86
|
-
#
|
|
87
|
-
#
|
|
88
|
-
#
|
|
89
|
-
# *
|
|
90
|
-
#
|
|
83
|
+
# <code>$&</code> is treated as ordinary text, not as a special match variable.
|
|
84
|
+
# However, you may refer to some special match variables using these
|
|
85
|
+
# combinations:
|
|
86
|
+
#
|
|
87
|
+
# * <code>\&</code> and <code>\0</code> correspond to <code>$&</code>, which
|
|
88
|
+
# contains the complete matched text.
|
|
89
|
+
# * <code>\'</code> corresponds to <code>$'</code>, which contains the string
|
|
90
|
+
# after the match.
|
|
91
|
+
# * <code>`</code> corresponds to <code>$`</code>, which contains the string
|
|
92
|
+
# before the match.
|
|
93
|
+
# * <code>\+</code> corresponds to <code>$+</code>, which contains the last
|
|
94
|
+
# capture group.
|
|
91
95
|
#
|
|
92
96
|
# See Regexp for details.
|
|
93
97
|
#
|
|
94
|
-
# Note that
|
|
98
|
+
# Note that <code>\\</code> is interpreted as an escape, i.e., a single
|
|
99
|
+
# backslash.
|
|
95
100
|
#
|
|
96
101
|
# Note also that a string literal consumes backslashes. See [String
|
|
97
102
|
# Literals](rdoc-ref:syntax/literals.rdoc@String+Literals) for details about
|
|
98
103
|
# string literals.
|
|
99
104
|
#
|
|
100
105
|
# A back-reference is typically preceded by an additional backslash. For
|
|
101
|
-
# example, if you want to write a back-reference
|
|
102
|
-
# double-quoted string literal, you need to write
|
|
106
|
+
# example, if you want to write a back-reference <code>\&</code> in
|
|
107
|
+
# `replacement` with a double-quoted string literal, you need to write
|
|
108
|
+
# <code>"..\\&.."</code>.
|
|
103
109
|
#
|
|
104
|
-
# If you want to write a non-back-reference string
|
|
105
|
-
# need to first escape the backslash to prevent this method
|
|
106
|
-
# as a back-reference, and then you need to escape the
|
|
107
|
-
# prevent a string literal from consuming them:
|
|
110
|
+
# If you want to write a non-back-reference string <code>\&</code> in
|
|
111
|
+
# `replacement`, you need to first escape the backslash to prevent this method
|
|
112
|
+
# from interpreting it as a back-reference, and then you need to escape the
|
|
113
|
+
# backslashes again to prevent a string literal from consuming them:
|
|
114
|
+
# <code>"..\\\\&.."</code>.
|
|
108
115
|
#
|
|
109
116
|
# You may want to use the block form to avoid excessive backslashes.
|
|
110
117
|
#
|
|
111
|
-
#
|
|
118
|
+
# <strong>Hash `replacement`</strong>
|
|
112
119
|
#
|
|
113
120
|
# If the argument `replacement` is a hash, and `pattern` matches one of its
|
|
114
121
|
# keys, the replacing string is the value for that key:
|
|
@@ -129,21 +136,21 @@
|
|
|
129
136
|
# s = '@'
|
|
130
137
|
# '1234'.gsub(/\d/) { |match| s.succ! } # => "ABCD"
|
|
131
138
|
#
|
|
132
|
-
# Special match variables such as
|
|
133
|
-
# appropriately.
|
|
139
|
+
# Special match variables such as <code>$1</code>, <code>$2</code>,
|
|
140
|
+
# <code>$`</code>, <code>$&</code>, and <code>$'</code> are set appropriately.
|
|
134
141
|
#
|
|
135
142
|
# ## Whitespace in Strings
|
|
136
143
|
#
|
|
137
144
|
# In the class `String`, *whitespace* is defined as a contiguous sequence of
|
|
138
145
|
# characters consisting of any mixture of the following:
|
|
139
146
|
#
|
|
140
|
-
# * NL (null):
|
|
141
|
-
# * HT (horizontal tab):
|
|
142
|
-
# * LF (line feed):
|
|
143
|
-
# * VT (vertical tab):
|
|
144
|
-
# * FF (form feed):
|
|
145
|
-
# * CR (carriage return):
|
|
146
|
-
# * SP (space):
|
|
147
|
+
# * NL (null): <code>"\x00"</code>, <code>"\u0000"</code>.
|
|
148
|
+
# * HT (horizontal tab): <code>"\x09"</code>, <code>"\t"</code>.
|
|
149
|
+
# * LF (line feed): <code>"\x0a"</code>, <code>"\n"</code>.
|
|
150
|
+
# * VT (vertical tab): <code>"\x0b"</code>, <code>"\v"</code>.
|
|
151
|
+
# * FF (form feed): <code>"\x0c"</code>, <code>"\f"</code>.
|
|
152
|
+
# * CR (carriage return): <code>"\x0d"</code>, <code>"\r"</code>.
|
|
153
|
+
# * SP (space): <code>"\x20"</code>, <code>" "</code>.
|
|
147
154
|
#
|
|
148
155
|
# Whitespace is relevant for the following methods:
|
|
149
156
|
#
|
|
@@ -160,15 +167,14 @@
|
|
|
160
167
|
#
|
|
161
168
|
# Here, class `String` provides methods that are useful for:
|
|
162
169
|
#
|
|
163
|
-
# * [Creating a
|
|
164
|
-
# * [Freezing/Unfreezing a
|
|
165
|
-
# * [Querying a
|
|
170
|
+
# * [Creating a String](rdoc-ref:String@Creating+a+String).
|
|
171
|
+
# * [Freezing/Unfreezing a String](rdoc-ref:String@Freezing-2FUnfreezing).
|
|
172
|
+
# * [Querying a String](rdoc-ref:String@Querying).
|
|
166
173
|
# * [Comparing Strings](rdoc-ref:String@Comparing).
|
|
167
|
-
# * [Modifying a
|
|
168
|
-
# * [Converting to a new
|
|
169
|
-
# * [Converting to a
|
|
170
|
-
#
|
|
171
|
-
# * [Iterating over a \String](rdoc-ref:String@Iterating).
|
|
174
|
+
# * [Modifying a String](rdoc-ref:String@Modifying).
|
|
175
|
+
# * [Converting to a new String](rdoc-ref:String@Converting+to+New+String).
|
|
176
|
+
# * [Converting to a non-String](rdoc-ref:String@Converting+to+Non--5CString).
|
|
177
|
+
# * [Iterating over a String](rdoc-ref:String@Iterating).
|
|
172
178
|
#
|
|
173
179
|
# ### Creating a String
|
|
174
180
|
#
|
|
@@ -177,10 +183,10 @@
|
|
|
177
183
|
#
|
|
178
184
|
# ### Freezing/Unfreezing
|
|
179
185
|
#
|
|
180
|
-
# * #+@: Returns a string that is not frozen: `self` if not frozen;
|
|
181
|
-
# otherwise.
|
|
186
|
+
# * #+@: Returns a string that is not frozen: `self` if not frozen;
|
|
187
|
+
# <code>self.dup</code> otherwise.
|
|
182
188
|
# * #-@ (aliased as #dedup): Returns a string that is frozen: `self` if
|
|
183
|
-
# already frozen;
|
|
189
|
+
# already frozen; <code>self.freeze</code> otherwise.
|
|
184
190
|
# * #freeze: Freezes `self` if not already frozen; returns `self`.
|
|
185
191
|
#
|
|
186
192
|
# ### Querying
|
|
@@ -406,7 +412,7 @@
|
|
|
406
412
|
#
|
|
407
413
|
# Each of these methods converts the contents of `self` to a non-`String`.
|
|
408
414
|
#
|
|
409
|
-
#
|
|
415
|
+
# <em>Characters, Bytes, and Clusters</em>
|
|
410
416
|
#
|
|
411
417
|
# * #bytes: Returns an array of the bytes in `self`.
|
|
412
418
|
# * #chars: Returns an array of the characters in `self`.
|
|
@@ -489,8 +495,8 @@
|
|
|
489
495
|
# * Method #String.
|
|
490
496
|
#
|
|
491
497
|
# Some `String` methods modify `self`. Typically, a method whose name ends with
|
|
492
|
-
#
|
|
493
|
-
# (without the
|
|
498
|
+
# <code>!</code> modifies `self` and returns `self`; often, a similarly named
|
|
499
|
+
# method (without the <code>!</code>) returns a new string.
|
|
494
500
|
#
|
|
495
501
|
# In general, if both bang and non-bang versions of a method exist, the bang
|
|
496
502
|
# method mutates and the non-bang method does not. However, a method without a
|
|
@@ -521,7 +527,7 @@
|
|
|
521
527
|
# The examples in this section mostly use the String#sub and String#gsub
|
|
522
528
|
# methods; the principles illustrated apply to all four substitution methods.
|
|
523
529
|
#
|
|
524
|
-
#
|
|
530
|
+
# <strong>Argument `pattern`</strong>
|
|
525
531
|
#
|
|
526
532
|
# Argument `pattern` is commonly a regular expression:
|
|
527
533
|
#
|
|
@@ -538,7 +544,7 @@
|
|
|
538
544
|
#
|
|
539
545
|
# 'THX1138'.gsub('\d+', '00') # => "THX1138"
|
|
540
546
|
#
|
|
541
|
-
#
|
|
547
|
+
# <strong>`String` `replacement`</strong>
|
|
542
548
|
#
|
|
543
549
|
# If `replacement` is a string, that string determines the replacing string that
|
|
544
550
|
# is substituted for the matched text.
|
|
@@ -547,41 +553,48 @@
|
|
|
547
553
|
#
|
|
548
554
|
# `String` `replacement` may contain back-references to the pattern's captures:
|
|
549
555
|
#
|
|
550
|
-
# *
|
|
551
|
-
# *
|
|
556
|
+
# * <code>\n</code> (*n* is a non-negative integer) refers to <code>$n</code>.
|
|
557
|
+
# * <code>\k<name></code> refers to the named capture `name`.
|
|
552
558
|
#
|
|
553
559
|
# See Regexp for details.
|
|
554
560
|
#
|
|
555
561
|
# Note that within the string `replacement`, a character combination such as
|
|
556
|
-
#
|
|
557
|
-
# you may refer to some special match variables using these
|
|
558
|
-
#
|
|
559
|
-
#
|
|
560
|
-
#
|
|
561
|
-
#
|
|
562
|
-
# *
|
|
563
|
-
#
|
|
562
|
+
# <code>$&</code> is treated as ordinary text, not as a special match variable.
|
|
563
|
+
# However, you may refer to some special match variables using these
|
|
564
|
+
# combinations:
|
|
565
|
+
#
|
|
566
|
+
# * <code>\&</code> and <code>\0</code> correspond to <code>$&</code>, which
|
|
567
|
+
# contains the complete matched text.
|
|
568
|
+
# * <code>\'</code> corresponds to <code>$'</code>, which contains the string
|
|
569
|
+
# after the match.
|
|
570
|
+
# * <code>`</code> corresponds to <code>$`</code>, which contains the string
|
|
571
|
+
# before the match.
|
|
572
|
+
# * <code>\+</code> corresponds to <code>$+</code>, which contains the last
|
|
573
|
+
# capture group.
|
|
564
574
|
#
|
|
565
575
|
# See Regexp for details.
|
|
566
576
|
#
|
|
567
|
-
# Note that
|
|
577
|
+
# Note that <code>\\</code> is interpreted as an escape, i.e., a single
|
|
578
|
+
# backslash.
|
|
568
579
|
#
|
|
569
580
|
# Note also that a string literal consumes backslashes. See [String
|
|
570
581
|
# Literals](rdoc-ref:syntax/literals.rdoc@String+Literals) for details about
|
|
571
582
|
# string literals.
|
|
572
583
|
#
|
|
573
584
|
# A back-reference is typically preceded by an additional backslash. For
|
|
574
|
-
# example, if you want to write a back-reference
|
|
575
|
-
# double-quoted string literal, you need to write
|
|
585
|
+
# example, if you want to write a back-reference <code>\&</code> in
|
|
586
|
+
# `replacement` with a double-quoted string literal, you need to write
|
|
587
|
+
# <code>"..\\&.."</code>.
|
|
576
588
|
#
|
|
577
|
-
# If you want to write a non-back-reference string
|
|
578
|
-
# need to first escape the backslash to prevent this method
|
|
579
|
-
# as a back-reference, and then you need to escape the
|
|
580
|
-
# prevent a string literal from consuming them:
|
|
589
|
+
# If you want to write a non-back-reference string <code>\&</code> in
|
|
590
|
+
# `replacement`, you need to first escape the backslash to prevent this method
|
|
591
|
+
# from interpreting it as a back-reference, and then you need to escape the
|
|
592
|
+
# backslashes again to prevent a string literal from consuming them:
|
|
593
|
+
# <code>"..\\\\&.."</code>.
|
|
581
594
|
#
|
|
582
595
|
# You may want to use the block form to avoid excessive backslashes.
|
|
583
596
|
#
|
|
584
|
-
#
|
|
597
|
+
# <strong>Hash `replacement`</strong>
|
|
585
598
|
#
|
|
586
599
|
# If the argument `replacement` is a hash, and `pattern` matches one of its
|
|
587
600
|
# keys, the replacing string is the value for that key:
|
|
@@ -602,21 +615,21 @@
|
|
|
602
615
|
# s = '@'
|
|
603
616
|
# '1234'.gsub(/\d/) { |match| s.succ! } # => "ABCD"
|
|
604
617
|
#
|
|
605
|
-
# Special match variables such as
|
|
606
|
-
# appropriately.
|
|
618
|
+
# Special match variables such as <code>$1</code>, <code>$2</code>,
|
|
619
|
+
# <code>$`</code>, <code>$&</code>, and <code>$'</code> are set appropriately.
|
|
607
620
|
#
|
|
608
621
|
# ## Whitespace in Strings
|
|
609
622
|
#
|
|
610
623
|
# In the class `String`, *whitespace* is defined as a contiguous sequence of
|
|
611
624
|
# characters consisting of any mixture of the following:
|
|
612
625
|
#
|
|
613
|
-
# * NL (null):
|
|
614
|
-
# * HT (horizontal tab):
|
|
615
|
-
# * LF (line feed):
|
|
616
|
-
# * VT (vertical tab):
|
|
617
|
-
# * FF (form feed):
|
|
618
|
-
# * CR (carriage return):
|
|
619
|
-
# * SP (space):
|
|
626
|
+
# * NL (null): <code>"\x00"</code>, <code>"\u0000"</code>.
|
|
627
|
+
# * HT (horizontal tab): <code>"\x09"</code>, <code>"\t"</code>.
|
|
628
|
+
# * LF (line feed): <code>"\x0a"</code>, <code>"\n"</code>.
|
|
629
|
+
# * VT (vertical tab): <code>"\x0b"</code>, <code>"\v"</code>.
|
|
630
|
+
# * FF (form feed): <code>"\x0c"</code>, <code>"\f"</code>.
|
|
631
|
+
# * CR (carriage return): <code>"\x0d"</code>, <code>"\r"</code>.
|
|
632
|
+
# * SP (space): <code>"\x20"</code>, <code>" "</code>.
|
|
620
633
|
#
|
|
621
634
|
# Whitespace is relevant for the following methods:
|
|
622
635
|
#
|
|
@@ -633,15 +646,14 @@
|
|
|
633
646
|
#
|
|
634
647
|
# Here, class `String` provides methods that are useful for:
|
|
635
648
|
#
|
|
636
|
-
# * [Creating a
|
|
637
|
-
# * [Freezing/Unfreezing a
|
|
638
|
-
# * [Querying a
|
|
649
|
+
# * [Creating a String](rdoc-ref:String@Creating+a+String).
|
|
650
|
+
# * [Freezing/Unfreezing a String](rdoc-ref:String@Freezing-2FUnfreezing).
|
|
651
|
+
# * [Querying a String](rdoc-ref:String@Querying).
|
|
639
652
|
# * [Comparing Strings](rdoc-ref:String@Comparing).
|
|
640
|
-
# * [Modifying a
|
|
641
|
-
# * [Converting to a new
|
|
642
|
-
# * [Converting to a
|
|
643
|
-
#
|
|
644
|
-
# * [Iterating over a \String](rdoc-ref:String@Iterating).
|
|
653
|
+
# * [Modifying a String](rdoc-ref:String@Modifying).
|
|
654
|
+
# * [Converting to a new String](rdoc-ref:String@Converting+to+New+String).
|
|
655
|
+
# * [Converting to a non-String](rdoc-ref:String@Converting+to+Non--5CString).
|
|
656
|
+
# * [Iterating over a String](rdoc-ref:String@Iterating).
|
|
645
657
|
#
|
|
646
658
|
# ### Creating a String
|
|
647
659
|
#
|
|
@@ -650,10 +662,10 @@
|
|
|
650
662
|
#
|
|
651
663
|
# ### Freezing/Unfreezing
|
|
652
664
|
#
|
|
653
|
-
# * #+@: Returns a string that is not frozen: `self` if not frozen;
|
|
654
|
-
# otherwise.
|
|
665
|
+
# * #+@: Returns a string that is not frozen: `self` if not frozen;
|
|
666
|
+
# <code>self.dup</code> otherwise.
|
|
655
667
|
# * #-@ (aliased as #dedup): Returns a string that is frozen: `self` if
|
|
656
|
-
# already frozen;
|
|
668
|
+
# already frozen; <code>self.freeze</code> otherwise.
|
|
657
669
|
# * #freeze: Freezes `self` if not already frozen; returns `self`.
|
|
658
670
|
#
|
|
659
671
|
# ### Querying
|
|
@@ -879,7 +891,7 @@
|
|
|
879
891
|
#
|
|
880
892
|
# Each of these methods converts the contents of `self` to a non-`String`.
|
|
881
893
|
#
|
|
882
|
-
#
|
|
894
|
+
# <em>Characters, Bytes, and Clusters</em>
|
|
883
895
|
#
|
|
884
896
|
# * #bytes: Returns an array of the bytes in `self`.
|
|
885
897
|
# * #chars: Returns an array of the characters in `self`.
|
|
@@ -958,12 +970,12 @@ class String
|
|
|
958
970
|
#
|
|
959
971
|
# If `object` is already a string, returns `object`, unmodified.
|
|
960
972
|
#
|
|
961
|
-
# Otherwise if `object` responds to
|
|
962
|
-
# the result.
|
|
973
|
+
# Otherwise if `object` responds to <code>:to_str</code>, calls
|
|
974
|
+
# <code>object.to_str</code> and returns the result.
|
|
963
975
|
#
|
|
964
|
-
# Returns `nil` if `object` does not respond to
|
|
976
|
+
# Returns `nil` if `object` does not respond to <code>:to_str</code>.
|
|
965
977
|
#
|
|
966
|
-
# Raises an exception unless
|
|
978
|
+
# Raises an exception unless <code>object.to_str</code> returns a string.
|
|
967
979
|
#
|
|
968
980
|
def self.try_convert: (String object) -> String # technically will return `object` unchanged.
|
|
969
981
|
| (_ToStr object) -> String
|
|
@@ -978,7 +990,7 @@ class String
|
|
|
978
990
|
# The `options` are optional keyword options (see below).
|
|
979
991
|
#
|
|
980
992
|
# With no argument given and keyword `encoding` also not given, returns an empty
|
|
981
|
-
# string with the Encoding
|
|
993
|
+
# string with the Encoding <code>ASCII-8BIT</code>:
|
|
982
994
|
#
|
|
983
995
|
# s = String.new # => ""
|
|
984
996
|
# s.encoding # => #<Encoding:ASCII-8BIT>
|
|
@@ -991,9 +1003,10 @@ class String
|
|
|
991
1003
|
# s1.encoding # => #<Encoding:UTF-16 (dummy)>
|
|
992
1004
|
#
|
|
993
1005
|
# (Unlike String.new, a [string
|
|
994
|
-
# literal](rdoc-ref:syntax/literals.rdoc@String+Literals) like
|
|
995
|
-
#
|
|
996
|
-
#
|
|
1006
|
+
# literal](rdoc-ref:syntax/literals.rdoc@String+Literals) like <code>''</code>
|
|
1007
|
+
# or a [here document
|
|
1008
|
+
# literal](rdoc-ref:syntax/literals.rdoc@Here+Document+Literals) always has
|
|
1009
|
+
# [script encoding](rdoc-ref:encodings.rdoc@Script+Encoding).)
|
|
997
1010
|
#
|
|
998
1011
|
# With keyword option `encoding` given, returns a string with the specified
|
|
999
1012
|
# encoding; the `encoding` may be an Encoding object, an encoding name, or an
|
|
@@ -1094,7 +1107,7 @@ class String
|
|
|
1094
1107
|
# Returns `self` if `self` is not frozen and can be mutated without warning
|
|
1095
1108
|
# issuance.
|
|
1096
1109
|
#
|
|
1097
|
-
# Otherwise returns
|
|
1110
|
+
# Otherwise returns <code>self.dup</code>, which is not frozen.
|
|
1098
1111
|
#
|
|
1099
1112
|
# Related: see [Freezing/Unfreezing](rdoc-ref:String@Freezing-2FUnfreezing).
|
|
1100
1113
|
#
|
|
@@ -1154,8 +1167,9 @@ class String
|
|
|
1154
1167
|
# s = 'foo'
|
|
1155
1168
|
# s << 33 # => "foo!"
|
|
1156
1169
|
#
|
|
1157
|
-
# Additionally, if the codepoint is in range
|
|
1158
|
-
# `self` is Encoding::US_ASCII, changes the encoding to
|
|
1170
|
+
# Additionally, if the codepoint is in range <code>0..0xff</code> and the
|
|
1171
|
+
# encoding of `self` is Encoding::US_ASCII, changes the encoding to
|
|
1172
|
+
# Encoding::ASCII_8BIT:
|
|
1159
1173
|
#
|
|
1160
1174
|
# s = 'foo'.encode(Encoding::US_ASCII)
|
|
1161
1175
|
# s.encoding # => #<Encoding:US-ASCII>
|
|
@@ -1183,7 +1197,7 @@ class String
|
|
|
1183
1197
|
#
|
|
1184
1198
|
# Returns:
|
|
1185
1199
|
#
|
|
1186
|
-
# *
|
|
1200
|
+
# * <code>-1</code>, if `self` is smaller.
|
|
1187
1201
|
# * `0`, if the two are equal.
|
|
1188
1202
|
# * `1`, if `self` is larger.
|
|
1189
1203
|
# * `nil`, if the two are incomparable.
|
|
@@ -1225,8 +1239,8 @@ class String
|
|
|
1225
1239
|
#
|
|
1226
1240
|
# When `object` is not a string:
|
|
1227
1241
|
#
|
|
1228
|
-
# * If `object` responds to method `to_str`,
|
|
1229
|
-
# its return value is returned.
|
|
1242
|
+
# * If `object` responds to method `to_str`, <code>object == self</code> is
|
|
1243
|
+
# called and its return value is returned.
|
|
1230
1244
|
# * If `object` does not respond to `to_str`, `false` is returned.
|
|
1231
1245
|
#
|
|
1232
1246
|
# Related: [Comparing](rdoc-ref:String@Comparing).
|
|
@@ -1250,8 +1264,8 @@ class String
|
|
|
1250
1264
|
#
|
|
1251
1265
|
# When `object` is not a string:
|
|
1252
1266
|
#
|
|
1253
|
-
# * If `object` responds to method `to_str`,
|
|
1254
|
-
# its return value is returned.
|
|
1267
|
+
# * If `object` responds to method `to_str`, <code>object == self</code> is
|
|
1268
|
+
# called and its return value is returned.
|
|
1255
1269
|
# * If `object` does not respond to `to_str`, `false` is returned.
|
|
1256
1270
|
#
|
|
1257
1271
|
# Related: [Comparing](rdoc-ref:String@Comparing).
|
|
@@ -1273,8 +1287,8 @@ class String
|
|
|
1273
1287
|
# 'foo' =~ /x/ # => nil
|
|
1274
1288
|
# $~ # => nil
|
|
1275
1289
|
#
|
|
1276
|
-
# Note that
|
|
1277
|
-
# Regexp#=~):
|
|
1290
|
+
# Note that <code>string =~ regexp</code> is different from <code>regexp =~
|
|
1291
|
+
# string</code> (see Regexp#=~):
|
|
1278
1292
|
#
|
|
1279
1293
|
# number = nil
|
|
1280
1294
|
# 'no. 9' =~ /(?<number>\d+)/ # => 4
|
|
@@ -1282,7 +1296,8 @@ class String
|
|
|
1282
1296
|
# /(?<number>\d+)/ =~ 'no. 9' # => 4
|
|
1283
1297
|
# number # => "9" # Assigned.
|
|
1284
1298
|
#
|
|
1285
|
-
# If `object` is not a Regexp, returns the value returned by
|
|
1299
|
+
# If `object` is not a Regexp, returns the value returned by <code>object =~
|
|
1300
|
+
# self</code>.
|
|
1286
1301
|
#
|
|
1287
1302
|
# Related: see [Querying](rdoc-ref:String@Querying).
|
|
1288
1303
|
#
|
|
@@ -1303,7 +1318,7 @@ class String
|
|
|
1303
1318
|
# -->
|
|
1304
1319
|
# Returns the substring of `self` specified by the arguments.
|
|
1305
1320
|
#
|
|
1306
|
-
#
|
|
1321
|
+
# <strong>Form <code>self[index]</code></strong>
|
|
1307
1322
|
#
|
|
1308
1323
|
# With non-negative integer argument `index` given, returns the 1-character
|
|
1309
1324
|
# substring found in self at character offset index:
|
|
@@ -1321,7 +1336,7 @@ class String
|
|
|
1321
1336
|
# 'hello'[-5] # => "h"
|
|
1322
1337
|
# 'hello'[-6] # => nil
|
|
1323
1338
|
#
|
|
1324
|
-
#
|
|
1339
|
+
# <strong>Form <code>self[start, length]</code></strong>
|
|
1325
1340
|
#
|
|
1326
1341
|
# With integer arguments `start` and `length` given, returns a substring of size
|
|
1327
1342
|
# `length` characters (as available) beginning at character offset specified by
|
|
@@ -1348,10 +1363,10 @@ class String
|
|
|
1348
1363
|
#
|
|
1349
1364
|
# 'hello'[5, 3] # => ""
|
|
1350
1365
|
#
|
|
1351
|
-
#
|
|
1366
|
+
# <strong>Form <code>self[range]</code></strong>
|
|
1352
1367
|
#
|
|
1353
|
-
# With Range argument `range` given, forms substring
|
|
1354
|
-
# range.size]
|
|
1368
|
+
# With Range argument `range` given, forms substring <code>self[range.start,
|
|
1369
|
+
# range.size]</code>:
|
|
1355
1370
|
#
|
|
1356
1371
|
# 'hello'[0..2] # => "hel"
|
|
1357
1372
|
# 'hello'[0, 3] # => "hel"
|
|
@@ -1362,7 +1377,7 @@ class String
|
|
|
1362
1377
|
# 'hello'[0, 0] # => ""
|
|
1363
1378
|
# 'hello'[0...0] # => ""
|
|
1364
1379
|
#
|
|
1365
|
-
#
|
|
1380
|
+
# <strong>Form <code>self[regexp, capture = 0]</code></strong>
|
|
1366
1381
|
#
|
|
1367
1382
|
# With Regexp argument `regexp` given and `capture` as zero, searches for a
|
|
1368
1383
|
# matching substring in `self`; updates [Regexp-related global
|
|
@@ -1384,7 +1399,7 @@ class String
|
|
|
1384
1399
|
# 'hello'[/(h)(e)(l+)(o)/, 4] # => "o"
|
|
1385
1400
|
# 'hello'[/(h)(e)(l+)(o)/, 5] # => nil
|
|
1386
1401
|
#
|
|
1387
|
-
#
|
|
1402
|
+
# <strong>Form <code>self[substring]</code></strong>
|
|
1388
1403
|
#
|
|
1389
1404
|
# With string argument `substring` given, returns the matching substring of
|
|
1390
1405
|
# `self`, if found:
|
|
@@ -1414,7 +1429,7 @@ class String
|
|
|
1414
1429
|
# Returns `self` with all, a substring, or none of its contents replaced;
|
|
1415
1430
|
# returns the argument `other_string`.
|
|
1416
1431
|
#
|
|
1417
|
-
#
|
|
1432
|
+
# <strong>Form <code>self[index] = other_string</code></strong>
|
|
1418
1433
|
#
|
|
1419
1434
|
# With non-negative integer argument `index` given, searches for the 1-character
|
|
1420
1435
|
# substring found in self at character offset index:
|
|
@@ -1448,7 +1463,7 @@ class String
|
|
|
1448
1463
|
# s = 'hello'
|
|
1449
1464
|
# s[-6] = 'foo' # Raises IndexError: index -6 out of string.
|
|
1450
1465
|
#
|
|
1451
|
-
#
|
|
1466
|
+
# <strong>Form <code>self[start, length] = other_string</code></strong>
|
|
1452
1467
|
#
|
|
1453
1468
|
# With integer arguments `start` and `length` given, searches for a substring of
|
|
1454
1469
|
# size `length` characters (as available) beginning at character offset
|
|
@@ -1503,10 +1518,10 @@ class String
|
|
|
1503
1518
|
# s[5, 3] = 'foo' # => "foo"
|
|
1504
1519
|
# s # => "hellofoo"
|
|
1505
1520
|
#
|
|
1506
|
-
#
|
|
1521
|
+
# <strong>Form <code>self[range] = other_string</code></strong>
|
|
1507
1522
|
#
|
|
1508
|
-
# With Range argument `range` given, equivalent to
|
|
1509
|
-
# range.size] = other_string
|
|
1523
|
+
# With Range argument `range` given, equivalent to <code>self[range.start,
|
|
1524
|
+
# range.size] = other_string</code>:
|
|
1510
1525
|
#
|
|
1511
1526
|
# s0 = 'hello'
|
|
1512
1527
|
# s1 = 'hello'
|
|
@@ -1526,7 +1541,7 @@ class String
|
|
|
1526
1541
|
# s = 'hello'
|
|
1527
1542
|
# s[9..10] = 'foo' # Raises RangeError: 9..10 out of range
|
|
1528
1543
|
#
|
|
1529
|
-
#
|
|
1544
|
+
# <strong>Form <code>self[regexp, capture = 0] = other_string</code></strong>
|
|
1530
1545
|
#
|
|
1531
1546
|
# With Regexp argument `regexp` given and `capture` as zero, searches for a
|
|
1532
1547
|
# matching substring in `self`; updates [Regexp-related global
|
|
@@ -1569,7 +1584,7 @@ class String
|
|
|
1569
1584
|
# s = 'hello'
|
|
1570
1585
|
# s[/nosuch/] = 'foo' # Raises IndexError: regexp not matched.
|
|
1571
1586
|
#
|
|
1572
|
-
#
|
|
1587
|
+
# <strong>Form <code>self[substring] = other_string</code></strong>
|
|
1573
1588
|
#
|
|
1574
1589
|
# With string argument `substring` given:
|
|
1575
1590
|
#
|
|
@@ -1623,7 +1638,7 @@ class String
|
|
|
1623
1638
|
#
|
|
1624
1639
|
# Related: see [Modifying](rdoc-ref:String@Modifying).
|
|
1625
1640
|
#
|
|
1626
|
-
def append_as_bytes: (String) -> String
|
|
1641
|
+
def append_as_bytes: (*String | Integer) -> String
|
|
1627
1642
|
|
|
1628
1643
|
# <!--
|
|
1629
1644
|
# rdoc-file=string.c
|
|
@@ -1884,8 +1899,8 @@ class String
|
|
|
1884
1899
|
# s.byteslice(-4) # => "6"
|
|
1885
1900
|
# s.byteslice(-4, 3) # => "678"
|
|
1886
1901
|
#
|
|
1887
|
-
# With Range argument `range` given, returns
|
|
1888
|
-
# range.size)
|
|
1902
|
+
# With Range argument `range` given, returns <code>byteslice(range.begin,
|
|
1903
|
+
# range.size)</code>:
|
|
1889
1904
|
#
|
|
1890
1905
|
# s = '0123456789' # => "0123456789"
|
|
1891
1906
|
# s.byteslice(4..6) # => "456"
|
|
@@ -2019,8 +2034,9 @@ class String
|
|
|
2019
2034
|
# s = 'こんにちは'
|
|
2020
2035
|
# s.capitalize == s # => true
|
|
2021
2036
|
#
|
|
2022
|
-
# The casing is affected by the given `mapping`, which may be
|
|
2023
|
-
# or
|
|
2037
|
+
# The casing is affected by the given `mapping`, which may be
|
|
2038
|
+
# <code>:ascii</code>, <code>:fold</code>, or <code>:turkic</code>; see [Case
|
|
2039
|
+
# Mappings](rdoc-ref:case_mapping.rdoc@Case+Mappings).
|
|
2024
2040
|
#
|
|
2025
2041
|
# Related: see [Converting to New
|
|
2026
2042
|
# String](rdoc-ref:String@Converting+to+New+String).
|
|
@@ -2052,9 +2068,11 @@ class String
|
|
|
2052
2068
|
# -->
|
|
2053
2069
|
# Ignoring case, compares `self` and `other_string`; returns:
|
|
2054
2070
|
#
|
|
2055
|
-
# * -1 if
|
|
2071
|
+
# * -1 if <code>self.downcase</code> is smaller than
|
|
2072
|
+
# <code>other_string.downcase</code>.
|
|
2056
2073
|
# * 0 if the two are equal.
|
|
2057
|
-
# * 1 if
|
|
2074
|
+
# * 1 if <code>self.downcase</code> is larger than
|
|
2075
|
+
# <code>other_string.downcase</code>.
|
|
2058
2076
|
# * `nil` if the two are incomparable.
|
|
2059
2077
|
#
|
|
2060
2078
|
# See [Case Mapping](rdoc-ref:case_mapping.rdoc).
|
|
@@ -2151,8 +2169,9 @@ class String
|
|
|
2151
2169
|
# Returns a new string copied from `self`, with trailing characters possibly
|
|
2152
2170
|
# removed:
|
|
2153
2171
|
#
|
|
2154
|
-
# When `line_sep` is
|
|
2155
|
-
#
|
|
2172
|
+
# When `line_sep` is <code>"\n"</code>, removes the last one or two characters
|
|
2173
|
+
# if they are <code>"\r"</code>, <code>"\n"</code>, or <code>"\r\n"</code> (but
|
|
2174
|
+
# not <code>"\n\r"</code>):
|
|
2156
2175
|
#
|
|
2157
2176
|
# $/ # => "\n"
|
|
2158
2177
|
# "abc\r".chomp # => "abc"
|
|
@@ -2162,8 +2181,9 @@ class String
|
|
|
2162
2181
|
# "тест\r\n".chomp # => "тест"
|
|
2163
2182
|
# "こんにちは\r\n".chomp # => "こんにちは"
|
|
2164
2183
|
#
|
|
2165
|
-
# When `line_sep` is
|
|
2166
|
-
# occurrences of
|
|
2184
|
+
# When `line_sep` is <code>''</code> (an empty string), removes multiple
|
|
2185
|
+
# trailing occurrences of <code>"\n"</code> or <code>"\r\n"</code> (but not
|
|
2186
|
+
# <code>"\r"</code> or <code>"\n\r"</code>):
|
|
2167
2187
|
#
|
|
2168
2188
|
# "abc\n\n\n".chomp('') # => "abc"
|
|
2169
2189
|
# "abc\r\n\r\n\r\n".chomp('') # => "abc"
|
|
@@ -2171,8 +2191,8 @@ class String
|
|
|
2171
2191
|
# "abc\n\r\n\r\n\r".chomp('') # => "abc\n\r\n\r\n\r"
|
|
2172
2192
|
# "abc\r\r\r".chomp('') # => "abc\r\r\r"
|
|
2173
2193
|
#
|
|
2174
|
-
# When `line_sep` is neither
|
|
2175
|
-
# separator if there is one:
|
|
2194
|
+
# When `line_sep` is neither <code>"\n"</code> nor <code>''</code>, removes a
|
|
2195
|
+
# single trailing line separator if there is one:
|
|
2176
2196
|
#
|
|
2177
2197
|
# 'abcd'.chomp('cd') # => "ab"
|
|
2178
2198
|
# 'abcdcd'.chomp('cd') # => "abcd"
|
|
@@ -2205,7 +2225,7 @@ class String
|
|
|
2205
2225
|
# Returns a new string copied from `self`, with trailing characters possibly
|
|
2206
2226
|
# removed.
|
|
2207
2227
|
#
|
|
2208
|
-
# Removes
|
|
2228
|
+
# Removes <code>"\r\n"</code> if those are the last two characters.
|
|
2209
2229
|
#
|
|
2210
2230
|
# "abc\r\n".chop # => "abc"
|
|
2211
2231
|
# "тест\r\n".chop # => "тест"
|
|
@@ -2356,20 +2376,20 @@ class String
|
|
|
2356
2376
|
#
|
|
2357
2377
|
# In a character selector, three characters get special treatment:
|
|
2358
2378
|
#
|
|
2359
|
-
# * A caret (
|
|
2360
|
-
# following characters:
|
|
2379
|
+
# * A caret (<code>'^'</code>) functions as a *negation* operator for the
|
|
2380
|
+
# immediately following characters:
|
|
2361
2381
|
#
|
|
2362
2382
|
# s = 'abracadabra'
|
|
2363
2383
|
# s.count('^bc') # => 8 # Count of all except 'b' and 'c'.
|
|
2364
2384
|
#
|
|
2365
|
-
# * A hyphen (
|
|
2366
|
-
# characters:
|
|
2385
|
+
# * A hyphen (<code>'-'</code>) between two other characters defines a *range*
|
|
2386
|
+
# of characters:
|
|
2367
2387
|
#
|
|
2368
2388
|
# s = 'abracadabra'
|
|
2369
2389
|
# s.count('a-c') # => 8 # Count of all 'a', 'b', and 'c'.
|
|
2370
2390
|
#
|
|
2371
|
-
# * A backslash (
|
|
2372
|
-
# backslash:
|
|
2391
|
+
# * A backslash (<code>'\'</code>) acts as an escape for a caret, a hyphen, or
|
|
2392
|
+
# another backslash:
|
|
2373
2393
|
#
|
|
2374
2394
|
# s = 'abracadabra'
|
|
2375
2395
|
# s.count('\^bc') # => 3 # Count of '^', 'b', and 'c'.
|
|
@@ -2399,24 +2419,24 @@ class String
|
|
|
2399
2419
|
# rdoc-file=string.c
|
|
2400
2420
|
# - crypt(salt_str) -> new_string
|
|
2401
2421
|
# -->
|
|
2402
|
-
# Returns the string generated by calling
|
|
2403
|
-
# with `str` and `salt_str`, in this order, as its arguments. Please
|
|
2404
|
-
# this method any longer. It is legacy; provided only for backward
|
|
2422
|
+
# Returns the string generated by calling <code>crypt(3)</code> standard library
|
|
2423
|
+
# function with `str` and `salt_str`, in this order, as its arguments. Please
|
|
2424
|
+
# do not use this method any longer. It is legacy; provided only for backward
|
|
2405
2425
|
# compatibility with ruby scripts in earlier days. It is bad to use in
|
|
2406
2426
|
# contemporary programs for several reasons:
|
|
2407
2427
|
#
|
|
2408
|
-
# * Behaviour of C's
|
|
2409
|
-
# string lacks data portability.
|
|
2428
|
+
# * Behaviour of C's <code>crypt(3)</code> depends on the OS it is run. The
|
|
2429
|
+
# generated string lacks data portability.
|
|
2410
2430
|
#
|
|
2411
|
-
# * On some OSes such as Mac OS,
|
|
2412
|
-
# in unexpected results).
|
|
2431
|
+
# * On some OSes such as Mac OS, <code>crypt(3)</code> never fails (i.e.
|
|
2432
|
+
# silently ends up in unexpected results).
|
|
2413
2433
|
#
|
|
2414
|
-
# * On some OSes such as Mac OS,
|
|
2434
|
+
# * On some OSes such as Mac OS, <code>crypt(3)</code> is not thread safe.
|
|
2415
2435
|
#
|
|
2416
|
-
# * So-called "traditional" usage of
|
|
2417
|
-
# According to its manpage, Linux's traditional
|
|
2418
|
-
# 2**56 variations; too easy to brute force today. And this
|
|
2419
|
-
# behaviour.
|
|
2436
|
+
# * So-called "traditional" usage of <code>crypt(3)</code> is very very very
|
|
2437
|
+
# weak. According to its manpage, Linux's traditional <code>crypt(3)</code>
|
|
2438
|
+
# output has only 2**56 variations; too easy to brute force today. And this
|
|
2439
|
+
# is the default behaviour.
|
|
2420
2440
|
#
|
|
2421
2441
|
# * In order to make things robust some OSes implement so-called "modular"
|
|
2422
2442
|
# usage. To go through, you have to do a complex build-up of the `salt_str`
|
|
@@ -2431,21 +2451,22 @@ class String
|
|
|
2431
2451
|
# "foo".crypt("$5$round=1000$salt$") # Typo not detected
|
|
2432
2452
|
#
|
|
2433
2453
|
# * Even in the "modular" mode, some hash functions are considered archaic and
|
|
2434
|
-
# no longer recommended at all; for instance module
|
|
2435
|
-
# abandoned by its author: see
|
|
2436
|
-
# For another instance module
|
|
2437
|
-
# manpage of
|
|
2454
|
+
# no longer recommended at all; for instance module <code>$1$</code> is
|
|
2455
|
+
# officially abandoned by its author: see
|
|
2456
|
+
# http://phk.freebsd.dk/sagas/md5crypt_eol/ . For another instance module
|
|
2457
|
+
# <code>$3$</code> is considered completely broken: see the manpage of
|
|
2458
|
+
# FreeBSD.
|
|
2438
2459
|
#
|
|
2439
2460
|
# * On some OS such as Mac OS, there is no modular mode. Yet, as written
|
|
2440
|
-
# above,
|
|
2441
|
-
# proper salt string it generates a traditional DES hash anyways,
|
|
2442
|
-
# is no way for you to be aware of.
|
|
2461
|
+
# above, <code>crypt(3)</code> on Mac OS never fails. This means even if you
|
|
2462
|
+
# build up a proper salt string it generates a traditional DES hash anyways,
|
|
2463
|
+
# and there is no way for you to be aware of.
|
|
2443
2464
|
#
|
|
2444
2465
|
# "foo".crypt("$5$rounds=1000$salt$") # => "$5fNPQMxC5j6."
|
|
2445
2466
|
#
|
|
2446
2467
|
# If for some reason you cannot migrate to other secure contemporary password
|
|
2447
|
-
# hashing algorithms, install the string-crypt gem and
|
|
2448
|
-
# to continue using it.
|
|
2468
|
+
# hashing algorithms, install the string-crypt gem and <code>require
|
|
2469
|
+
# 'string/crypt'</code> to continue using it.
|
|
2449
2470
|
#
|
|
2450
2471
|
%a{deprecated}
|
|
2451
2472
|
def crypt: (string salt_str) -> String
|
|
@@ -2532,20 +2553,20 @@ class String
|
|
|
2532
2553
|
#
|
|
2533
2554
|
# In a character selector, three characters get special treatment:
|
|
2534
2555
|
#
|
|
2535
|
-
# * A caret (
|
|
2536
|
-
# following characters:
|
|
2556
|
+
# * A caret (<code>'^'</code>) functions as a *negation* operator for the
|
|
2557
|
+
# immediately following characters:
|
|
2537
2558
|
#
|
|
2538
2559
|
# s = 'abracadabra'
|
|
2539
2560
|
# s.delete('^bc') # => "bcb" # Deletes all except 'b' and 'c'.
|
|
2540
2561
|
#
|
|
2541
|
-
# * A hyphen (
|
|
2542
|
-
# characters:
|
|
2562
|
+
# * A hyphen (<code>'-'</code>) between two other characters defines a *range*
|
|
2563
|
+
# of characters:
|
|
2543
2564
|
#
|
|
2544
2565
|
# s = 'abracadabra'
|
|
2545
2566
|
# s.delete('a-c') # => "rdr" # Deletes all 'a', 'b', and 'c'.
|
|
2546
2567
|
#
|
|
2547
|
-
# * A backslash (
|
|
2548
|
-
# backslash:
|
|
2568
|
+
# * A backslash (<code>'\'</code>) acts as an escape for a caret, a hyphen, or
|
|
2569
|
+
# another backslash:
|
|
2549
2570
|
#
|
|
2550
2571
|
# s = 'abracadabra'
|
|
2551
2572
|
# s.delete('\^bc') # => "araadara" # Deletes all '^', 'b', and 'c'.
|
|
@@ -2661,8 +2682,9 @@ class String
|
|
|
2661
2682
|
# s = 'こんにちは'
|
|
2662
2683
|
# s.downcase == s # => true
|
|
2663
2684
|
#
|
|
2664
|
-
# The casing is affected by the given `mapping`, which may be
|
|
2665
|
-
# or
|
|
2685
|
+
# The casing is affected by the given `mapping`, which may be
|
|
2686
|
+
# <code>:ascii</code>, <code>:fold</code>, or <code>:turkic</code>; see [Case
|
|
2687
|
+
# Mappings](rdoc-ref:case_mapping.rdoc@Case+Mappings).
|
|
2666
2688
|
#
|
|
2667
2689
|
# Related: see [Converting to New
|
|
2668
2690
|
# String](rdoc-ref:String@Converting+to+New+String).
|
|
@@ -2723,7 +2745,7 @@ class String
|
|
|
2723
2745
|
# print "Undumped: ", s.dump.undump, "\n"
|
|
2724
2746
|
# end
|
|
2725
2747
|
#
|
|
2726
|
-
# So that for string
|
|
2748
|
+
# So that for string <code>'hello'</code>, we'll see:
|
|
2727
2749
|
#
|
|
2728
2750
|
# String: hello
|
|
2729
2751
|
# Dumped: "hello"
|
|
@@ -2774,10 +2796,11 @@ class String
|
|
|
2774
2796
|
# Undumped: こんにちは
|
|
2775
2797
|
#
|
|
2776
2798
|
# If the encoding of `self` is not ASCII-compatible (i.e., if
|
|
2777
|
-
#
|
|
2778
|
-
# is dumped as an ASCII character, and all other bytes are
|
|
2779
|
-
# hexadecimal; also appends
|
|
2780
|
-
#
|
|
2799
|
+
# <code>self.encoding.ascii_compatible?</code> returns `false`), each
|
|
2800
|
+
# ASCII-compatible byte is dumped as an ASCII character, and all other bytes are
|
|
2801
|
+
# dumped as hexadecimal; also appends
|
|
2802
|
+
# <code>.dup.force_encoding(\"encoding\")</code>, where <code><encoding></code>
|
|
2803
|
+
# is <code>self.encoding.name</code>:
|
|
2781
2804
|
#
|
|
2782
2805
|
# String: hello
|
|
2783
2806
|
# Dumped: "\xFE\xFF\x00h\x00e\x00l\x00l\x00o".dup.force_encoding("UTF-16")
|
|
@@ -3017,8 +3040,8 @@ class String
|
|
|
3017
3040
|
#
|
|
3018
3041
|
# With no arguments:
|
|
3019
3042
|
#
|
|
3020
|
-
# * Uses the same encoding if
|
|
3021
|
-
# default):
|
|
3043
|
+
# * Uses the same encoding if <code>Encoding.default_internal</code> is `nil`
|
|
3044
|
+
# (the default):
|
|
3022
3045
|
#
|
|
3023
3046
|
# Encoding.default_internal # => nil
|
|
3024
3047
|
# s = "Ruby\x99".force_encoding('Windows-1252')
|
|
@@ -3028,7 +3051,7 @@ class String
|
|
|
3028
3051
|
# t.encoding # => #<Encoding:Windows-1252>
|
|
3029
3052
|
# t.bytes # => [82, 117, 98, 121, 226, 132, 162]
|
|
3030
3053
|
#
|
|
3031
|
-
# * Otherwise, uses the encoding
|
|
3054
|
+
# * Otherwise, uses the encoding <code>Encoding.default_internal</code>:
|
|
3032
3055
|
#
|
|
3033
3056
|
# Encoding.default_internal = 'UTF-8'
|
|
3034
3057
|
# t = s.encode # => "Ruby™"
|
|
@@ -3051,11 +3074,11 @@ class String
|
|
|
3051
3074
|
# Optional keyword arguments `enc_opts` specify encoding options; see [Encoding
|
|
3052
3075
|
# Options](rdoc-ref:encodings.rdoc@Encoding+Options).
|
|
3053
3076
|
#
|
|
3054
|
-
# Please note that, unless
|
|
3055
|
-
# an encoding `enc` to the same encoding `enc` (independent of
|
|
3056
|
-
# given explicitly or implicitly) is a no-op, i.e. the string
|
|
3057
|
-
# without any changes, and no exceptions are raised, even if
|
|
3058
|
-
# bytes.
|
|
3077
|
+
# Please note that, unless <code>invalid: :replace</code> option is given,
|
|
3078
|
+
# conversion from an encoding `enc` to the same encoding `enc` (independent of
|
|
3079
|
+
# whether `enc` is given explicitly or implicitly) is a no-op, i.e. the string
|
|
3080
|
+
# is simply copied without any changes, and no exceptions are raised, even if
|
|
3081
|
+
# there are invalid bytes.
|
|
3059
3082
|
#
|
|
3060
3083
|
# Related: see [Converting to New
|
|
3061
3084
|
# String](rdoc-ref:String@Converting+to+New+String).
|
|
@@ -3351,9 +3374,9 @@ class String
|
|
|
3351
3374
|
# The leading substring is interpreted as hexadecimal when it begins with:
|
|
3352
3375
|
#
|
|
3353
3376
|
# * One or more character representing hexadecimal digits (each in one of the
|
|
3354
|
-
# ranges
|
|
3355
|
-
# interpreted ends at the first
|
|
3356
|
-
# hexadecimal digit:
|
|
3377
|
+
# ranges <code>'0'..'9'</code>, <code>'a'..'f'</code>, or
|
|
3378
|
+
# <code>'A'..'F'</code>); the string to be interpreted ends at the first
|
|
3379
|
+
# character that does not represent a hexadecimal digit:
|
|
3357
3380
|
#
|
|
3358
3381
|
# 'f'.hex # => 15
|
|
3359
3382
|
# '11'.hex # => 17
|
|
@@ -3363,12 +3386,14 @@ class String
|
|
|
3363
3386
|
# 'bar'.hex # => 186 # 'ba' hexadecimal, 'r' not.
|
|
3364
3387
|
# 'deadbeef'.hex # => 3735928559
|
|
3365
3388
|
#
|
|
3366
|
-
# *
|
|
3389
|
+
# * <code>'0x'</code> or <code>'0X'</code>, followed by one or more
|
|
3390
|
+
# hexadecimal digits:
|
|
3367
3391
|
#
|
|
3368
3392
|
# '0xfff'.hex # => 4095
|
|
3369
3393
|
# '0xfffg'.hex # => 4095
|
|
3370
3394
|
#
|
|
3371
|
-
# Any of the above may prefixed with
|
|
3395
|
+
# Any of the above may prefixed with <code>'-'</code>, which negates the
|
|
3396
|
+
# interpreted value:
|
|
3372
3397
|
#
|
|
3373
3398
|
# '-fff'.hex # => -4095
|
|
3374
3399
|
# '-0xFFF'.hex # => -4095
|
|
@@ -3496,15 +3521,15 @@ class String
|
|
|
3496
3521
|
# 'тест'.inspect # => "\"тест\""
|
|
3497
3522
|
# 'こんにちは'.inspect # => "\"こんにちは\""
|
|
3498
3523
|
#
|
|
3499
|
-
# But printable characters double-quote (
|
|
3500
|
-
# escaped:
|
|
3524
|
+
# But printable characters double-quote (<code>'"'</code>) and backslash and
|
|
3525
|
+
# (<code>'\'</code>) are escaped:
|
|
3501
3526
|
#
|
|
3502
3527
|
# '"'.inspect # => "\"\\\"\""
|
|
3503
3528
|
# '\\'.inspect # => "\"\\\\\""
|
|
3504
3529
|
#
|
|
3505
3530
|
# Unprintable characters are the [ASCII
|
|
3506
3531
|
# characters](https://en.wikipedia.org/wiki/ASCII) whose values are in range
|
|
3507
|
-
#
|
|
3532
|
+
# <code>0..31</code>, along with the character whose value is `127`.
|
|
3508
3533
|
#
|
|
3509
3534
|
# Most of these characters are rendered thus:
|
|
3510
3535
|
#
|
|
@@ -3631,7 +3656,7 @@ class String
|
|
|
3631
3656
|
# 'тест'.ljust(10) # => "тест "
|
|
3632
3657
|
# 'こんにちは'.ljust(10) # => "こんにちは "
|
|
3633
3658
|
#
|
|
3634
|
-
# If
|
|
3659
|
+
# If <code>width <= self.length</code>, returns a copy of `self`:
|
|
3635
3660
|
#
|
|
3636
3661
|
# 'hello'.ljust(5) # => "hello"
|
|
3637
3662
|
# 'hello'.ljust(1) # => "hello" # Does not truncate to width.
|
|
@@ -3735,8 +3760,8 @@ class String
|
|
|
3735
3760
|
#
|
|
3736
3761
|
# regexp = Regexp.new(pattern)
|
|
3737
3762
|
#
|
|
3738
|
-
# Returns `true` if
|
|
3739
|
-
# `false` otherwise:
|
|
3763
|
+
# Returns `true` if <code>self[offset..].match(regexp)</code> returns a
|
|
3764
|
+
# MatchData object, `false` otherwise:
|
|
3740
3765
|
#
|
|
3741
3766
|
# 'foo'.match?(/o/) # => true
|
|
3742
3767
|
# 'foo'.match?('o') # => true
|
|
@@ -3835,8 +3860,8 @@ class String
|
|
|
3835
3860
|
# The leading substring is interpreted as octal when it begins with:
|
|
3836
3861
|
#
|
|
3837
3862
|
# * One or more character representing octal digits (each in the range
|
|
3838
|
-
#
|
|
3839
|
-
# does not represent an octal digit:
|
|
3863
|
+
# <code>'0'..'7'</code>); the string to be interpreted ends at the first
|
|
3864
|
+
# character that does not represent an octal digit:
|
|
3840
3865
|
#
|
|
3841
3866
|
# '7'.oct @ => 7
|
|
3842
3867
|
# '11'.oct # => 9
|
|
@@ -3845,39 +3870,41 @@ class String
|
|
|
3845
3870
|
# '7778'.oct # => 511
|
|
3846
3871
|
# '777x'.oct # => 511
|
|
3847
3872
|
#
|
|
3848
|
-
# *
|
|
3873
|
+
# * <code>'0o'</code>, followed by one or more octal digits:
|
|
3849
3874
|
#
|
|
3850
3875
|
# '0o777'.oct # => 511
|
|
3851
3876
|
# '0o7778'.oct # => 511
|
|
3852
3877
|
#
|
|
3853
3878
|
# The leading substring is *not* interpreted as octal when it begins with:
|
|
3854
3879
|
#
|
|
3855
|
-
# *
|
|
3856
|
-
# (each in the range
|
|
3857
|
-
# first character that does not represent a binary
|
|
3858
|
-
# interpreted as binary digits (base 2):
|
|
3880
|
+
# * <code>'0b'</code>, followed by one or more characters representing binary
|
|
3881
|
+
# digits (each in the range <code>'0'..'1'</code>); the string to be
|
|
3882
|
+
# interpreted ends at the first character that does not represent a binary
|
|
3883
|
+
# digit. the string is interpreted as binary digits (base 2):
|
|
3859
3884
|
#
|
|
3860
3885
|
# '0b111'.oct # => 7
|
|
3861
3886
|
# '0b1112'.oct # => 7
|
|
3862
3887
|
#
|
|
3863
|
-
# *
|
|
3864
|
-
# (each in the range
|
|
3865
|
-
# first character that does not represent a decimal
|
|
3866
|
-
# interpreted as decimal digits (base 10):
|
|
3888
|
+
# * <code>'0d'</code>, followed by one or more characters representing decimal
|
|
3889
|
+
# digits (each in the range <code>'0'..'9'</code>); the string to be
|
|
3890
|
+
# interpreted ends at the first character that does not represent a decimal
|
|
3891
|
+
# digit. the string is interpreted as decimal digits (base 10):
|
|
3867
3892
|
#
|
|
3868
3893
|
# '0d999'.oct # => 999
|
|
3869
3894
|
# '0d999x'.oct # => 999
|
|
3870
3895
|
#
|
|
3871
|
-
# *
|
|
3872
|
-
# (each in one of the ranges
|
|
3873
|
-
#
|
|
3874
|
-
#
|
|
3875
|
-
# digits (base
|
|
3896
|
+
# * <code>'0x'</code>, followed by one or more characters representing
|
|
3897
|
+
# hexadecimal digits (each in one of the ranges <code>'0'..'9'</code>,
|
|
3898
|
+
# <code>'a'..'f'</code>, or <code>'A'..'F'</code>); the string to be
|
|
3899
|
+
# interpreted ends at the first character that does not represent a
|
|
3900
|
+
# hexadecimal digit. the string is interpreted as hexadecimal digits (base
|
|
3901
|
+
# 16):
|
|
3876
3902
|
#
|
|
3877
3903
|
# '0xfff'.oct # => 4095
|
|
3878
3904
|
# '0xfffg'.oct # => 4095
|
|
3879
3905
|
#
|
|
3880
|
-
# Any of the above may prefixed with
|
|
3906
|
+
# Any of the above may prefixed with <code>'-'</code>, which negates the
|
|
3907
|
+
# interpreted value:
|
|
3881
3908
|
#
|
|
3882
3909
|
# '-777'.oct # => -511
|
|
3883
3910
|
# '-0777'.oct # => -511
|
|
@@ -3929,11 +3956,11 @@ class String
|
|
|
3929
3956
|
#
|
|
3930
3957
|
# [self.dup, "", ""]
|
|
3931
3958
|
#
|
|
3932
|
-
# Note that in the examples below, a returned string
|
|
3933
|
-
# `self`, not `self`.
|
|
3959
|
+
# Note that in the examples below, a returned string <code>'hello'</code> is a
|
|
3960
|
+
# copy of `self`, not `self`.
|
|
3934
3961
|
#
|
|
3935
|
-
# If `pattern` is a Regexp, performs the equivalent of
|
|
3936
|
-
# (also setting [matched-data
|
|
3962
|
+
# If `pattern` is a Regexp, performs the equivalent of
|
|
3963
|
+
# <code>self.match(pattern)</code> (also setting [matched-data
|
|
3937
3964
|
# variables](rdoc-ref:language/globals.md@Matched+Data)):
|
|
3938
3965
|
#
|
|
3939
3966
|
# 'hello'.partition(/h/) # => ["", "h", "ello"]
|
|
@@ -3946,8 +3973,8 @@ class String
|
|
|
3946
3973
|
# 'hello'.partition(/x/) # => ["hello", "", ""]
|
|
3947
3974
|
#
|
|
3948
3975
|
# If `pattern` is not a Regexp, converts it to a string (if it is not already
|
|
3949
|
-
# one), then performs the equivalent of
|
|
3950
|
-
# set [matched-data global
|
|
3976
|
+
# one), then performs the equivalent of <code>self.index(pattern)</code> (and
|
|
3977
|
+
# does *not* set [matched-data global
|
|
3951
3978
|
# variables](rdoc-ref:language/globals.md@Matched+Data)):
|
|
3952
3979
|
#
|
|
3953
3980
|
# 'hello'.partition('h') # => ["", "h", "ello"]
|
|
@@ -4098,7 +4125,7 @@ class String
|
|
|
4098
4125
|
# 'тест'.rjust(10) # => " тест"
|
|
4099
4126
|
# 'こんにちは'.rjust(10) # => " こんにちは"
|
|
4100
4127
|
#
|
|
4101
|
-
# If
|
|
4128
|
+
# If <code>width <= self.size</code>, returns a copy of `self`:
|
|
4102
4129
|
#
|
|
4103
4130
|
# 'hello'.rjust(5, 'ab') # => "hello"
|
|
4104
4131
|
# 'hello'.rjust(1, 'ab') # => "hello"
|
|
@@ -4132,10 +4159,10 @@ class String
|
|
|
4132
4159
|
# The pattern used is:
|
|
4133
4160
|
#
|
|
4134
4161
|
# * `pattern` itself, if it is a Regexp.
|
|
4135
|
-
# *
|
|
4162
|
+
# * <code>Regexp.quote(pattern)</code>, if `pattern` is a string.
|
|
4136
4163
|
#
|
|
4137
|
-
# Note that in the examples below, a returned string
|
|
4138
|
-
# `self`, not `self`.
|
|
4164
|
+
# Note that in the examples below, a returned string <code>'hello'</code> is a
|
|
4165
|
+
# copy of `self`, not `self`.
|
|
4139
4166
|
#
|
|
4140
4167
|
# If `pattern` is a Regexp, searches for the last matching substring (also
|
|
4141
4168
|
# setting [matched-data global
|
|
@@ -4218,7 +4245,8 @@ class String
|
|
|
4218
4245
|
# Matches a pattern against `self`:
|
|
4219
4246
|
#
|
|
4220
4247
|
# * If `pattern` is a Regexp, the pattern used is `pattern` itself.
|
|
4221
|
-
# * If `pattern` is a string, the pattern used is
|
|
4248
|
+
# * If `pattern` is a string, the pattern used is
|
|
4249
|
+
# <code>Regexp.quote(pattern)</code>.
|
|
4222
4250
|
#
|
|
4223
4251
|
# Generates a collection of matching results and updates [regexp-related global
|
|
4224
4252
|
# variables](rdoc-ref:Regexp@Global+Variables):
|
|
@@ -4267,8 +4295,8 @@ class String
|
|
|
4267
4295
|
# `replacement_string`.
|
|
4268
4296
|
#
|
|
4269
4297
|
# With no block given, replaces each invalid sequence with the given
|
|
4270
|
-
# `default_replacement_string` (by default,
|
|
4271
|
-
# otherwise):
|
|
4298
|
+
# `default_replacement_string` (by default, <code>"�"</code> for a Unicode
|
|
4299
|
+
# encoding, <code>'?'</code> otherwise):
|
|
4272
4300
|
#
|
|
4273
4301
|
# "foo\x81\x81bar"scrub # => "foo��bar"
|
|
4274
4302
|
# "foo\x81\x81bar".force_encoding('US-ASCII').scrub # => "foo??bar"
|
|
@@ -4340,7 +4368,7 @@ class String
|
|
|
4340
4368
|
# <!-- rdoc-file=string.c -->
|
|
4341
4369
|
# Returns the substring of `self` specified by the arguments.
|
|
4342
4370
|
#
|
|
4343
|
-
#
|
|
4371
|
+
# <strong>Form <code>self[index]</code></strong>
|
|
4344
4372
|
#
|
|
4345
4373
|
# With non-negative integer argument `index` given, returns the 1-character
|
|
4346
4374
|
# substring found in self at character offset index:
|
|
@@ -4358,7 +4386,7 @@ class String
|
|
|
4358
4386
|
# 'hello'[-5] # => "h"
|
|
4359
4387
|
# 'hello'[-6] # => nil
|
|
4360
4388
|
#
|
|
4361
|
-
#
|
|
4389
|
+
# <strong>Form <code>self[start, length]</code></strong>
|
|
4362
4390
|
#
|
|
4363
4391
|
# With integer arguments `start` and `length` given, returns a substring of size
|
|
4364
4392
|
# `length` characters (as available) beginning at character offset specified by
|
|
@@ -4385,10 +4413,10 @@ class String
|
|
|
4385
4413
|
#
|
|
4386
4414
|
# 'hello'[5, 3] # => ""
|
|
4387
4415
|
#
|
|
4388
|
-
#
|
|
4416
|
+
# <strong>Form <code>self[range]</code></strong>
|
|
4389
4417
|
#
|
|
4390
|
-
# With Range argument `range` given, forms substring
|
|
4391
|
-
# range.size]
|
|
4418
|
+
# With Range argument `range` given, forms substring <code>self[range.start,
|
|
4419
|
+
# range.size]</code>:
|
|
4392
4420
|
#
|
|
4393
4421
|
# 'hello'[0..2] # => "hel"
|
|
4394
4422
|
# 'hello'[0, 3] # => "hel"
|
|
@@ -4399,7 +4427,7 @@ class String
|
|
|
4399
4427
|
# 'hello'[0, 0] # => ""
|
|
4400
4428
|
# 'hello'[0...0] # => ""
|
|
4401
4429
|
#
|
|
4402
|
-
#
|
|
4430
|
+
# <strong>Form <code>self[regexp, capture = 0]</code></strong>
|
|
4403
4431
|
#
|
|
4404
4432
|
# With Regexp argument `regexp` given and `capture` as zero, searches for a
|
|
4405
4433
|
# matching substring in `self`; updates [Regexp-related global
|
|
@@ -4421,7 +4449,7 @@ class String
|
|
|
4421
4449
|
# 'hello'[/(h)(e)(l+)(o)/, 4] # => "o"
|
|
4422
4450
|
# 'hello'[/(h)(e)(l+)(o)/, 5] # => nil
|
|
4423
4451
|
#
|
|
4424
|
-
#
|
|
4452
|
+
# <strong>Form <code>self[substring]</code></strong>
|
|
4425
4453
|
#
|
|
4426
4454
|
# With string argument `substring` given, returns the matching substring of
|
|
4427
4455
|
# `self`, if found:
|
|
@@ -4474,15 +4502,15 @@ class String
|
|
|
4474
4502
|
# Creates an array of substrings by splitting `self` at each occurrence of the
|
|
4475
4503
|
# given field separator `field_sep`.
|
|
4476
4504
|
#
|
|
4477
|
-
# With no arguments given, splits using the field separator
|
|
4478
|
-
# value is `nil`.
|
|
4505
|
+
# With no arguments given, splits using the field separator <code>$;</code>,
|
|
4506
|
+
# whose default value is `nil`.
|
|
4479
4507
|
#
|
|
4480
4508
|
# With no block given, returns the array of substrings:
|
|
4481
4509
|
#
|
|
4482
4510
|
# 'abracadabra'.split('a') # => ["", "br", "c", "d", "br"]
|
|
4483
4511
|
#
|
|
4484
|
-
# When `field_sep` is `nil` or
|
|
4485
|
-
# of whitespace:
|
|
4512
|
+
# When `field_sep` is `nil` or <code>' '</code> (a single space), splits at each
|
|
4513
|
+
# sequence of whitespace:
|
|
4486
4514
|
#
|
|
4487
4515
|
# 'foo bar baz'.split(nil) # => ["foo", "bar", "baz"]
|
|
4488
4516
|
# 'foo bar baz'.split(' ') # => ["foo", "bar", "baz"]
|
|
@@ -4497,8 +4525,8 @@ class String
|
|
|
4497
4525
|
# 'тест'.split('') # => ["т", "е", "с", "т"]
|
|
4498
4526
|
# 'こんにちは'.split('') # => ["こ", "ん", "に", "ち", "は"]
|
|
4499
4527
|
#
|
|
4500
|
-
# When `field_sep` is a non-empty string and different from
|
|
4501
|
-
# space), uses that string as the separator:
|
|
4528
|
+
# When `field_sep` is a non-empty string and different from <code>' '</code> (a
|
|
4529
|
+
# single space), uses that string as the separator:
|
|
4502
4530
|
#
|
|
4503
4531
|
# 'abracadabra'.split('a') # => ["", "br", "c", "d", "br"]
|
|
4504
4532
|
# 'abracadabra'.split('ab') # => ["", "racad", "ra"]
|
|
@@ -4528,7 +4556,8 @@ class String
|
|
|
4528
4556
|
# 'abracadabra'.split('a', 0) # => ["", "br", "c", "d", "br"] # Empty string after last 'a' omitted.
|
|
4529
4557
|
#
|
|
4530
4558
|
# When `limit` is a positive integer, there is a limit on the size of the array
|
|
4531
|
-
# (no more than
|
|
4559
|
+
# (no more than <code>n - 1</code> splits occur), and trailing empty strings are
|
|
4560
|
+
# included:
|
|
4532
4561
|
#
|
|
4533
4562
|
# 'abracadabra'.split('', 3) # => ["a", "b", "racadabra"]
|
|
4534
4563
|
# 'abracadabra'.split('a', 3) # => ["", "br", "cadabra"]
|
|
@@ -4637,7 +4666,7 @@ class String
|
|
|
4637
4666
|
# For each argument, the pattern used is:
|
|
4638
4667
|
#
|
|
4639
4668
|
# * The pattern itself, if it is a Regexp.
|
|
4640
|
-
# *
|
|
4669
|
+
# * <code>Regexp.quote(pattern)</code>, if it is a string.
|
|
4641
4670
|
#
|
|
4642
4671
|
# Returns `true` if any pattern matches the beginning, `false` otherwise:
|
|
4643
4672
|
#
|
|
@@ -4830,7 +4859,7 @@ class String
|
|
|
4830
4859
|
# -->
|
|
4831
4860
|
# Returns a basic `n`-bit [checksum](https://en.wikipedia.org/wiki/Checksum) of
|
|
4832
4861
|
# the characters in `self`; the checksum is the sum of the binary value of each
|
|
4833
|
-
# byte in `self`, modulo
|
|
4862
|
+
# byte in `self`, modulo <code>2**n - 1</code>:
|
|
4834
4863
|
#
|
|
4835
4864
|
# 'hello'.sum # => 532
|
|
4836
4865
|
# 'hello'.sum(4) # => 4
|
|
@@ -4875,8 +4904,9 @@ class String
|
|
|
4875
4904
|
# s = 'こんにちは'
|
|
4876
4905
|
# s.swapcase == s # => true
|
|
4877
4906
|
#
|
|
4878
|
-
# The casing is affected by the given `mapping`, which may be
|
|
4879
|
-
# or
|
|
4907
|
+
# The casing is affected by the given `mapping`, which may be
|
|
4908
|
+
# <code>:ascii</code>, <code>:fold</code>, or <code>:turkic</code>; see [Case
|
|
4909
|
+
# Mappings](rdoc-ref:case_mapping.rdoc@Case+Mappings).
|
|
4880
4910
|
#
|
|
4881
4911
|
# Related: see [Converting to New
|
|
4882
4912
|
# String](rdoc-ref:String@Converting+to+New+String).
|
|
@@ -4913,8 +4943,9 @@ class String
|
|
|
4913
4943
|
# (real and imaginary parts) or polar coordinates (magnitude and angle parts),
|
|
4914
4944
|
# depending on an included or implied "separator" character:
|
|
4915
4945
|
#
|
|
4916
|
-
# *
|
|
4917
|
-
#
|
|
4946
|
+
# * <code>'+'</code>, <code>'-'</code>, or no separator: rectangular
|
|
4947
|
+
# coordinates.
|
|
4948
|
+
# * <code>'@'</code>: polar coordinates.
|
|
4918
4949
|
#
|
|
4919
4950
|
# **In Brief**
|
|
4920
4951
|
#
|
|
@@ -4994,8 +5025,8 @@ class String
|
|
|
4994
5025
|
#
|
|
4995
5026
|
# **Rectangular Coordinates**
|
|
4996
5027
|
#
|
|
4997
|
-
# With separator
|
|
4998
|
-
# rectangular coordinates: real and imaginary.
|
|
5028
|
+
# With separator <code>'+'</code> or <code>'-'</code>, or with no separator,
|
|
5029
|
+
# interprets the values as rectangular coordinates: real and imaginary.
|
|
4999
5030
|
#
|
|
5000
5031
|
# With no separator, assigns a single value to either the real or the imaginary
|
|
5001
5032
|
# part:
|
|
@@ -5005,7 +5036,7 @@ class String
|
|
|
5005
5036
|
# '1i'.to_c # => (0+1i) # Imaginary (trailing 'i').
|
|
5006
5037
|
# 'i'.to_c # => (0+1i) # Special case (imaginary 1).
|
|
5007
5038
|
#
|
|
5008
|
-
# With separator
|
|
5039
|
+
# With separator <code>'+'</code>, both parts positive (or zero):
|
|
5009
5040
|
#
|
|
5010
5041
|
# # Without trailing 'i'.
|
|
5011
5042
|
# '+'.to_c # => (0+0i) # No values: defaults to zero.
|
|
@@ -5017,7 +5048,7 @@ class String
|
|
|
5017
5048
|
# '2+i'.to_c # => (2+1i) # Value before '+': real and imaginary 1.
|
|
5018
5049
|
# '2+1i'.to_c # => (2+1i) # Values before and after '+': real and imaginary.
|
|
5019
5050
|
#
|
|
5020
|
-
# With separator
|
|
5051
|
+
# With separator <code>'-'</code>, negative imaginary part:
|
|
5021
5052
|
#
|
|
5022
5053
|
# # Without trailing 'i'.
|
|
5023
5054
|
# '-'.to_c # => (0+0i) # No values: defaults to zero.
|
|
@@ -5029,13 +5060,13 @@ class String
|
|
|
5029
5060
|
# '2-i'.to_c # => (2-1i) # Value before '-': positive real, negative imaginary.
|
|
5030
5061
|
# '2-1i'.to_c # => (2-1i) # Values before and after '-': positive real, negative imaginary.
|
|
5031
5062
|
#
|
|
5032
|
-
# Note that the suffixed character
|
|
5033
|
-
#
|
|
5063
|
+
# Note that the suffixed character <code>'i'</code> may instead be one of
|
|
5064
|
+
# <code>'I'</code>, <code>'j'</code>, or <code>'J'</code>, with the same effect.
|
|
5034
5065
|
#
|
|
5035
5066
|
# **Polar Coordinates**
|
|
5036
5067
|
#
|
|
5037
|
-
# With separator
|
|
5038
|
-
# and angle.
|
|
5068
|
+
# With separator <code>'@'</code>) interprets the values as polar coordinates:
|
|
5069
|
+
# magnitude and angle.
|
|
5039
5070
|
#
|
|
5040
5071
|
# '2@'.to_c.polar # => [2, 0.0] # Value before '@': magnitude only.
|
|
5041
5072
|
# # Values before and after '@': magnitude and angle.
|
|
@@ -5048,8 +5079,9 @@ class String
|
|
|
5048
5079
|
#
|
|
5049
5080
|
# '1.0@0'.to_c # => (1+0.0i)
|
|
5050
5081
|
#
|
|
5051
|
-
# Note that in all cases, the suffixed character
|
|
5052
|
-
#
|
|
5082
|
+
# Note that in all cases, the suffixed character <code>'i'</code> may instead be
|
|
5083
|
+
# one of <code>'I'</code>, <code>'j'</code>, <code>'J'</code>, with the same
|
|
5084
|
+
# effect.
|
|
5053
5085
|
#
|
|
5054
5086
|
# See [Converting to Non-String](rdoc-ref:String@Converting+to+Non--5CString).
|
|
5055
5087
|
#
|
|
@@ -5081,7 +5113,8 @@ class String
|
|
|
5081
5113
|
# - to_i(base = 10) -> integer
|
|
5082
5114
|
# -->
|
|
5083
5115
|
# Returns the result of interpreting leading characters in `self` as an integer
|
|
5084
|
-
# in the given `base`; `base` must be either `0` or in range
|
|
5116
|
+
# in the given `base`; `base` must be either `0` or in range
|
|
5117
|
+
# <code>(2..36)</code>:
|
|
5085
5118
|
#
|
|
5086
5119
|
# '123456'.to_i # => 123456
|
|
5087
5120
|
# '123def'.to_i(16) # => 1195503
|
|
@@ -5132,7 +5165,8 @@ class String
|
|
|
5132
5165
|
#
|
|
5133
5166
|
# 'BWV 1079'.to_r # => (0/1)
|
|
5134
5167
|
#
|
|
5135
|
-
# NOTE:
|
|
5168
|
+
# NOTE: <code>'0.3'.to_r</code> is equivalent to <code>3/10r</code>, but is
|
|
5169
|
+
# different from <code>0.3.to_r</code>:
|
|
5136
5170
|
#
|
|
5137
5171
|
# '0.3'.to_r # => (3/10)
|
|
5138
5172
|
# 3/10r # => (3/10)
|
|
@@ -5214,7 +5248,7 @@ class String
|
|
|
5214
5248
|
# Related: see [Converting to New
|
|
5215
5249
|
# String](rdoc-ref:String@Converting+to+New+String).
|
|
5216
5250
|
#
|
|
5217
|
-
def tr: (selector source, string
|
|
5251
|
+
def tr: (selector source, string replacement) -> String
|
|
5218
5252
|
|
|
5219
5253
|
# <!--
|
|
5220
5254
|
# rdoc-file=string.c
|
|
@@ -5227,7 +5261,7 @@ class String
|
|
|
5227
5261
|
#
|
|
5228
5262
|
# Related: [Modifying](rdoc-ref:String@Modifying).
|
|
5229
5263
|
#
|
|
5230
|
-
def tr!: (selector source, string
|
|
5264
|
+
def tr!: (selector source, string replacement) -> self?
|
|
5231
5265
|
|
|
5232
5266
|
# <!--
|
|
5233
5267
|
# rdoc-file=string.c
|
|
@@ -5285,21 +5319,23 @@ class String
|
|
|
5285
5319
|
# Argument `form` must be one of the following symbols (see [Unicode
|
|
5286
5320
|
# normalization forms](https://unicode.org/reports/tr15/#Norm_Forms)):
|
|
5287
5321
|
#
|
|
5288
|
-
# *
|
|
5289
|
-
#
|
|
5290
|
-
# *
|
|
5291
|
-
# *
|
|
5322
|
+
# * <code>:nfc</code>: Canonical decomposition, followed by canonical
|
|
5323
|
+
# composition.
|
|
5324
|
+
# * <code>:nfd</code>: Canonical decomposition.
|
|
5325
|
+
# * <code>:nfkc</code>: Compatibility decomposition, followed by canonical
|
|
5326
|
+
# composition.
|
|
5327
|
+
# * <code>:nfkd</code>: Compatibility decomposition.
|
|
5292
5328
|
#
|
|
5293
5329
|
# The encoding of `self` must be one of:
|
|
5294
5330
|
#
|
|
5295
|
-
# *
|
|
5296
|
-
# *
|
|
5297
|
-
# *
|
|
5298
|
-
# *
|
|
5299
|
-
# *
|
|
5300
|
-
# *
|
|
5301
|
-
# *
|
|
5302
|
-
# *
|
|
5331
|
+
# * <code>Encoding::UTF_8</code>.
|
|
5332
|
+
# * <code>Encoding::UTF_16BE</code>.
|
|
5333
|
+
# * <code>Encoding::UTF_16LE</code>.
|
|
5334
|
+
# * <code>Encoding::UTF_32BE</code>.
|
|
5335
|
+
# * <code>Encoding::UTF_32LE</code>.
|
|
5336
|
+
# * <code>Encoding::GB18030</code>.
|
|
5337
|
+
# * <code>Encoding::UCS_2BE</code>.
|
|
5338
|
+
# * <code>Encoding::UCS_4BE</code>.
|
|
5303
5339
|
#
|
|
5304
5340
|
# Examples:
|
|
5305
5341
|
#
|
|
@@ -5329,7 +5365,8 @@ class String
|
|
|
5329
5365
|
# Returns whether `self` is in the given `form` of Unicode normalization; see
|
|
5330
5366
|
# String#unicode_normalize.
|
|
5331
5367
|
#
|
|
5332
|
-
# The `form` must be one of
|
|
5368
|
+
# The `form` must be one of <code>:nfc</code>, <code>:nfd</code>,
|
|
5369
|
+
# <code>:nfkc</code>, or <code>:nfkd</code>.
|
|
5333
5370
|
#
|
|
5334
5371
|
# Examples:
|
|
5335
5372
|
#
|
|
@@ -5403,8 +5440,9 @@ class String
|
|
|
5403
5440
|
# s = 'こんにちは'
|
|
5404
5441
|
# s.upcase == s # => true
|
|
5405
5442
|
#
|
|
5406
|
-
# The casing is affected by the given `mapping`, which may be
|
|
5407
|
-
# or
|
|
5443
|
+
# The casing is affected by the given `mapping`, which may be
|
|
5444
|
+
# <code>:ascii</code>, <code>:fold</code>, or <code>:turkic</code>; see [Case
|
|
5445
|
+
# Mappings](rdoc-ref:case_mapping.rdoc@Case+Mappings).
|
|
5408
5446
|
#
|
|
5409
5447
|
# Related: see [Converting to New
|
|
5410
5448
|
# String](rdoc-ref:String@Converting+to+New+String).
|
|
@@ -5437,8 +5475,8 @@ class String
|
|
|
5437
5475
|
# -->
|
|
5438
5476
|
# With a block given, calls the block with each `String` value returned by
|
|
5439
5477
|
# successive calls to String#succ; the first value is `self`, the next is
|
|
5440
|
-
#
|
|
5441
|
-
# reached; returns `self`:
|
|
5478
|
+
# <code>self.succ</code>, and so on; the sequence terminates when value
|
|
5479
|
+
# `other_string` is reached; returns `self`:
|
|
5442
5480
|
#
|
|
5443
5481
|
# a = []
|
|
5444
5482
|
# 'a'.upto('f') {|c| a.push(c) }
|
|
@@ -5492,22 +5530,3 @@ class String
|
|
|
5492
5530
|
#
|
|
5493
5531
|
def valid_encoding?: () -> bool
|
|
5494
5532
|
end
|
|
5495
|
-
|
|
5496
|
-
%a{deprecated}
|
|
5497
|
-
interface _ArefFromStringToString
|
|
5498
|
-
def []: (String) -> String
|
|
5499
|
-
end
|
|
5500
|
-
|
|
5501
|
-
%a{deprecated}
|
|
5502
|
-
type String::encode_fallback = Hash[String, String] | Proc | Method | String::_ArefFromStringToString
|
|
5503
|
-
|
|
5504
|
-
# Don't use this interface directly
|
|
5505
|
-
#
|
|
5506
|
-
# This is a copy of `::_ArefFromStringToString` but without deprecated attribute.
|
|
5507
|
-
# This is a workaround to avoid deprecation warnings in `String::encode_fallback` type.
|
|
5508
|
-
#
|
|
5509
|
-
# This type will be deprecated soon once `::_ArefFromStringToString` and `String::encode_fallback` are removed.
|
|
5510
|
-
#
|
|
5511
|
-
interface String::_ArefFromStringToString
|
|
5512
|
-
def []: (String) -> String
|
|
5513
|
-
end
|