rbs 2.0.0 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ruby.yml +5 -0
- data/CHANGELOG.md +23 -0
- data/README.md +6 -1
- data/core/array.rbs +2866 -1086
- data/core/basic_object.rbs +150 -30
- data/core/binding.rbs +33 -0
- data/core/builtin.rbs +4 -4
- data/core/class.rbs +43 -5
- data/core/comparable.rbs +57 -0
- data/core/complex.rbs +170 -4
- data/core/constants.rbs +51 -0
- data/core/deprecated.rbs +7 -0
- data/core/dir.rbs +305 -20
- data/core/encoding.rbs +472 -77
- data/core/enumerable.rbs +2173 -234
- data/core/enumerator.rbs +448 -182
- data/core/env.rbs +448 -1
- data/core/errno.rbs +1 -10
- data/core/errors.rbs +152 -2
- data/core/exception.rbs +201 -127
- data/core/false_class.rbs +27 -0
- data/core/fiber.rbs +118 -37
- data/core/fiber_error.rbs +8 -9
- data/core/file.rbs +1059 -139
- data/core/file_test.rbs +287 -32
- data/core/float.rbs +776 -300
- data/core/gc.rbs +185 -34
- data/core/global_variables.rbs +5 -1
- data/core/hash.rbs +1582 -649
- data/core/integer.rbs +974 -204
- data/core/io/buffer.rbs +710 -0
- data/core/io/wait.rbs +29 -8
- data/core/io.rbs +2438 -417
- data/core/kernel.rbs +2315 -316
- data/core/marshal.rbs +37 -2
- data/core/match_data.rbs +123 -6
- data/core/math.rbs +126 -6
- data/core/method.rbs +226 -102
- data/core/module.rbs +421 -45
- data/core/nil_class.rbs +64 -0
- data/core/numeric.rbs +620 -142
- data/core/object.rbs +453 -81
- data/core/object_space.rbs +92 -2
- data/core/proc.rbs +482 -285
- data/core/process.rbs +443 -34
- data/core/ractor.rbs +232 -9
- data/core/random.rbs +151 -52
- data/core/range.rbs +885 -160
- data/core/rational.rbs +122 -6
- data/core/rb_config.rbs +14 -4
- data/core/refinement.rbs +44 -0
- data/core/regexp.rbs +156 -14
- data/core/ruby_vm.rbs +42 -3
- data/core/signal.rbs +78 -39
- data/core/string.rbs +2123 -567
- data/core/string_io.rbs +204 -0
- data/core/struct.rbs +283 -28
- data/core/symbol.rbs +304 -30
- data/core/thread.rbs +1288 -688
- data/core/thread_group.rbs +66 -10
- data/core/time.rbs +643 -217
- data/core/trace_point.rbs +100 -12
- data/core/true_class.rbs +24 -0
- data/core/unbound_method.rbs +73 -7
- data/core/warning.rbs +37 -12
- data/docs/CONTRIBUTING.md +40 -34
- data/docs/stdlib.md +3 -102
- data/lib/rbs/annotate/annotations.rb +197 -0
- data/lib/rbs/annotate/formatter.rb +80 -0
- data/lib/rbs/annotate/rdoc_annotator.rb +398 -0
- data/lib/rbs/annotate/rdoc_source.rb +120 -0
- data/lib/rbs/annotate.rb +6 -0
- data/lib/rbs/cli.rb +45 -1
- data/lib/rbs/definition_builder.rb +5 -1
- data/lib/rbs/location_aux.rb +12 -0
- data/lib/rbs/prototype/rb.rb +12 -0
- data/lib/rbs/version.rb +1 -1
- data/sig/annotate/annotations.rbs +102 -0
- data/sig/annotate/formatter.rbs +24 -0
- data/sig/annotate/rdoc_annotater.rbs +82 -0
- data/sig/annotate/rdoc_source.rbs +30 -0
- data/sig/cli.rbs +2 -0
- data/sig/collection/{collections.rbs → sources.rbs} +0 -0
- data/sig/location.rbs +6 -0
- data/sig/method_types.rbs +5 -1
- data/sig/polyfill.rbs +78 -0
- data/stdlib/abbrev/0/abbrev.rbs +6 -0
- data/stdlib/abbrev/0/array.rbs +26 -0
- data/stdlib/base64/0/base64.rbs +31 -0
- data/stdlib/benchmark/0/benchmark.rbs +74 -3
- data/stdlib/bigdecimal/0/big_decimal.rbs +614 -165
- data/stdlib/bigdecimal-math/0/big_math.rbs +41 -64
- data/stdlib/cgi/0/core.rbs +59 -0
- data/stdlib/coverage/0/coverage.rbs +164 -2
- data/stdlib/csv/0/csv.rbs +2862 -398
- data/stdlib/date/0/date.rbs +483 -25
- data/stdlib/date/0/date_time.rbs +187 -12
- data/stdlib/dbm/0/dbm.rbs +152 -17
- data/stdlib/digest/0/digest.rbs +146 -0
- data/stdlib/erb/0/erb.rbs +65 -245
- data/stdlib/fiber/0/fiber.rbs +73 -91
- data/stdlib/fileutils/0/fileutils.rbs +301 -1
- data/stdlib/find/0/find.rbs +9 -0
- data/stdlib/forwardable/0/forwardable.rbs +65 -1
- data/stdlib/io-console/0/io-console.rbs +227 -15
- data/stdlib/ipaddr/0/ipaddr.rbs +161 -0
- data/stdlib/json/0/json.rbs +1146 -144
- data/stdlib/logger/0/formatter.rbs +24 -0
- data/stdlib/logger/0/log_device.rbs +64 -0
- data/stdlib/logger/0/logger.rbs +165 -13
- data/stdlib/logger/0/period.rbs +10 -0
- data/stdlib/logger/0/severity.rbs +26 -0
- data/stdlib/monitor/0/monitor.rbs +163 -0
- data/stdlib/mutex_m/0/mutex_m.rbs +35 -6
- data/stdlib/net-http/0/net-http.rbs +1492 -683
- data/stdlib/nkf/0/nkf.rbs +372 -0
- data/stdlib/objspace/0/objspace.rbs +149 -90
- data/stdlib/openssl/0/openssl.rbs +8108 -71
- data/stdlib/optparse/0/optparse.rbs +487 -19
- data/stdlib/pathname/0/pathname.rbs +425 -124
- data/stdlib/prettyprint/0/prettyprint.rbs +120 -99
- data/stdlib/prime/0/integer-extension.rbs +20 -2
- data/stdlib/prime/0/prime.rbs +88 -21
- data/stdlib/pstore/0/pstore.rbs +102 -0
- data/stdlib/pty/0/pty.rbs +64 -14
- data/stdlib/resolv/0/resolv.rbs +420 -31
- data/stdlib/rubygems/0/basic_specification.rbs +4 -1
- data/stdlib/rubygems/0/config_file.rbs +33 -1
- data/stdlib/rubygems/0/dependency_installer.rbs +4 -3
- data/stdlib/rubygems/0/installer.rbs +13 -1
- data/stdlib/rubygems/0/path_support.rbs +4 -1
- data/stdlib/rubygems/0/platform.rbs +5 -1
- data/stdlib/rubygems/0/request_set.rbs +44 -2
- data/stdlib/rubygems/0/requirement.rbs +65 -2
- data/stdlib/rubygems/0/rubygems.rbs +407 -0
- data/stdlib/rubygems/0/source_list.rbs +13 -0
- data/stdlib/rubygems/0/specification.rbs +21 -1
- data/stdlib/rubygems/0/stream_ui.rbs +3 -1
- data/stdlib/rubygems/0/uninstaller.rbs +8 -1
- data/stdlib/rubygems/0/version.rbs +60 -157
- data/stdlib/securerandom/0/securerandom.rbs +44 -0
- data/stdlib/set/0/set.rbs +420 -106
- data/stdlib/shellwords/0/shellwords.rbs +55 -77
- data/stdlib/singleton/0/singleton.rbs +20 -0
- data/stdlib/socket/0/addrinfo.rbs +210 -9
- data/stdlib/socket/0/basic_socket.rbs +103 -11
- data/stdlib/socket/0/ip_socket.rbs +31 -9
- data/stdlib/socket/0/socket.rbs +586 -38
- data/stdlib/socket/0/tcp_server.rbs +22 -2
- data/stdlib/socket/0/tcp_socket.rbs +12 -1
- data/stdlib/socket/0/udp_socket.rbs +25 -2
- data/stdlib/socket/0/unix_server.rbs +22 -2
- data/stdlib/socket/0/unix_socket.rbs +45 -5
- data/stdlib/strscan/0/string_scanner.rbs +210 -9
- data/stdlib/tempfile/0/tempfile.rbs +58 -10
- data/stdlib/time/0/time.rbs +208 -116
- data/stdlib/timeout/0/timeout.rbs +10 -0
- data/stdlib/tmpdir/0/tmpdir.rbs +13 -4
- data/stdlib/tsort/0/cyclic.rbs +1 -0
- data/stdlib/tsort/0/interfaces.rbs +1 -0
- data/stdlib/tsort/0/tsort.rbs +42 -0
- data/stdlib/uri/0/common.rbs +57 -8
- data/stdlib/uri/0/file.rbs +55 -109
- data/stdlib/uri/0/ftp.rbs +6 -3
- data/stdlib/uri/0/generic.rbs +556 -327
- data/stdlib/uri/0/http.rbs +26 -115
- data/stdlib/uri/0/https.rbs +8 -102
- data/stdlib/uri/0/ldap.rbs +143 -137
- data/stdlib/uri/0/ldaps.rbs +8 -102
- data/stdlib/uri/0/mailto.rbs +3 -0
- data/stdlib/uri/0/rfc2396_parser.rbs +66 -26
- data/stdlib/uri/0/ws.rbs +6 -3
- data/stdlib/uri/0/wss.rbs +5 -3
- data/stdlib/yaml/0/dbm.rbs +151 -87
- data/stdlib/yaml/0/store.rbs +6 -0
- data/stdlib/zlib/0/zlib.rbs +90 -31
- metadata +17 -5
- data/lib/rbs/location.rb +0 -221
data/core/string.rbs
CHANGED
@@ -1,187 +1,978 @@
|
|
1
|
-
#
|
2
|
-
#
|
3
|
-
# String::new or as
|
1
|
+
# <!-- rdoc-file=string.c -->
|
2
|
+
# A String object has an arbitrary sequence of bytes, typically representing
|
3
|
+
# text or binary data. A String object may be created using String::new or as
|
4
|
+
# literals.
|
4
5
|
#
|
5
|
-
#
|
6
|
-
#
|
7
|
-
#
|
8
|
-
#
|
6
|
+
# String objects differ from Symbol objects in that Symbol objects are designed
|
7
|
+
# to be used as identifiers, instead of text or data.
|
8
|
+
#
|
9
|
+
# You can create a String object explicitly with:
|
10
|
+
#
|
11
|
+
# * A [string literal](doc/syntax/literals_rdoc.html#label-String+Literals).
|
12
|
+
# * A [heredoc
|
13
|
+
# literal](doc/syntax/literals_rdoc.html#label-Here+Document+Literals).
|
14
|
+
#
|
15
|
+
#
|
16
|
+
# You can convert certain objects to Strings with:
|
17
|
+
#
|
18
|
+
# * Method [String](Kernel.html#method-i-String).
|
19
|
+
#
|
20
|
+
#
|
21
|
+
# Some String methods modify `self`. Typically, a method whose name ends with
|
22
|
+
# `!` modifies `self` and returns `self`; often a similarly named method
|
23
|
+
# (without the `!`) returns a new string.
|
24
|
+
#
|
25
|
+
# In general, if there exist both bang and non-bang version of method, the bang!
|
26
|
+
# mutates and the non-bang! does not. However, a method without a bang can also
|
27
|
+
# mutate, such as String#replace.
|
28
|
+
#
|
29
|
+
# ## Substitution Methods
|
30
|
+
#
|
31
|
+
# These methods perform substitutions:
|
32
|
+
#
|
33
|
+
# * String#sub: One substitution (or none); returns a new string.
|
34
|
+
# * String#sub!: One substitution (or none); returns `self`.
|
35
|
+
# * String#gsub: Zero or more substitutions; returns a new string.
|
36
|
+
# * String#gsub!: Zero or more substitutions; returns `self`.
|
37
|
+
#
|
38
|
+
#
|
39
|
+
# Each of these methods takes:
|
40
|
+
#
|
41
|
+
# * A first argument, `pattern` (string or regexp), that specifies the
|
42
|
+
# substring(s) to be replaced.
|
43
|
+
#
|
44
|
+
# * Either of these:
|
45
|
+
#
|
46
|
+
# * A second argument, `replacement` (string or hash), that determines the
|
47
|
+
# replacing string.
|
48
|
+
# * A block that will determine the replacing string.
|
49
|
+
#
|
50
|
+
#
|
51
|
+
#
|
52
|
+
# The examples in this section mostly use methods String#sub and String#gsub;
|
53
|
+
# the principles illustrated apply to all four substitution methods.
|
54
|
+
#
|
55
|
+
# **Argument `pattern`**
|
56
|
+
#
|
57
|
+
# Argument `pattern` is commonly a regular expression:
|
58
|
+
#
|
59
|
+
# s = 'hello'
|
60
|
+
# s.sub(/[aeiou]/, '*') # => "h*llo"
|
61
|
+
# s.gsub(/[aeiou]/, '*') # => "h*ll*"
|
62
|
+
# s.gsub(/[aeiou]/, '') # => "hll"
|
63
|
+
# s.sub(/ell/, 'al') # => "halo"
|
64
|
+
# s.gsub(/xyzzy/, '*') # => "hello"
|
65
|
+
# 'THX1138'.gsub(/\d+/, '00') # => "THX00"
|
66
|
+
#
|
67
|
+
# When `pattern` is a string, all its characters are treated as ordinary
|
68
|
+
# characters (not as regexp special characters):
|
69
|
+
#
|
70
|
+
# 'THX1138'.gsub('\d+', '00') # => "THX1138"
|
71
|
+
#
|
72
|
+
# **\String `replacement`**
|
73
|
+
#
|
74
|
+
# If `replacement` is a string, that string will determine the replacing string
|
75
|
+
# that is to be substituted for the matched text.
|
76
|
+
#
|
77
|
+
# Each of the examples above uses a simple string as the replacing string.
|
78
|
+
#
|
79
|
+
# String `replacement` may contain back-references to the pattern's captures:
|
80
|
+
#
|
81
|
+
# * `\n` (*n* a non-negative integer) refers to `$n`.
|
82
|
+
# * `\k<name>` refers to the named capture `name`.
|
83
|
+
#
|
84
|
+
#
|
85
|
+
# See regexp.rdoc for details.
|
86
|
+
#
|
87
|
+
# Note that within the string `replacement`, a character combination such as
|
88
|
+
# `$&` is treated as ordinary text, and not as a special match variable.
|
89
|
+
# However, you may refer to some special match variables using these
|
90
|
+
# combinations:
|
91
|
+
#
|
92
|
+
# * `\&` and `\0` correspond to `$&`, which contains the complete matched
|
93
|
+
# text.
|
94
|
+
# * `\'` corresponds to `$'`, which contains string after match.
|
95
|
+
# * `\`` corresponds to `$``, which contains string before match.
|
96
|
+
# * `+` corresponds to `$+`, which contains last capture group.
|
97
|
+
#
|
98
|
+
#
|
99
|
+
# See regexp.rdoc for details.
|
100
|
+
#
|
101
|
+
# Note that `\\\` is interpreted as an escape, i.e., a single backslash.
|
102
|
+
#
|
103
|
+
# Note also that a string literal consumes backslashes. See [String
|
104
|
+
# Literals](doc/syntax/literals_rdoc.html#label-String+Literals) for details
|
105
|
+
# about string literals.
|
106
|
+
#
|
107
|
+
# A back-reference is typically preceded by an additional backslash. For
|
108
|
+
# example, if you want to write a back-reference `\&` in `replacement` with a
|
109
|
+
# double-quoted string literal, you need to write `"..\\\\&.."`.
|
110
|
+
#
|
111
|
+
# If you want to write a non-back-reference string `\&` in `replacement`, you
|
112
|
+
# need first to escape the backslash to prevent this method from interpreting it
|
113
|
+
# as a back-reference, and then you need to escape the backslashes again to
|
114
|
+
# prevent a string literal from consuming them: `"..\\\\\\\\&.."`.
|
115
|
+
#
|
116
|
+
# You may want to use the block form to avoid a lot of backslashes.
|
117
|
+
#
|
118
|
+
# **\Hash `replacement`**
|
119
|
+
#
|
120
|
+
# If argument `replacement` is a hash, and `pattern` matches one of its keys,
|
121
|
+
# the replacing string is the value for that key:
|
122
|
+
#
|
123
|
+
# h = {'foo' => 'bar', 'baz' => 'bat'}
|
124
|
+
# 'food'.sub('foo', h) # => "bard"
|
125
|
+
#
|
126
|
+
# Note that a symbol key does not match:
|
127
|
+
#
|
128
|
+
# h = {foo: 'bar', baz: 'bat'}
|
129
|
+
# 'food'.sub('foo', h) # => "d"
|
130
|
+
#
|
131
|
+
# **Block**
|
132
|
+
#
|
133
|
+
# In the block form, the current match string is passed to the block; the
|
134
|
+
# block's return value becomes the replacing string:
|
135
|
+
#
|
136
|
+
# s = '@'
|
137
|
+
# '1234'.gsub(/\d/) {|match| s.succ! } # => "ABCD"
|
138
|
+
#
|
139
|
+
# Special match variables such as `$1`, `$2`, `$``, `$&`, and `$'` are set
|
140
|
+
# appropriately.
|
141
|
+
#
|
142
|
+
# ## What's Here
|
143
|
+
#
|
144
|
+
# First, what's elsewhere. Class String:
|
145
|
+
#
|
146
|
+
# * Inherits from [class
|
147
|
+
# Object](Object.html#class-Object-label-What-27s+Here).
|
148
|
+
# * Includes [module
|
149
|
+
# Comparable](Comparable.html#module-Comparable-label-What-27s+Here).
|
150
|
+
#
|
151
|
+
#
|
152
|
+
# Here, class String provides methods that are useful for:
|
153
|
+
#
|
154
|
+
# * [Creating a String](#class-String-label-Methods+for+Creating+a+String)
|
155
|
+
# * [Frozen/Unfrozen
|
156
|
+
# Strings](#class-String-label-Methods+for+a+Frozen-2FUnfrozen+String)
|
157
|
+
# * [Querying](#class-String-label-Methods+for+Querying)
|
158
|
+
# * [Comparing](#class-String-label-Methods+for+Comparing)
|
159
|
+
# * [Modifying a String](#class-String-label-Methods+for+Modifying+a+String)
|
160
|
+
# * [Converting to New
|
161
|
+
# String](#class-String-label-Methods+for+Converting+to+New+String)
|
162
|
+
# * [Converting to
|
163
|
+
# Non-String](#class-String-label-Methods+for+Converting+to+Non--5CString)
|
164
|
+
# * [Iterating](#class-String-label-Methods+for+Iterating)
|
165
|
+
#
|
166
|
+
#
|
167
|
+
# ### Methods for Creating a String
|
168
|
+
#
|
169
|
+
# ::new
|
170
|
+
# : Returns a new string.
|
171
|
+
#
|
172
|
+
# ::try_convert
|
173
|
+
# : Returns a new string created from a given object.
|
174
|
+
#
|
175
|
+
#
|
176
|
+
#
|
177
|
+
# ### Methods for a Frozen/Unfrozen String
|
178
|
+
#
|
179
|
+
# [#+string](#method-i-2B-40)
|
180
|
+
# : Returns a string that is not frozen: `self`, if not frozen; `self.dup`
|
181
|
+
# otherwise.
|
182
|
+
#
|
183
|
+
# [#-string](#method-i-2D-40)
|
184
|
+
# : Returns a string that is frozen: `self`, if already frozen;
|
185
|
+
# `self.freeze` otherwise.
|
186
|
+
#
|
187
|
+
# #freeze
|
188
|
+
# : Freezes `self`, if not already frozen; returns `self`.
|
189
|
+
#
|
190
|
+
#
|
191
|
+
#
|
192
|
+
# ### Methods for Querying
|
193
|
+
#
|
194
|
+
# *Counts*
|
195
|
+
#
|
196
|
+
# #length, #size
|
197
|
+
# : Returns the count of characters (not bytes).
|
198
|
+
#
|
199
|
+
# #empty?
|
200
|
+
# : Returns `true` if `self.length` is zero; `false` otherwise.
|
201
|
+
#
|
202
|
+
# #bytesize
|
203
|
+
# : Returns the count of bytes.
|
204
|
+
#
|
205
|
+
# #count
|
206
|
+
# : Returns the count of substrings matching given strings.
|
207
|
+
#
|
208
|
+
#
|
209
|
+
#
|
210
|
+
# *Substrings*
|
211
|
+
#
|
212
|
+
# [#=~](#method-i-3D~)
|
213
|
+
# : Returns the index of the first substring that matches a given Regexp
|
214
|
+
# or other object; returns `nil` if no match is found.
|
215
|
+
#
|
216
|
+
# #index
|
217
|
+
# : Returns the index of the *first* occurrence of a given substring;
|
218
|
+
# returns `nil` if none found.
|
219
|
+
#
|
220
|
+
# #rindex
|
221
|
+
# : Returns the index of the *last* occurrence of a given substring;
|
222
|
+
# returns `nil` if none found.
|
223
|
+
#
|
224
|
+
# #include?
|
225
|
+
# : Returns `true` if the string contains a given substring; `false`
|
226
|
+
# otherwise.
|
227
|
+
#
|
228
|
+
# #match
|
229
|
+
# : Returns a MatchData object if the string matches a given Regexp; `nil`
|
230
|
+
# otherwise.
|
231
|
+
#
|
232
|
+
# #match?
|
233
|
+
# : Returns `true` if the string matches a given Regexp; `false`
|
234
|
+
# otherwise.
|
235
|
+
#
|
236
|
+
# #start_with?
|
237
|
+
# : Returns `true` if the string begins with any of the given substrings.
|
238
|
+
#
|
239
|
+
# #end_with?
|
240
|
+
# : Returns `true` if the string ends with any of the given substrings.
|
241
|
+
#
|
242
|
+
#
|
243
|
+
#
|
244
|
+
# *Encodings*
|
245
|
+
#
|
246
|
+
# #encoding
|
247
|
+
# : Returns the Encoding object that represents the encoding of the
|
248
|
+
# string.
|
249
|
+
#
|
250
|
+
# #unicode_normalized?
|
251
|
+
# : Returns `true` if the string is in Unicode normalized form; `false`
|
252
|
+
# otherwise.
|
253
|
+
#
|
254
|
+
# #valid_encoding?
|
255
|
+
# : Returns `true` if the string contains only characters that are valid
|
256
|
+
# for its encoding.
|
257
|
+
#
|
258
|
+
# #ascii_only?
|
259
|
+
# : Returns `true` if the string has only ASCII characters; `false`
|
260
|
+
# otherwise.
|
261
|
+
#
|
262
|
+
#
|
263
|
+
#
|
264
|
+
# *Other*
|
265
|
+
#
|
266
|
+
# #sum
|
267
|
+
# : Returns a basic checksum for the string: the sum of each byte.
|
268
|
+
#
|
269
|
+
# #hash
|
270
|
+
# : Returns the integer hash code.
|
271
|
+
#
|
272
|
+
#
|
273
|
+
#
|
274
|
+
# ### Methods for Comparing
|
275
|
+
#
|
276
|
+
# [#==, #===](#method-i-3D-3D)
|
277
|
+
# : Returns `true` if a given other string has the same content as `self`.
|
278
|
+
#
|
279
|
+
# #eql?
|
280
|
+
# : Returns `true` if the content is the same as the given other string.
|
281
|
+
#
|
282
|
+
# [#<=>](#method-i-3C-3D-3E)
|
283
|
+
# : Returns -1, 0, or 1 as a given other string is smaller than, equal to,
|
284
|
+
# or larger than `self`.
|
285
|
+
#
|
286
|
+
# #casecmp
|
287
|
+
# : Ignoring case, returns -1, 0, or 1 as a given other string is smaller
|
288
|
+
# than, equal to, or larger than `self`.
|
289
|
+
#
|
290
|
+
# #casecmp?
|
291
|
+
# : Returns `true` if the string is equal to a given string after Unicode
|
292
|
+
# case folding; `false` otherwise.
|
293
|
+
#
|
294
|
+
#
|
295
|
+
#
|
296
|
+
# ### Methods for Modifying a String
|
297
|
+
#
|
298
|
+
# Each of these methods modifies `self`.
|
299
|
+
#
|
300
|
+
# *Insertion*
|
301
|
+
#
|
302
|
+
# #insert
|
303
|
+
# : Returns `self` with a given string inserted at a given offset.
|
304
|
+
#
|
305
|
+
# #<<
|
306
|
+
# : Returns `self` concatenated with a given string or integer.
|
307
|
+
#
|
308
|
+
#
|
309
|
+
#
|
310
|
+
# *Substitution*
|
311
|
+
#
|
312
|
+
# #sub!
|
313
|
+
# : Replaces the first substring that matches a given pattern with a given
|
314
|
+
# replacement string; returns `self` if any changes, `nil` otherwise.
|
315
|
+
#
|
316
|
+
# #gsub!
|
317
|
+
# : Replaces each substring that matches a given pattern with a given
|
318
|
+
# replacement string; returns `self` if any changes, `nil` otherwise.
|
319
|
+
#
|
320
|
+
# #succ!, #next!
|
321
|
+
# : Returns `self` modified to become its own successor.
|
322
|
+
#
|
323
|
+
# #replace
|
324
|
+
# : Returns `self` with its entire content replaced by a given string.
|
325
|
+
#
|
326
|
+
# #reverse!
|
327
|
+
# : Returns `self` with its characters in reverse order.
|
328
|
+
#
|
329
|
+
# #setbyte
|
330
|
+
# : Sets the byte at a given integer offset to a given value; returns the
|
331
|
+
# argument.
|
332
|
+
#
|
333
|
+
# #tr!
|
334
|
+
# : Replaces specified characters in `self` with specified replacement
|
335
|
+
# characters; returns `self` if any changes, `nil` otherwise.
|
336
|
+
#
|
337
|
+
# #tr_s!
|
338
|
+
# : Replaces specified characters in `self` with specified replacement
|
339
|
+
# characters, removing duplicates from the substrings that were
|
340
|
+
# modified; returns `self` if any changes, `nil` otherwise.
|
341
|
+
#
|
342
|
+
#
|
343
|
+
#
|
344
|
+
# *Casing*
|
345
|
+
#
|
346
|
+
# #capitalize!
|
347
|
+
# : Upcases the initial character and downcases all others; returns `self`
|
348
|
+
# if any changes, `nil` otherwise.
|
349
|
+
#
|
350
|
+
# #downcase!
|
351
|
+
# : Downcases all characters; returns `self` if any changes, `nil`
|
352
|
+
# otherwise.
|
353
|
+
#
|
354
|
+
# #upcase!
|
355
|
+
# : Upcases all characters; returns `self` if any changes, `nil`
|
356
|
+
# otherwise.
|
357
|
+
#
|
358
|
+
# #swapcase!
|
359
|
+
# : Upcases each downcase character and downcases each upcase character;
|
360
|
+
# returns `self` if any changes, `nil` otherwise.
|
361
|
+
#
|
362
|
+
#
|
363
|
+
#
|
364
|
+
# *Encoding*
|
365
|
+
#
|
366
|
+
# #encode!
|
367
|
+
# : Returns `self` with all characters transcoded from one given encoding
|
368
|
+
# into another.
|
369
|
+
#
|
370
|
+
# #unicode_normalize!
|
371
|
+
# : Unicode-normalizes `self`; returns `self`.
|
372
|
+
#
|
373
|
+
# #scrub!
|
374
|
+
# : Replaces each invalid byte with a given character; returns `self`.
|
375
|
+
#
|
376
|
+
# #force_encoding
|
377
|
+
# : Changes the encoding to a given encoding; returns `self`.
|
378
|
+
#
|
379
|
+
#
|
380
|
+
#
|
381
|
+
# *Deletion*
|
382
|
+
#
|
383
|
+
# #clear
|
384
|
+
# : Removes all content, so that `self` is empty; returns `self`.
|
385
|
+
#
|
386
|
+
# #slice!, #[]=
|
387
|
+
# : Removes a substring determined by a given index, start/length, range,
|
388
|
+
# regexp, or substring.
|
389
|
+
#
|
390
|
+
# #squeeze!
|
391
|
+
# : Removes contiguous duplicate characters; returns `self`.
|
392
|
+
#
|
393
|
+
# #delete!
|
394
|
+
# : Removes characters as determined by the intersection of substring
|
395
|
+
# arguments.
|
396
|
+
#
|
397
|
+
# #lstrip!
|
398
|
+
# : Removes leading whitespace; returns `self` if any changes, `nil`
|
399
|
+
# otherwise.
|
400
|
+
#
|
401
|
+
# #rstrip!
|
402
|
+
# : Removes trailing whitespace; returns `self` if any changes, `nil`
|
403
|
+
# otherwise.
|
404
|
+
#
|
405
|
+
# #strip!
|
406
|
+
# : Removes leading and trailing whitespace; returns `self` if any
|
407
|
+
# changes, `nil` otherwise.
|
408
|
+
#
|
409
|
+
# #chomp!
|
410
|
+
# : Removes trailing record separator, if found; returns `self` if any
|
411
|
+
# changes, `nil` otherwise.
|
412
|
+
#
|
413
|
+
# #chop!
|
414
|
+
# : Removes trailing whitespace if found, otherwise removes the last
|
415
|
+
# character; returns `self` if any changes, `nil` otherwise.
|
416
|
+
#
|
417
|
+
#
|
418
|
+
#
|
419
|
+
# ### Methods for Converting to New String
|
420
|
+
#
|
421
|
+
# Each of these methods returns a new String based on `self`, often just a
|
422
|
+
# modified copy of `self`.
|
423
|
+
#
|
424
|
+
# *Extension*
|
425
|
+
#
|
426
|
+
# #*
|
427
|
+
# : Returns the concatenation of multiple copies of `self`,
|
428
|
+
#
|
429
|
+
# #+
|
430
|
+
# : Returns the concatenation of `self` and a given other string.
|
431
|
+
#
|
432
|
+
# #center
|
433
|
+
# : Returns a copy of `self` centered between pad substring.
|
434
|
+
#
|
435
|
+
# #concat
|
436
|
+
# : Returns the concatenation of `self` with given other strings.
|
437
|
+
#
|
438
|
+
# #prepend
|
439
|
+
# : Returns the concatenation of a given other string with `self`.
|
440
|
+
#
|
441
|
+
# #ljust
|
442
|
+
# : Returns a copy of `self` of a given length, right-padded with a given
|
443
|
+
# other string.
|
444
|
+
#
|
445
|
+
# #rjust
|
446
|
+
# : Returns a copy of `self` of a given length, left-padded with a given
|
447
|
+
# other string.
|
448
|
+
#
|
449
|
+
#
|
450
|
+
#
|
451
|
+
# *Encoding*
|
452
|
+
#
|
453
|
+
# #b
|
454
|
+
# : Returns a copy of `self` with ASCII-8BIT encoding.
|
455
|
+
#
|
456
|
+
# #scrub
|
457
|
+
# : Returns a copy of `self` with each invalid byte replaced with a given
|
458
|
+
# character.
|
459
|
+
#
|
460
|
+
# #unicode_normalize
|
461
|
+
# : Returns a copy of `self` with each character Unicode-normalized.
|
462
|
+
#
|
463
|
+
# #encode
|
464
|
+
# : Returns a copy of `self` with all characters transcoded from one given
|
465
|
+
# encoding into another.
|
466
|
+
#
|
467
|
+
#
|
468
|
+
#
|
469
|
+
# *Substitution*
|
470
|
+
#
|
471
|
+
# #dump
|
472
|
+
# : Returns a copy of +self with all non-printing characters replaced by
|
473
|
+
# xHH notation and all special characters escaped.
|
474
|
+
#
|
475
|
+
# #undump
|
476
|
+
# : Returns a copy of +self with all `\xNN` notation replace by `\uNNNN`
|
477
|
+
# notation and all escaped characters unescaped.
|
478
|
+
#
|
479
|
+
# #sub
|
480
|
+
# : Returns a copy of `self` with the first substring matching a given
|
481
|
+
# pattern replaced with a given replacement string;.
|
482
|
+
#
|
483
|
+
# #gsub
|
484
|
+
# : Returns a copy of `self` with each substring that matches a given
|
485
|
+
# pattern replaced with a given replacement string.
|
486
|
+
#
|
487
|
+
# #succ, #next
|
488
|
+
# : Returns the string that is the successor to `self`.
|
489
|
+
#
|
490
|
+
# #reverse
|
491
|
+
# : Returns a copy of `self` with its characters in reverse order.
|
492
|
+
#
|
493
|
+
# #tr
|
494
|
+
# : Returns a copy of `self` with specified characters replaced with
|
495
|
+
# specified replacement characters.
|
496
|
+
#
|
497
|
+
# #tr_s
|
498
|
+
# : Returns a copy of `self` with specified characters replaced with
|
499
|
+
# specified replacement characters, removing duplicates from the
|
500
|
+
# substrings that were modified.
|
501
|
+
#
|
502
|
+
# #%
|
503
|
+
# : Returns the string resulting from formatting a given object into
|
504
|
+
# `self`
|
505
|
+
#
|
506
|
+
#
|
507
|
+
#
|
508
|
+
# *Casing*
|
509
|
+
#
|
510
|
+
# #capitalize
|
511
|
+
# : Returns a copy of `self` with the first character upcased and all
|
512
|
+
# other characters downcased.
|
513
|
+
#
|
514
|
+
# #downcase
|
515
|
+
# : Returns a copy of `self` with all characters downcased.
|
516
|
+
#
|
517
|
+
# #upcase
|
518
|
+
# : Returns a copy of `self` with all characters upcased.
|
519
|
+
#
|
520
|
+
# #swapcase
|
521
|
+
# : Returns a copy of `self` with all upcase characters downcased and all
|
522
|
+
# downcase characters upcased.
|
523
|
+
#
|
524
|
+
#
|
525
|
+
#
|
526
|
+
# *Deletion*
|
527
|
+
#
|
528
|
+
# #delete
|
529
|
+
# : Returns a copy of `self` with characters removed
|
530
|
+
#
|
531
|
+
# #delete_prefix
|
532
|
+
# : Returns a copy of `self` with a given prefix removed.
|
533
|
+
#
|
534
|
+
# #delete_suffix
|
535
|
+
# : Returns a copy of `self` with a given suffix removed.
|
536
|
+
#
|
537
|
+
# #lstrip
|
538
|
+
# : Returns a copy of `self` with leading whitespace removed.
|
539
|
+
#
|
540
|
+
# #rstrip
|
541
|
+
# : Returns a copy of `self` with trailing whitespace removed.
|
542
|
+
#
|
543
|
+
# #strip
|
544
|
+
# : Returns a copy of `self` with leading and trailing whitespace removed.
|
545
|
+
#
|
546
|
+
# #chomp
|
547
|
+
# : Returns a copy of `self` with a trailing record separator removed, if
|
548
|
+
# found.
|
549
|
+
#
|
550
|
+
# #chop
|
551
|
+
# : Returns a copy of `self` with trailing whitespace or the last
|
552
|
+
# character removed.
|
553
|
+
#
|
554
|
+
# #squeeze
|
555
|
+
# : Returns a copy of `self` with contiguous duplicate characters removed.
|
556
|
+
#
|
557
|
+
# #[], #slice
|
558
|
+
# : Returns a substring determined by a given index, start/length, or
|
559
|
+
# range, or string.
|
560
|
+
#
|
561
|
+
# #byteslice
|
562
|
+
# : Returns a substring determined by a given index, start/length, or
|
563
|
+
# range.
|
564
|
+
#
|
565
|
+
# #chr
|
566
|
+
# : Returns the first character.
|
567
|
+
#
|
568
|
+
#
|
569
|
+
#
|
570
|
+
# *Duplication*
|
571
|
+
#
|
572
|
+
# #to_s, $to_str
|
573
|
+
# : If `self` is a subclass of String, returns `self` copied into a
|
574
|
+
# String; otherwise, returns `self`.
|
575
|
+
#
|
576
|
+
#
|
577
|
+
#
|
578
|
+
# ### Methods for Converting to Non-String
|
579
|
+
#
|
580
|
+
# Each of these methods converts the contents of `self` to a non-String.
|
581
|
+
#
|
582
|
+
# *Characters, Bytes, and Clusters*
|
583
|
+
#
|
584
|
+
# #bytes
|
585
|
+
# : Returns an array of the bytes in `self`.
|
586
|
+
#
|
587
|
+
# #chars
|
588
|
+
# : Returns an array of the characters in `self`.
|
589
|
+
#
|
590
|
+
# #codepoints
|
591
|
+
# : Returns an array of the integer ordinals in `self`.
|
592
|
+
#
|
593
|
+
# #getbyte
|
594
|
+
# : Returns an integer byte as determined by a given index.
|
595
|
+
#
|
596
|
+
# #grapheme_clusters
|
597
|
+
# : Returns an array of the grapheme clusters in `self`.
|
598
|
+
#
|
599
|
+
#
|
600
|
+
#
|
601
|
+
# *Splitting*
|
602
|
+
#
|
603
|
+
# #lines
|
604
|
+
# : Returns an array of the lines in `self`, as determined by a given
|
605
|
+
# record separator.
|
606
|
+
#
|
607
|
+
# #partition
|
608
|
+
# : Returns a 3-element array determined by the first substring that
|
609
|
+
# matches a given substring or regexp,
|
610
|
+
#
|
611
|
+
# #rpartition
|
612
|
+
# : Returns a 3-element array determined by the last substring that
|
613
|
+
# matches a given substring or regexp,
|
614
|
+
#
|
615
|
+
# #split
|
616
|
+
# : Returns an array of substrings determined by a given delimiter --
|
617
|
+
# regexp or string -- or, if a block given, passes those substrings to
|
618
|
+
# the block.
|
619
|
+
#
|
620
|
+
#
|
621
|
+
#
|
622
|
+
# *Matching*
|
623
|
+
#
|
624
|
+
# #scan
|
625
|
+
# : Returns an array of substrings matching a given regexp or string, or,
|
626
|
+
# if a block given, passes each matching substring to the block.
|
627
|
+
#
|
628
|
+
# #unpack
|
629
|
+
# : Returns an array of substrings extracted from `self` according to a
|
630
|
+
# given format.
|
631
|
+
#
|
632
|
+
# #unpack1
|
633
|
+
# : Returns the first substring extracted from `self` according to a given
|
634
|
+
# format.
|
635
|
+
#
|
636
|
+
#
|
637
|
+
#
|
638
|
+
# *Numerics*
|
639
|
+
#
|
640
|
+
# #hex
|
641
|
+
# : Returns the integer value of the leading characters, interpreted as
|
642
|
+
# hexadecimal digits.
|
643
|
+
#
|
644
|
+
# #oct
|
645
|
+
# : Returns the integer value of the leading characters, interpreted as
|
646
|
+
# octal digits.
|
647
|
+
#
|
648
|
+
# #ord
|
649
|
+
# : Returns the integer ordinal of the first character in `self`.
|
650
|
+
#
|
651
|
+
# #to_i
|
652
|
+
# : Returns the integer value of leading characters, interpreted as an
|
653
|
+
# integer.
|
654
|
+
#
|
655
|
+
# #to_f
|
656
|
+
# : Returns the floating-point value of leading characters, interpreted as
|
657
|
+
# a floating-point number.
|
658
|
+
#
|
659
|
+
#
|
660
|
+
#
|
661
|
+
# *Strings and Symbols*
|
662
|
+
#
|
663
|
+
# #inspect
|
664
|
+
# : Returns copy of `self`, enclosed in double-quotes, with special
|
665
|
+
# characters escaped.
|
666
|
+
#
|
667
|
+
# #to_sym, #intern
|
668
|
+
# : Returns the symbol corresponding to `self`.
|
669
|
+
#
|
670
|
+
#
|
671
|
+
#
|
672
|
+
# ### Methods for Iterating
|
673
|
+
#
|
674
|
+
# #each_byte
|
675
|
+
# : Calls the given block with each successive byte in `self`.
|
676
|
+
#
|
677
|
+
# #each_char
|
678
|
+
# : Calls the given block with each successive character in `self`.
|
679
|
+
#
|
680
|
+
# #each_codepoint
|
681
|
+
# : Calls the given block with each successive integer codepoint in
|
682
|
+
# `self`.
|
683
|
+
#
|
684
|
+
# #each_grapheme_cluster
|
685
|
+
# : Calls the given block with each successive grapheme cluster in `self`.
|
686
|
+
#
|
687
|
+
# #each_line
|
688
|
+
# : Calls the given block with each successive line in `self`, as
|
689
|
+
# determined by a given record separator.
|
690
|
+
#
|
691
|
+
# #upto
|
692
|
+
# : Calls the given block with each string value returned by successive
|
693
|
+
# calls to #succ.
|
9
694
|
#
|
10
695
|
class String
|
11
696
|
include Comparable
|
12
697
|
|
13
|
-
#
|
14
|
-
#
|
698
|
+
# <!--
|
699
|
+
# rdoc-file=string.c
|
700
|
+
# - String.try_convert(object) -> object, new_string, or nil
|
701
|
+
# -->
|
702
|
+
# If `object` is a String object, returns `object`.
|
703
|
+
#
|
704
|
+
# Otherwise if `object` responds to `:to_str`, calls `object.to_str` and returns
|
705
|
+
# the result.
|
15
706
|
#
|
16
|
-
#
|
17
|
-
#
|
707
|
+
# Returns `nil` if `object` does not respond to `:to_str`.
|
708
|
+
#
|
709
|
+
# Raises an exception unless `object.to_str` returns a String object.
|
18
710
|
#
|
19
711
|
def self.try_convert: (untyped obj) -> String?
|
20
712
|
|
21
713
|
public
|
22
714
|
|
23
|
-
#
|
24
|
-
#
|
25
|
-
#
|
26
|
-
#
|
715
|
+
# <!--
|
716
|
+
# rdoc-file=string.c
|
717
|
+
# - string % object -> new_string
|
718
|
+
# -->
|
719
|
+
# Returns the result of formatting `object` into the format specification `self`
|
720
|
+
# (see Kernel#sprintf for formatting details):
|
721
|
+
#
|
722
|
+
# "%05d" % 123 # => "00123"
|
723
|
+
#
|
724
|
+
# If `self` contains multiple substitutions, `object` must be an Array or Hash
|
725
|
+
# containing the values to be substituted:
|
27
726
|
#
|
28
|
-
# "%
|
29
|
-
# "
|
30
|
-
# "foo = %{foo}" % {
|
727
|
+
# "%-5s: %016x" % [ "ID", self.object_id ] # => "ID : 00002b054ec93168"
|
728
|
+
# "foo = %{foo}" % {foo: 'bar'} # => "foo = bar"
|
729
|
+
# "foo = %{foo}, baz = %{baz}" % {foo: 'bar', baz: 'bat'} # => "foo = bar, baz = bat"
|
31
730
|
#
|
32
731
|
def %: (Hash[Symbol, untyped]) -> String
|
33
732
|
| (Array[untyped]) -> String
|
34
733
|
| (untyped arg) -> String
|
35
734
|
|
36
|
-
#
|
37
|
-
#
|
735
|
+
# <!--
|
736
|
+
# rdoc-file=string.c
|
737
|
+
# - string * integer -> new_string
|
738
|
+
# -->
|
739
|
+
# Returns a new String containing `integer` copies of `self`:
|
38
740
|
#
|
39
|
-
# "Ho! " * 3
|
40
|
-
# "Ho! " * 0
|
741
|
+
# "Ho! " * 3 # => "Ho! Ho! Ho! "
|
742
|
+
# "Ho! " * 0 # => ""
|
41
743
|
#
|
42
744
|
def *: (int n) -> String
|
43
745
|
|
44
|
-
#
|
45
|
-
#
|
746
|
+
# <!--
|
747
|
+
# rdoc-file=string.c
|
748
|
+
# - string + other_string -> new_string
|
749
|
+
# -->
|
750
|
+
# Returns a new String containing `other_string` concatenated to `self`:
|
46
751
|
#
|
47
|
-
# "Hello from " + self.to_s
|
752
|
+
# "Hello from " + self.to_s # => "Hello from main"
|
48
753
|
#
|
49
754
|
def +: (string other_str) -> String
|
50
755
|
|
51
|
-
#
|
756
|
+
# <!--
|
757
|
+
# rdoc-file=string.c
|
758
|
+
# - +string -> new_string or self
|
759
|
+
# -->
|
760
|
+
# Returns `self` if `self` is not frozen.
|
52
761
|
#
|
53
|
-
#
|
762
|
+
# Otherwise. returns `self.dup`, which is not frozen.
|
54
763
|
#
|
55
764
|
def +@: () -> String
|
56
765
|
|
766
|
+
# <!--
|
767
|
+
# rdoc-file=string.c
|
768
|
+
# - -string -> frozen_string
|
769
|
+
# -->
|
57
770
|
# Returns a frozen, possibly pre-existing copy of the string.
|
58
771
|
#
|
59
|
-
# The
|
60
|
-
# variables set on it.
|
772
|
+
# The returned String will be deduplicated as long as it does not have any
|
773
|
+
# instance variables set on it.
|
61
774
|
#
|
62
775
|
def -@: () -> String
|
63
776
|
|
64
|
-
#
|
65
|
-
#
|
777
|
+
# <!--
|
778
|
+
# rdoc-file=string.c
|
779
|
+
# - string << object -> string
|
780
|
+
# -->
|
781
|
+
# Concatenates `object` to `self` and returns `self`:
|
66
782
|
#
|
67
|
-
#
|
68
|
-
#
|
69
|
-
#
|
783
|
+
# s = 'foo'
|
784
|
+
# s << 'bar' # => "foobar"
|
785
|
+
# s # => "foobar"
|
70
786
|
#
|
71
|
-
#
|
787
|
+
# If `object` is an Integer, the value is considered a codepoint and converted
|
788
|
+
# to a character before concatenation:
|
789
|
+
#
|
790
|
+
# s = 'foo'
|
791
|
+
# s << 33 # => "foo!"
|
792
|
+
#
|
793
|
+
# Related: String#concat, which takes multiple arguments.
|
72
794
|
#
|
73
795
|
def <<: (string | Integer str_or_codepoint) -> String
|
74
796
|
|
75
|
-
#
|
76
|
-
#
|
797
|
+
# <!--
|
798
|
+
# rdoc-file=string.c
|
799
|
+
# - string <=> other_string -> -1, 0, 1, or nil
|
800
|
+
# -->
|
801
|
+
# Compares `self` and `other_string`, returning:
|
77
802
|
#
|
78
|
-
# `
|
803
|
+
# * -1 if `other_string` is larger.
|
804
|
+
# * 0 if the two are equal.
|
805
|
+
# * 1 if `other_string` is smaller.
|
806
|
+
# * `nil` if the two are incomparable.
|
79
807
|
#
|
80
|
-
# If the strings are of different lengths, and the strings are equal when
|
81
|
-
# compared up to the shortest length, then the longer string is considered
|
82
|
-
# greater than the shorter one.
|
83
808
|
#
|
84
|
-
#
|
85
|
-
# included from module Comparable. The method String#== does not use
|
86
|
-
# Comparable#==.
|
809
|
+
# Examples:
|
87
810
|
#
|
88
|
-
#
|
89
|
-
#
|
90
|
-
#
|
91
|
-
#
|
92
|
-
#
|
811
|
+
# 'foo' <=> 'foo' # => 0
|
812
|
+
# 'foo' <=> 'food' # => -1
|
813
|
+
# 'food' <=> 'foo' # => 1
|
814
|
+
# 'FOO' <=> 'foo' # => -1
|
815
|
+
# 'foo' <=> 'FOO' # => 1
|
816
|
+
# 'foo' <=> 1 # => nil
|
93
817
|
#
|
94
818
|
def <=>: (string other) -> Integer
|
95
819
|
| (untyped other) -> Integer?
|
96
820
|
|
97
|
-
#
|
821
|
+
# <!--
|
822
|
+
# rdoc-file=string.c
|
823
|
+
# - string == object -> true or false
|
824
|
+
# - string === object -> true or false
|
825
|
+
# -->
|
826
|
+
# Returns `true` if `object` has the same length and content; as `self`; `false`
|
827
|
+
# otherwise:
|
98
828
|
#
|
99
|
-
#
|
100
|
-
#
|
829
|
+
# s = 'foo'
|
830
|
+
# s == 'foo' # => true
|
831
|
+
# s == 'food' # => false
|
832
|
+
# s == 'FOO' # => false
|
101
833
|
#
|
102
|
-
#
|
834
|
+
# Returns `false` if the two strings' encodings are not compatible:
|
835
|
+
# "\u{e4 f6 fc}".encode("ISO-8859-1") == ("\u{c4 d6 dc}") # => false
|
836
|
+
#
|
837
|
+
# If `object` is not an instance of String but responds to `to_str`, then the
|
838
|
+
# two strings are compared using `object.==`.
|
103
839
|
#
|
104
840
|
def ==: (untyped obj) -> bool
|
105
841
|
|
106
|
-
#
|
842
|
+
# <!-- rdoc-file=string.c -->
|
843
|
+
# Returns `true` if `object` has the same length and content; as `self`; `false`
|
844
|
+
# otherwise:
|
845
|
+
#
|
846
|
+
# s = 'foo'
|
847
|
+
# s == 'foo' # => true
|
848
|
+
# s == 'food' # => false
|
849
|
+
# s == 'FOO' # => false
|
107
850
|
#
|
108
|
-
#
|
109
|
-
#
|
851
|
+
# Returns `false` if the two strings' encodings are not compatible:
|
852
|
+
# "\u{e4 f6 fc}".encode("ISO-8859-1") == ("\u{c4 d6 dc}") # => false
|
110
853
|
#
|
111
|
-
#
|
854
|
+
# If `object` is not an instance of String but responds to `to_str`, then the
|
855
|
+
# two strings are compared using `object.==`.
|
112
856
|
#
|
113
857
|
def ===: (untyped obj) -> bool
|
114
858
|
|
115
|
-
#
|
116
|
-
#
|
117
|
-
#
|
118
|
-
#
|
859
|
+
# <!--
|
860
|
+
# rdoc-file=string.c
|
861
|
+
# - string =~ regexp -> integer or nil
|
862
|
+
# - string =~ object -> integer or nil
|
863
|
+
# -->
|
864
|
+
# Returns the Integer index of the first substring that matches the given
|
865
|
+
# `regexp`, or `nil` if no match found:
|
119
866
|
#
|
120
|
-
#
|
121
|
-
#
|
867
|
+
# 'foo' =~ /f/ # => 0
|
868
|
+
# 'foo' =~ /o/ # => 1
|
869
|
+
# 'foo' =~ /x/ # => nil
|
122
870
|
#
|
123
|
-
# Note
|
124
|
-
#
|
125
|
-
# second case.
|
871
|
+
# Note: also updates [Regexp-related global
|
872
|
+
# variables](Regexp.html#class-Regexp-label-Special+global+variables).
|
126
873
|
#
|
874
|
+
# If the given `object` is not a Regexp, returns the value returned by `object
|
875
|
+
# =~ self`.
|
876
|
+
#
|
877
|
+
# Note that `string =~ regexp` is different from `regexp =~ string` (see
|
878
|
+
# [Regexp#=~](https://ruby-doc.org/core-2.7.1/Regexp.html#method-i-3D-7E)):
|
879
|
+
#
|
880
|
+
# number= nil
|
127
881
|
# "no. 9" =~ /(?<number>\d+)/
|
128
|
-
# number
|
882
|
+
# number # => nil (not assigned)
|
129
883
|
# /(?<number>\d+)/ =~ "no. 9"
|
130
|
-
# number
|
884
|
+
# number #=> "9"
|
131
885
|
#
|
132
886
|
def =~: (untyped obj) -> Integer?
|
133
887
|
|
134
|
-
#
|
135
|
-
#
|
136
|
-
#
|
137
|
-
#
|
138
|
-
#
|
888
|
+
# <!--
|
889
|
+
# rdoc-file=string.c
|
890
|
+
# - string[index] -> new_string or nil
|
891
|
+
# - string[start, length] -> new_string or nil
|
892
|
+
# - string[range] -> new_string or nil
|
893
|
+
# - string[regexp, capture = 0] -> new_string or nil
|
894
|
+
# - string[substring] -> new_string or nil
|
895
|
+
# -->
|
896
|
+
# Returns the substring of `self` specified by the arguments.
|
897
|
+
#
|
898
|
+
# When the single Integer argument `index` is given, returns the 1-character
|
899
|
+
# substring found in `self` at offset `index`:
|
900
|
+
#
|
901
|
+
# 'bar'[2] # => "r"
|
902
|
+
#
|
903
|
+
# Counts backward from the end of `self` if `index` is negative:
|
904
|
+
#
|
905
|
+
# 'foo'[-3] # => "f"
|
906
|
+
#
|
907
|
+
# Returns `nil` if `index` is out of range:
|
908
|
+
#
|
909
|
+
# 'foo'[3] # => nil
|
910
|
+
# 'foo'[-4] # => nil
|
911
|
+
#
|
912
|
+
# When the two Integer arguments `start` and `length` are given, returns the
|
913
|
+
# substring of the given `length` found in `self` at offset `start`:
|
914
|
+
#
|
915
|
+
# 'foo'[0, 2] # => "fo"
|
916
|
+
# 'foo'[0, 0] # => ""
|
917
|
+
#
|
918
|
+
# Counts backward from the end of `self` if `start` is negative:
|
919
|
+
#
|
920
|
+
# 'foo'[-2, 2] # => "oo"
|
921
|
+
#
|
922
|
+
# Special case: returns a new empty String if `start` is equal to the length of
|
923
|
+
# `self`:
|
924
|
+
#
|
925
|
+
# 'foo'[3, 2] # => ""
|
926
|
+
#
|
927
|
+
# Returns `nil` if `start` is out of range:
|
928
|
+
#
|
929
|
+
# 'foo'[4, 2] # => nil
|
930
|
+
# 'foo'[-4, 2] # => nil
|
139
931
|
#
|
140
|
-
#
|
141
|
-
# the string. For the `start` and `range` cases the starting index is just
|
142
|
-
# before a character and an index matching the string's size. Additionally, an
|
143
|
-
# empty string is returned when the starting index for a character range is at
|
144
|
-
# the end of the string.
|
932
|
+
# Returns the trailing substring of `self` if `length` is large:
|
145
933
|
#
|
146
|
-
#
|
147
|
-
# negative.
|
934
|
+
# 'foo'[1, 50] # => "oo"
|
148
935
|
#
|
149
|
-
#
|
150
|
-
# a `capture` follows the regular expression, which may be a capture group index
|
151
|
-
# or name, follows the regular expression that component of the MatchData is
|
152
|
-
# returned instead.
|
936
|
+
# Returns `nil` if `length` is negative:
|
153
937
|
#
|
154
|
-
#
|
938
|
+
# 'foo'[0, -1] # => nil
|
155
939
|
#
|
156
|
-
#
|
157
|
-
#
|
940
|
+
# When the single Range argument `range` is given, derives `start` and `length`
|
941
|
+
# values from the given `range`, and returns values as above:
|
158
942
|
#
|
159
|
-
#
|
943
|
+
# * `'foo'[0..1]` is equivalent to `'foo'[0, 2]`.
|
944
|
+
# * `'foo'[0...1]` is equivalent to `'foo'[0, 1]`.
|
160
945
|
#
|
161
|
-
# a[1] #=> "e"
|
162
|
-
# a[2, 3] #=> "llo"
|
163
|
-
# a[2..3] #=> "ll"
|
164
946
|
#
|
165
|
-
#
|
166
|
-
#
|
167
|
-
# a[-4..-2] #=> "her"
|
168
|
-
# a[-2..-4] #=> ""
|
947
|
+
# When the Regexp argument `regexp` is given, and the `capture` argument is `0`,
|
948
|
+
# returns the first matching substring found in `self`, or `nil` if none found:
|
169
949
|
#
|
170
|
-
#
|
171
|
-
#
|
172
|
-
#
|
173
|
-
#
|
950
|
+
# 'foo'[/o/] # => "o"
|
951
|
+
# 'foo'[/x/] # => nil
|
952
|
+
# s = 'hello there'
|
953
|
+
# s[/[aeiou](.)\1/] # => "ell"
|
954
|
+
# s[/[aeiou](.)\1/, 0] # => "ell"
|
174
955
|
#
|
175
|
-
#
|
176
|
-
#
|
177
|
-
#
|
178
|
-
#
|
956
|
+
# If argument `capture` is given and not `0`, it should be either an Integer
|
957
|
+
# capture group index or a String or Symbol capture group name; the method call
|
958
|
+
# returns only the specified capture (see [Regexp
|
959
|
+
# Capturing](Regexp.html#class-Regexp-label-Capturing)):
|
179
960
|
#
|
180
|
-
#
|
181
|
-
#
|
961
|
+
# s = 'hello there'
|
962
|
+
# s[/[aeiou](.)\1/, 1] # => "l"
|
963
|
+
# s[/(?<vowel>[aeiou])(?<non_vowel>[^aeiou])/, "non_vowel"] # => "l"
|
964
|
+
# s[/(?<vowel>[aeiou])(?<non_vowel>[^aeiou])/, :vowel] # => "e"
|
182
965
|
#
|
183
|
-
#
|
184
|
-
#
|
966
|
+
# If an invalid capture group index is given, `nil` is returned. If an invalid
|
967
|
+
# capture group name is given, `IndexError` is raised.
|
968
|
+
#
|
969
|
+
# When the single String argument `substring` is given, returns the substring
|
970
|
+
# from `self` if found, otherwise `nil`:
|
971
|
+
#
|
972
|
+
# 'foo'['oo'] # => "oo"
|
973
|
+
# 'foo'['xx'] # => nil
|
974
|
+
#
|
975
|
+
# String#slice is an alias for String#[].
|
185
976
|
#
|
186
977
|
def []: (int index) -> String?
|
187
978
|
| (int start, int length) -> String?
|
@@ -190,6 +981,16 @@ class String
|
|
190
981
|
| (Regexp regexp, int | String capture) -> String?
|
191
982
|
| (String match_str) -> String?
|
192
983
|
|
984
|
+
# <!--
|
985
|
+
# rdoc-file=string.c
|
986
|
+
# - str[integer] = new_str
|
987
|
+
# - str[integer, integer] = new_str
|
988
|
+
# - str[range] = aString
|
989
|
+
# - str[regexp] = new_str
|
990
|
+
# - str[regexp, integer] = new_str
|
991
|
+
# - str[regexp, name] = new_str
|
992
|
+
# - str[other_str] = new_str
|
993
|
+
# -->
|
193
994
|
# Element Assignment---Replaces some or all of the content of *str*. The portion
|
194
995
|
# of the string affected is determined using the same criteria as String#[]. If
|
195
996
|
# the replacement string is not the same length as the text it is replacing, the
|
@@ -210,6 +1011,10 @@ class String
|
|
210
1011
|
| (Regexp regexp, String name, String new_str) -> String
|
211
1012
|
| (String other_str, String new_str) -> String
|
212
1013
|
|
1014
|
+
# <!--
|
1015
|
+
# rdoc-file=string.c
|
1016
|
+
# - str.ascii_only? -> true or false
|
1017
|
+
# -->
|
213
1018
|
# Returns true for a string which has only ASCII characters.
|
214
1019
|
#
|
215
1020
|
# "abc".force_encoding("UTF-8").ascii_only? #=> true
|
@@ -217,10 +1022,18 @@ class String
|
|
217
1022
|
#
|
218
1023
|
def ascii_only?: () -> bool
|
219
1024
|
|
1025
|
+
# <!--
|
1026
|
+
# rdoc-file=string.c
|
1027
|
+
# - str.b -> str
|
1028
|
+
# -->
|
220
1029
|
# Returns a copied string whose encoding is ASCII-8BIT.
|
221
1030
|
#
|
222
1031
|
def b: () -> String
|
223
1032
|
|
1033
|
+
# <!--
|
1034
|
+
# rdoc-file=string.c
|
1035
|
+
# - str.bytes -> an_array
|
1036
|
+
# -->
|
224
1037
|
# Returns an array of bytes in *str*. This is a shorthand for
|
225
1038
|
# `str.each_byte.to_a`.
|
226
1039
|
#
|
@@ -230,96 +1043,159 @@ class String
|
|
230
1043
|
def bytes: () -> Array[Integer]
|
231
1044
|
| () { (Integer byte) -> void } -> String
|
232
1045
|
|
233
|
-
#
|
1046
|
+
# <!--
|
1047
|
+
# rdoc-file=string.c
|
1048
|
+
# - bytesize -> integer
|
1049
|
+
# -->
|
1050
|
+
# Returns the count of bytes in `self`:
|
1051
|
+
#
|
1052
|
+
# "\x80\u3042".bytesize # => 4
|
1053
|
+
# "hello".bytesize # => 5
|
234
1054
|
#
|
235
|
-
#
|
236
|
-
# "hello".bytesize #=> 5
|
1055
|
+
# Related: String#length.
|
237
1056
|
#
|
238
1057
|
def bytesize: () -> Integer
|
239
1058
|
|
240
|
-
#
|
241
|
-
#
|
242
|
-
#
|
243
|
-
#
|
244
|
-
#
|
245
|
-
#
|
246
|
-
#
|
247
|
-
#
|
248
|
-
#
|
249
|
-
#
|
250
|
-
#
|
251
|
-
#
|
252
|
-
#
|
253
|
-
#
|
1059
|
+
# <!--
|
1060
|
+
# rdoc-file=string.c
|
1061
|
+
# - byteslice(index, length = 1) -> string or nil
|
1062
|
+
# - byteslice(range) -> string or nil
|
1063
|
+
# -->
|
1064
|
+
# Returns a substring of `self`, or `nil` if the substring cannot be
|
1065
|
+
# constructed.
|
1066
|
+
#
|
1067
|
+
# With integer arguments `index` and `length` given, returns the substring
|
1068
|
+
# beginning at the given `index` of the given `length` (if possible), or `nil`
|
1069
|
+
# if `length` is negative or `index` falls outside of `self`:
|
1070
|
+
#
|
1071
|
+
# s = '0123456789' # => "0123456789"
|
1072
|
+
# s.byteslice(2) # => "2"
|
1073
|
+
# s.byteslice(200) # => nil
|
1074
|
+
# s.byteslice(4, 3) # => "456"
|
1075
|
+
# s.byteslice(4, 30) # => "456789"
|
1076
|
+
# s.byteslice(4, -1) # => nil
|
1077
|
+
# s.byteslice(40, 2) # => nil
|
1078
|
+
#
|
1079
|
+
# In either case above, counts backwards from the end of `self` if `index` is
|
1080
|
+
# negative:
|
1081
|
+
#
|
1082
|
+
# s = '0123456789' # => "0123456789"
|
1083
|
+
# s.byteslice(-4) # => "6"
|
1084
|
+
# s.byteslice(-4, 3) # => "678"
|
1085
|
+
#
|
1086
|
+
# With Range argument `range` given, returns `byteslice(range.begin,
|
1087
|
+
# range.size)`:
|
1088
|
+
#
|
1089
|
+
# s = '0123456789' # => "0123456789"
|
1090
|
+
# s.byteslice(4..6) # => "456"
|
1091
|
+
# s.byteslice(-6..-4) # => "456"
|
1092
|
+
# s.byteslice(5..2) # => "" # range.size is zero.
|
1093
|
+
# s.byteslice(40..42) # => nil
|
1094
|
+
#
|
1095
|
+
# In all cases, a returned string has the same encoding as `self`:
|
1096
|
+
#
|
1097
|
+
# s.encoding # => #<Encoding:UTF-8>
|
1098
|
+
# s.byteslice(4).encoding # => #<Encoding:UTF-8>
|
254
1099
|
#
|
255
1100
|
def byteslice: (int start, ?int length) -> String?
|
256
1101
|
| (Range[Integer] | Range[Integer?] range) -> String?
|
257
1102
|
|
258
|
-
#
|
259
|
-
#
|
1103
|
+
# <!--
|
1104
|
+
# rdoc-file=string.c
|
1105
|
+
# - capitalize(*options) -> string
|
1106
|
+
# -->
|
1107
|
+
# Returns a string containing the characters in `self`; the first character is
|
1108
|
+
# upcased; the remaining characters are downcased:
|
1109
|
+
#
|
1110
|
+
# s = 'hello World!' # => "hello World!"
|
1111
|
+
# s.capitalize # => "Hello world!"
|
260
1112
|
#
|
261
|
-
#
|
1113
|
+
# The casing may be affected by the given `options`; see [Case
|
1114
|
+
# Mapping](doc/case_mapping_rdoc.html).
|
262
1115
|
#
|
263
|
-
#
|
264
|
-
# "HELLO".capitalize #=> "Hello"
|
265
|
-
# "123ABC".capitalize #=> "123abc"
|
1116
|
+
# Related: String#capitalize!.
|
266
1117
|
#
|
267
1118
|
def capitalize: () -> String
|
268
1119
|
| (:ascii | :lithuanian | :turkic) -> String
|
269
1120
|
| (:lithuanian, :turkic) -> String
|
270
1121
|
| (:turkic, :lithuanian) -> String
|
271
1122
|
|
272
|
-
#
|
273
|
-
#
|
274
|
-
#
|
275
|
-
#
|
1123
|
+
# <!--
|
1124
|
+
# rdoc-file=string.c
|
1125
|
+
# - capitalize!(*options) -> self or nil
|
1126
|
+
# -->
|
1127
|
+
# Upcases the first character in `self`; downcases the remaining characters;
|
1128
|
+
# returns `self` if any changes were made, `nil` otherwise:
|
1129
|
+
#
|
1130
|
+
# s = 'hello World!' # => "hello World!"
|
1131
|
+
# s.capitalize! # => "Hello world!"
|
1132
|
+
# s # => "Hello world!"
|
1133
|
+
# s.capitalize! # => nil
|
276
1134
|
#
|
277
|
-
#
|
1135
|
+
# The casing may be affected by the given `options`; see [Case
|
1136
|
+
# Mapping](doc/case_mapping_rdoc.html).
|
278
1137
|
#
|
279
|
-
#
|
280
|
-
# a.capitalize! #=> "Hello"
|
281
|
-
# a #=> "Hello"
|
282
|
-
# a.capitalize! #=> nil
|
1138
|
+
# Related: String#capitalize.
|
283
1139
|
#
|
284
1140
|
def capitalize!: () -> String?
|
285
1141
|
| (:ascii | :lithuanian | :turkic) -> String?
|
286
1142
|
| (:lithuanian, :turkic) -> String?
|
287
1143
|
| (:turkic, :lithuanian) -> String?
|
288
1144
|
|
289
|
-
#
|
290
|
-
#
|
291
|
-
#
|
1145
|
+
# <!--
|
1146
|
+
# rdoc-file=string.c
|
1147
|
+
# - casecmp(other_string) -> -1, 0, 1, or nil
|
1148
|
+
# -->
|
1149
|
+
# Compares `self.downcase` and `other_string.downcase`; returns:
|
292
1150
|
#
|
293
|
-
#
|
294
|
-
#
|
295
|
-
#
|
296
|
-
#
|
1151
|
+
# * -1 if `other_string.downcase` is larger.
|
1152
|
+
# * 0 if the two are equal.
|
1153
|
+
# * 1 if `other_string.downcase` is smaller.
|
1154
|
+
# * `nil` if the two are incomparable.
|
297
1155
|
#
|
298
|
-
# `nil` is returned if the two strings have incompatible encodings, or if
|
299
|
-
# `other_str` is not a string.
|
300
1156
|
#
|
301
|
-
#
|
302
|
-
#
|
1157
|
+
# Examples:
|
1158
|
+
#
|
1159
|
+
# 'foo'.casecmp('foo') # => 0
|
1160
|
+
# 'foo'.casecmp('food') # => -1
|
1161
|
+
# 'food'.casecmp('foo') # => 1
|
1162
|
+
# 'FOO'.casecmp('foo') # => 0
|
1163
|
+
# 'foo'.casecmp('FOO') # => 0
|
1164
|
+
# 'foo'.casecmp(1) # => nil
|
1165
|
+
#
|
1166
|
+
# See [Case Mapping](doc/case_mapping_rdoc.html).
|
1167
|
+
#
|
1168
|
+
# Related: String#casecmp?.
|
303
1169
|
#
|
304
1170
|
def casecmp: (untyped other) -> Integer?
|
305
1171
|
|
306
|
-
#
|
307
|
-
#
|
1172
|
+
# <!--
|
1173
|
+
# rdoc-file=string.c
|
1174
|
+
# - casecmp?(other_string) -> true, false, or nil
|
1175
|
+
# -->
|
1176
|
+
# Returns `true` if `self` and `other_string` are equal after Unicode case
|
1177
|
+
# folding, otherwise `false`:
|
1178
|
+
#
|
1179
|
+
# 'foo'.casecmp?('foo') # => true
|
1180
|
+
# 'foo'.casecmp?('food') # => false
|
1181
|
+
# 'food'.casecmp?('foo') # => false
|
1182
|
+
# 'FOO'.casecmp?('foo') # => true
|
1183
|
+
# 'foo'.casecmp?('FOO') # => true
|
308
1184
|
#
|
309
|
-
#
|
310
|
-
# "aBcDeF".casecmp?("abcdef") #=> true
|
311
|
-
# "aBcDeF".casecmp?("abcdefg") #=> false
|
312
|
-
# "abcdef".casecmp?("ABCDEF") #=> true
|
313
|
-
# "\u{e4 f6 fc}".casecmp?("\u{c4 d6 dc}") #=> true
|
1185
|
+
# Returns `nil` if the two values are incomparable:
|
314
1186
|
#
|
315
|
-
#
|
316
|
-
# `other_str` is not a string.
|
1187
|
+
# 'foo'.casecmp?(1) # => nil
|
317
1188
|
#
|
318
|
-
#
|
319
|
-
#
|
1189
|
+
# See [Case Mapping](doc/case_mapping_rdoc.html).
|
1190
|
+
#
|
1191
|
+
# Related: String#casecmp.
|
320
1192
|
#
|
321
1193
|
def casecmp?: (untyped other) -> bool?
|
322
1194
|
|
1195
|
+
# <!--
|
1196
|
+
# rdoc-file=string.c
|
1197
|
+
# - str.center(width, padstr=' ') -> new_str
|
1198
|
+
# -->
|
323
1199
|
# Centers `str` in `width`. If `width` is greater than the length of `str`,
|
324
1200
|
# returns a new String of length `width` with `str` centered and padded with
|
325
1201
|
# `padstr`; otherwise, returns `str`.
|
@@ -330,6 +1206,10 @@ class String
|
|
330
1206
|
#
|
331
1207
|
def center: (int width, ?string padstr) -> String
|
332
1208
|
|
1209
|
+
# <!--
|
1210
|
+
# rdoc-file=string.c
|
1211
|
+
# - str.chars -> an_array
|
1212
|
+
# -->
|
333
1213
|
# Returns an array of characters in *str*. This is a shorthand for
|
334
1214
|
# `str.each_char.to_a`.
|
335
1215
|
#
|
@@ -339,9 +1219,13 @@ class String
|
|
339
1219
|
def chars: () -> Array[String]
|
340
1220
|
| () { (String char) -> void } -> String
|
341
1221
|
|
1222
|
+
# <!--
|
1223
|
+
# rdoc-file=string.c
|
1224
|
+
# - str.chomp(separator=$/) -> new_str
|
1225
|
+
# -->
|
342
1226
|
# Returns a new String with the given record separator removed from the end of
|
343
1227
|
# *str* (if present). If `$/` has not been changed from the default Ruby record
|
344
|
-
# separator, then `chomp` also removes carriage return characters (that is it
|
1228
|
+
# separator, then `chomp` also removes carriage return characters (that is, it
|
345
1229
|
# will remove `\n`, `\r`, and `\r\n`). If `$/` is an empty string, it will
|
346
1230
|
# remove all trailing newlines from the string.
|
347
1231
|
#
|
@@ -357,11 +1241,19 @@ class String
|
|
357
1241
|
#
|
358
1242
|
def chomp: (?string separator) -> String
|
359
1243
|
|
1244
|
+
# <!--
|
1245
|
+
# rdoc-file=string.c
|
1246
|
+
# - str.chomp!(separator=$/) -> str or nil
|
1247
|
+
# -->
|
360
1248
|
# Modifies *str* in place as described for String#chomp, returning *str*, or
|
361
1249
|
# `nil` if no modifications were made.
|
362
1250
|
#
|
363
1251
|
def chomp!: (?string separator) -> String?
|
364
1252
|
|
1253
|
+
# <!--
|
1254
|
+
# rdoc-file=string.c
|
1255
|
+
# - str.chop -> new_str
|
1256
|
+
# -->
|
365
1257
|
# Returns a new String with the last character removed. If the string ends with
|
366
1258
|
# `\r\n`, both characters are removed. Applying `chop` to an empty string
|
367
1259
|
# returns an empty string. String#chomp is often a safer alternative, as it
|
@@ -375,25 +1267,41 @@ class String
|
|
375
1267
|
#
|
376
1268
|
def chop: () -> String
|
377
1269
|
|
1270
|
+
# <!--
|
1271
|
+
# rdoc-file=string.c
|
1272
|
+
# - str.chop! -> str or nil
|
1273
|
+
# -->
|
378
1274
|
# Processes *str* as for String#chop, returning *str*, or `nil` if *str* is the
|
379
1275
|
# empty string. See also String#chomp!.
|
380
1276
|
#
|
381
1277
|
def chop!: () -> String?
|
382
1278
|
|
383
|
-
#
|
1279
|
+
# <!--
|
1280
|
+
# rdoc-file=string.c
|
1281
|
+
# - chr -> string
|
1282
|
+
# -->
|
1283
|
+
# Returns a string containing the first character of `self`:
|
384
1284
|
#
|
385
|
-
#
|
386
|
-
#
|
1285
|
+
# s = 'foo' # => "foo"
|
1286
|
+
# s.chr # => "f"
|
387
1287
|
#
|
388
1288
|
def chr: () -> String
|
389
1289
|
|
390
|
-
#
|
1290
|
+
# <!--
|
1291
|
+
# rdoc-file=string.c
|
1292
|
+
# - clear -> self
|
1293
|
+
# -->
|
1294
|
+
# Removes the contents of `self`:
|
391
1295
|
#
|
392
|
-
#
|
393
|
-
#
|
1296
|
+
# s = 'foo' # => "foo"
|
1297
|
+
# s.clear # => ""
|
394
1298
|
#
|
395
1299
|
def clear: () -> String
|
396
1300
|
|
1301
|
+
# <!--
|
1302
|
+
# rdoc-file=string.c
|
1303
|
+
# - str.codepoints -> an_array
|
1304
|
+
# -->
|
397
1305
|
# Returns an array of the Integer ordinals of the characters in *str*. This is
|
398
1306
|
# a shorthand for `str.each_codepoint.to_a`.
|
399
1307
|
#
|
@@ -403,23 +1311,30 @@ class String
|
|
403
1311
|
def codepoints: () -> ::Array[Integer]
|
404
1312
|
| () { (Integer codepoint) -> void } -> String
|
405
1313
|
|
406
|
-
#
|
407
|
-
#
|
1314
|
+
# <!--
|
1315
|
+
# rdoc-file=string.c
|
1316
|
+
# - concat(*objects) -> string
|
1317
|
+
# -->
|
1318
|
+
# Concatenates each object in `objects` to `self` and returns `self`:
|
408
1319
|
#
|
409
|
-
#
|
410
|
-
#
|
1320
|
+
# s = 'foo'
|
1321
|
+
# s.concat('bar', 'baz') # => "foobarbaz"
|
1322
|
+
# s # => "foobarbaz"
|
411
1323
|
#
|
412
|
-
#
|
413
|
-
#
|
414
|
-
# a #=> "hello world!"
|
1324
|
+
# For each given object `object` that is an Integer, the value is considered a
|
1325
|
+
# codepoint and converted to a character before concatenation:
|
415
1326
|
#
|
416
|
-
#
|
417
|
-
#
|
1327
|
+
# s = 'foo'
|
1328
|
+
# s.concat(32, 'bar', 32, 'baz') # => "foo bar baz"
|
418
1329
|
#
|
419
|
-
#
|
1330
|
+
# Related: String#<<, which takes a single argument.
|
420
1331
|
#
|
421
1332
|
def concat: (*string | Integer str_or_codepoint) -> String
|
422
1333
|
|
1334
|
+
# <!--
|
1335
|
+
# rdoc-file=string.c
|
1336
|
+
# - str.count([other_str]+) -> integer
|
1337
|
+
# -->
|
423
1338
|
# Each `other_str` parameter defines a set of characters to count. The
|
424
1339
|
# intersection of these sets defines the characters to count in `str`. Any
|
425
1340
|
# `other_str` that starts with a caret `^` is negated. The sequence `c1-c2`
|
@@ -443,6 +1358,10 @@ class String
|
|
443
1358
|
#
|
444
1359
|
def count: (string other_str, *string other_strs) -> Integer
|
445
1360
|
|
1361
|
+
# <!--
|
1362
|
+
# rdoc-file=string.c
|
1363
|
+
# - str.crypt(salt_str) -> new_str
|
1364
|
+
# -->
|
446
1365
|
# Returns the string generated by calling `crypt(3)` standard library function
|
447
1366
|
# with `str` and `salt_str`, in this order, as its arguments. Please do not use
|
448
1367
|
# this method any longer. It is legacy; provided only for backward
|
@@ -477,9 +1396,9 @@ class String
|
|
477
1396
|
#
|
478
1397
|
# * Even in the "modular" mode, some hash functions are considered archaic and
|
479
1398
|
# no longer recommended at all; for instance module `$1$` is officially
|
480
|
-
# abandoned by its author: see http://phk.freebsd.dk/sagas/md5crypt_eol.
|
481
|
-
#
|
482
|
-
#
|
1399
|
+
# abandoned by its author: see http://phk.freebsd.dk/sagas/md5crypt_eol/ .
|
1400
|
+
# For another instance module `$3$` is considered completely broken: see the
|
1401
|
+
# manpage of FreeBSD.
|
483
1402
|
#
|
484
1403
|
# * On some OS such as Mac OS, there is no modular mode. Yet, as written
|
485
1404
|
# above, `crypt(3)` on Mac OS never fails. This means even if you build up a
|
@@ -495,6 +1414,10 @@ class String
|
|
495
1414
|
#
|
496
1415
|
def crypt: (string salt_str) -> String
|
497
1416
|
|
1417
|
+
# <!--
|
1418
|
+
# rdoc-file=string.c
|
1419
|
+
# - str.delete([other_str]+) -> new_str
|
1420
|
+
# -->
|
498
1421
|
# Returns a copy of *str* with all characters in the intersection of its
|
499
1422
|
# arguments deleted. Uses the same rules for building the set of characters as
|
500
1423
|
# String#count.
|
@@ -506,11 +1429,19 @@ class String
|
|
506
1429
|
#
|
507
1430
|
def delete: (string other_str, *string other_strs) -> String
|
508
1431
|
|
1432
|
+
# <!--
|
1433
|
+
# rdoc-file=string.c
|
1434
|
+
# - str.delete!([other_str]+) -> str or nil
|
1435
|
+
# -->
|
509
1436
|
# Performs a `delete` operation in place, returning *str*, or `nil` if *str* was
|
510
1437
|
# not modified.
|
511
1438
|
#
|
512
1439
|
def delete!: (string other_str, *string other_strs) -> String?
|
513
1440
|
|
1441
|
+
# <!--
|
1442
|
+
# rdoc-file=string.c
|
1443
|
+
# - str.delete_prefix(prefix) -> new_str
|
1444
|
+
# -->
|
514
1445
|
# Returns a copy of *str* with leading `prefix` deleted.
|
515
1446
|
#
|
516
1447
|
# "hello".delete_prefix("hel") #=> "lo"
|
@@ -518,6 +1449,10 @@ class String
|
|
518
1449
|
#
|
519
1450
|
def delete_prefix: (string prefix) -> String
|
520
1451
|
|
1452
|
+
# <!--
|
1453
|
+
# rdoc-file=string.c
|
1454
|
+
# - str.delete_prefix!(prefix) -> self or nil
|
1455
|
+
# -->
|
521
1456
|
# Deletes leading `prefix` from *str*, returning `nil` if no change was made.
|
522
1457
|
#
|
523
1458
|
# "hello".delete_prefix!("hel") #=> "lo"
|
@@ -525,6 +1460,10 @@ class String
|
|
525
1460
|
#
|
526
1461
|
def delete_prefix!: (string prefix) -> String?
|
527
1462
|
|
1463
|
+
# <!--
|
1464
|
+
# rdoc-file=string.c
|
1465
|
+
# - str.delete_suffix(suffix) -> new_str
|
1466
|
+
# -->
|
528
1467
|
# Returns a copy of *str* with trailing `suffix` deleted.
|
529
1468
|
#
|
530
1469
|
# "hello".delete_suffix("llo") #=> "he"
|
@@ -532,6 +1471,10 @@ class String
|
|
532
1471
|
#
|
533
1472
|
def delete_suffix: (string suffix) -> String
|
534
1473
|
|
1474
|
+
# <!--
|
1475
|
+
# rdoc-file=string.c
|
1476
|
+
# - str.delete_suffix!(suffix) -> self or nil
|
1477
|
+
# -->
|
535
1478
|
# Deletes trailing `suffix` from *str*, returning `nil` if no change was made.
|
536
1479
|
#
|
537
1480
|
# "hello".delete_suffix!("llo") #=> "he"
|
@@ -539,77 +1482,67 @@ class String
|
|
539
1482
|
#
|
540
1483
|
def delete_suffix!: (string suffix) -> String?
|
541
1484
|
|
542
|
-
#
|
543
|
-
#
|
544
|
-
#
|
545
|
-
#
|
546
|
-
#
|
547
|
-
#
|
548
|
-
#
|
549
|
-
#
|
550
|
-
#
|
551
|
-
#
|
552
|
-
#
|
553
|
-
#
|
554
|
-
# :
|
555
|
-
# : Only the ASCII region, i.e. the characters ``A'' to ``Z'' and ``a'' to
|
556
|
-
# ``z'', are affected. This option cannot be combined with any other option.
|
557
|
-
# :turkic
|
558
|
-
# : Full Unicode case mapping, adapted for Turkic languages (Turkish,
|
559
|
-
# Azerbaijani, ...). This means that upper case I is mapped to lower case
|
560
|
-
# dotless i, and so on.
|
561
|
-
# :lithuanian
|
562
|
-
# : Currently, just full Unicode case mapping. In the future, full Unicode
|
563
|
-
# case mapping adapted for Lithuanian (keeping the dot on the lower case i
|
564
|
-
# even if there is an accent on top).
|
565
|
-
# :fold
|
566
|
-
# : Only available on `downcase` and `downcase!`. Unicode case **folding**,
|
567
|
-
# which is more far-reaching than Unicode case mapping. This option
|
568
|
-
# currently cannot be combined with any other option (i.e. there is
|
569
|
-
# currently no variant for turkic languages).
|
570
|
-
#
|
571
|
-
#
|
572
|
-
# Please note that several assumptions that are valid for ASCII-only case
|
573
|
-
# conversions do not hold for more general case conversions. For example, the
|
574
|
-
# length of the result may not be the same as the length of the input (neither
|
575
|
-
# in characters nor in bytes), some roundtrip assumptions (e.g. str.downcase ==
|
576
|
-
# str.upcase.downcase) may not apply, and Unicode normalization (i.e.
|
577
|
-
# String#unicode_normalize) is not necessarily maintained by case mapping
|
578
|
-
# operations.
|
579
|
-
#
|
580
|
-
# Non-ASCII case mapping/folding is currently supported for UTF-8, UTF-16BE/LE,
|
581
|
-
# UTF-32BE/LE, and ISO-8859-1~16 Strings/Symbols. This support will be extended
|
582
|
-
# to other encodings.
|
583
|
-
#
|
584
|
-
# "hEllO".downcase #=> "hello"
|
1485
|
+
# <!--
|
1486
|
+
# rdoc-file=string.c
|
1487
|
+
# - downcase(*options) -> string
|
1488
|
+
# -->
|
1489
|
+
# Returns a string containing the downcased characters in `self`:
|
1490
|
+
#
|
1491
|
+
# s = 'Hello World!' # => "Hello World!"
|
1492
|
+
# s.downcase # => "hello world!"
|
1493
|
+
#
|
1494
|
+
# The casing may be affected by the given `options`; see [Case
|
1495
|
+
# Mapping](doc/case_mapping_rdoc.html).
|
1496
|
+
#
|
1497
|
+
# Related: String#downcase!, String#upcase, String#upcase!.
|
585
1498
|
#
|
586
1499
|
def downcase: () -> String
|
587
1500
|
| (:ascii | :fold | :lithuanian | :turkic) -> String
|
588
1501
|
| (:lithuanian, :turkic) -> String
|
589
1502
|
| (:turkic, :lithuanian) -> String
|
590
1503
|
|
591
|
-
#
|
1504
|
+
# <!--
|
1505
|
+
# rdoc-file=string.c
|
1506
|
+
# - downcase!(*options) -> self or nil
|
1507
|
+
# -->
|
1508
|
+
# Downcases the characters in `self`; returns `self` if any changes were made,
|
1509
|
+
# `nil` otherwise:
|
1510
|
+
#
|
1511
|
+
# s = 'Hello World!' # => "Hello World!"
|
1512
|
+
# s.downcase! # => "hello world!"
|
1513
|
+
# s # => "hello world!"
|
1514
|
+
# s.downcase! # => nil
|
592
1515
|
#
|
593
|
-
#
|
1516
|
+
# The casing may be affected by the given `options`; see [Case
|
1517
|
+
# Mapping](doc/case_mapping_rdoc.html).
|
1518
|
+
#
|
1519
|
+
# Related: String#downcase, String#upcase, String#upcase!.
|
594
1520
|
#
|
595
1521
|
def downcase!: () -> String?
|
596
1522
|
| (:ascii | :fold | :lithuanian | :turkic) -> String?
|
597
1523
|
| (:lithuanian, :turkic) -> String?
|
598
1524
|
| (:turkic, :lithuanian) -> String?
|
599
1525
|
|
600
|
-
#
|
601
|
-
#
|
602
|
-
#
|
603
|
-
#
|
604
|
-
#
|
1526
|
+
# <!--
|
1527
|
+
# rdoc-file=string.c
|
1528
|
+
# - dump -> string
|
1529
|
+
# -->
|
1530
|
+
# Returns a printable version of `self`, enclosed in double-quotes, with special
|
1531
|
+
# characters escaped, and with non-printing characters replaced by hexadecimal
|
1532
|
+
# notation:
|
605
1533
|
#
|
606
|
-
# "hello \n ''".dump
|
607
|
-
# "\f\x00\xff\\\"".dump
|
1534
|
+
# "hello \n ''".dump # => "\"hello \\n ''\""
|
1535
|
+
# "\f\x00\xff\\\"".dump # => "\"\\f\\x00\\xFF\\\\\\\"\""
|
608
1536
|
#
|
609
|
-
#
|
1537
|
+
# Related: String#undump (inverse of String#dump).
|
610
1538
|
#
|
611
1539
|
def dump: () -> String
|
612
1540
|
|
1541
|
+
# <!--
|
1542
|
+
# rdoc-file=string.c
|
1543
|
+
# - str.each_byte {|integer| block } -> str
|
1544
|
+
# - str.each_byte -> an_enumerator
|
1545
|
+
# -->
|
613
1546
|
# Passes each byte in *str* to the given block, or returns an enumerator if no
|
614
1547
|
# block is given.
|
615
1548
|
#
|
@@ -622,6 +1555,11 @@ class String
|
|
622
1555
|
def each_byte: () { (Integer byte) -> void } -> self
|
623
1556
|
| () -> ::Enumerator[Integer, self]
|
624
1557
|
|
1558
|
+
# <!--
|
1559
|
+
# rdoc-file=string.c
|
1560
|
+
# - str.each_char {|cstr| block } -> str
|
1561
|
+
# - str.each_char -> an_enumerator
|
1562
|
+
# -->
|
625
1563
|
# Passes each character in *str* to the given block, or returns an enumerator if
|
626
1564
|
# no block is given.
|
627
1565
|
#
|
@@ -634,6 +1572,11 @@ class String
|
|
634
1572
|
def each_char: () { (String char) -> void } -> self
|
635
1573
|
| () -> ::Enumerator[String, self]
|
636
1574
|
|
1575
|
+
# <!--
|
1576
|
+
# rdoc-file=string.c
|
1577
|
+
# - str.each_codepoint {|integer| block } -> str
|
1578
|
+
# - str.each_codepoint -> an_enumerator
|
1579
|
+
# -->
|
637
1580
|
# Passes the Integer ordinal of each character in *str*, also known as a
|
638
1581
|
# *codepoint* when applied to Unicode strings to the given block. For encodings
|
639
1582
|
# other than UTF-8/UTF-16(BE|LE)/UTF-32(BE|LE), values are directly derived from
|
@@ -650,6 +1593,11 @@ class String
|
|
650
1593
|
def each_codepoint: () { (Integer codepoint) -> void } -> self
|
651
1594
|
| () -> ::Enumerator[Integer, self]
|
652
1595
|
|
1596
|
+
# <!--
|
1597
|
+
# rdoc-file=string.c
|
1598
|
+
# - str.each_grapheme_cluster {|cstr| block } -> str
|
1599
|
+
# - str.each_grapheme_cluster -> an_enumerator
|
1600
|
+
# -->
|
653
1601
|
# Passes each grapheme cluster in *str* to the given block, or returns an
|
654
1602
|
# enumerator if no block is given. Unlike String#each_char, this enumerates by
|
655
1603
|
# grapheme clusters defined by Unicode Standard Annex #29
|
@@ -661,6 +1609,11 @@ class String
|
|
661
1609
|
def each_grapheme_cluster: () { (String grapheme) -> void } -> self
|
662
1610
|
| () -> ::Enumerator[String, self]
|
663
1611
|
|
1612
|
+
# <!--
|
1613
|
+
# rdoc-file=string.c
|
1614
|
+
# - str.each_line(separator=$/, chomp: false) {|substr| block } -> str
|
1615
|
+
# - str.each_line(separator=$/, chomp: false) -> an_enumerator
|
1616
|
+
# -->
|
664
1617
|
# Splits *str* using the supplied parameter as the record separator (`$/` by
|
665
1618
|
# default), passing each substring in turn to the supplied block. If a
|
666
1619
|
# zero-length record separator is supplied, the string is split into paragraphs
|
@@ -702,14 +1655,24 @@ class String
|
|
702
1655
|
def each_line: (?string separator, ?chomp: boolish) { (String line) -> void } -> self
|
703
1656
|
| (?string separator, ?chomp: boolish) -> Enumerator[String, self]
|
704
1657
|
|
705
|
-
#
|
1658
|
+
# <!--
|
1659
|
+
# rdoc-file=string.c
|
1660
|
+
# - empty? -> true or false
|
1661
|
+
# -->
|
1662
|
+
# Returns `true` if the length of `self` is zero, `false` otherwise:
|
706
1663
|
#
|
707
|
-
# "hello".empty?
|
708
|
-
# " ".empty?
|
709
|
-
# "".empty?
|
1664
|
+
# "hello".empty? # => false
|
1665
|
+
# " ".empty? # => false
|
1666
|
+
# "".empty? # => true
|
710
1667
|
#
|
711
1668
|
def empty?: () -> bool
|
712
1669
|
|
1670
|
+
# <!--
|
1671
|
+
# rdoc-file=transcode.c
|
1672
|
+
# - str.encode(encoding, **options) -> str
|
1673
|
+
# - str.encode(dst_encoding, src_encoding, **options) -> str
|
1674
|
+
# - str.encode(**options) -> str
|
1675
|
+
# -->
|
713
1676
|
# The first form returns a copy of `str` transcoded to encoding `encoding`. The
|
714
1677
|
# second form returns a copy of `str` transcoded from src_encoding to
|
715
1678
|
# dst_encoding. The last form returns a copy of `str` transcoded to
|
@@ -721,8 +1684,8 @@ class String
|
|
721
1684
|
# encoding. The last form by default does not raise exceptions but uses
|
722
1685
|
# replacement strings.
|
723
1686
|
#
|
724
|
-
# The `options`
|
725
|
-
#
|
1687
|
+
# The `options` keyword arguments give details for conversion. The arguments
|
1688
|
+
# are:
|
726
1689
|
#
|
727
1690
|
# :invalid
|
728
1691
|
# : If the value is `:replace`, #encode replaces invalid byte sequences in
|
@@ -754,20 +1717,33 @@ class String
|
|
754
1717
|
# :universal_newline
|
755
1718
|
# : Replaces CRLF ("r\n") and CR ("r") with LF ("n") if value is true.
|
756
1719
|
#
|
757
|
-
#
|
758
1720
|
def encode: (?encoding encoding, ?encoding from_encoding, ?invalid: :replace ?, ?undef: :replace ?, ?replace: String, ?fallback: String::encode_fallback, ?xml: :text | :attr, ?universal_newline: true, ?cr_newline: true, ?crlf_newline: true) -> String
|
759
1721
|
|
1722
|
+
# <!--
|
1723
|
+
# rdoc-file=transcode.c
|
1724
|
+
# - str.encode!(encoding, **options) -> str
|
1725
|
+
# - str.encode!(dst_encoding, src_encoding, **options) -> str
|
1726
|
+
# -->
|
760
1727
|
# The first form transcodes the contents of *str* from str.encoding to
|
761
1728
|
# `encoding`. The second form transcodes the contents of *str* from src_encoding
|
762
|
-
# to dst_encoding. The options
|
763
|
-
# String#encode for details. Returns the string even if no changes were
|
1729
|
+
# to dst_encoding. The `options` keyword arguments give details for conversion.
|
1730
|
+
# See String#encode for details. Returns the string even if no changes were
|
1731
|
+
# made.
|
764
1732
|
#
|
765
1733
|
def encode!: (?encoding encoding, ?encoding from_encoding, ?invalid: :replace ?, ?undef: :replace ?, ?replace: String, ?fallback: String::encode_fallback, ?xml: :text | :attr, ?universal_newline: true, ?cr_newline: true, ?crlf_newline: true) -> self
|
766
1734
|
|
1735
|
+
# <!--
|
1736
|
+
# rdoc-file=string.c
|
1737
|
+
# - obj.encoding -> encoding
|
1738
|
+
# -->
|
767
1739
|
# Returns the Encoding object that represents the encoding of obj.
|
768
1740
|
#
|
769
1741
|
def encoding: () -> Encoding
|
770
1742
|
|
1743
|
+
# <!--
|
1744
|
+
# rdoc-file=string.c
|
1745
|
+
# - str.end_with?([suffixes]+) -> true or false
|
1746
|
+
# -->
|
771
1747
|
# Returns true if `str` ends with one of the `suffixes` given.
|
772
1748
|
#
|
773
1749
|
# "hello".end_with?("ello") #=> true
|
@@ -778,20 +1754,57 @@ class String
|
|
778
1754
|
#
|
779
1755
|
def end_with?: (*string suffixes) -> bool
|
780
1756
|
|
781
|
-
#
|
1757
|
+
# <!--
|
1758
|
+
# rdoc-file=string.c
|
1759
|
+
# - eql?(object) -> true or false
|
1760
|
+
# -->
|
1761
|
+
# Returns `true` if `object` has the same length and content; as `self`; `false`
|
1762
|
+
# otherwise:
|
1763
|
+
#
|
1764
|
+
# s = 'foo'
|
1765
|
+
# s.eql?('foo') # => true
|
1766
|
+
# s.eql?('food') # => false
|
1767
|
+
# s.eql?('FOO') # => false
|
1768
|
+
#
|
1769
|
+
# Returns `false` if the two strings' encodings are not compatible:
|
1770
|
+
#
|
1771
|
+
# "\u{e4 f6 fc}".encode("ISO-8859-1").eql?("\u{c4 d6 dc}") # => false
|
782
1772
|
#
|
783
1773
|
def eql?: (untyped other) -> bool
|
784
1774
|
|
1775
|
+
# <!--
|
1776
|
+
# rdoc-file=string.c
|
1777
|
+
# - str.force_encoding(encoding) -> str
|
1778
|
+
# -->
|
785
1779
|
# Changes the encoding to `encoding` and returns self.
|
786
1780
|
#
|
787
1781
|
def force_encoding: (string | Encoding encoding) -> self
|
788
1782
|
|
1783
|
+
# <!--
|
1784
|
+
# rdoc-file=string.c
|
1785
|
+
# - freeze()
|
1786
|
+
# -->
|
1787
|
+
#
|
789
1788
|
def freeze: () -> self
|
790
1789
|
|
791
|
-
#
|
1790
|
+
# <!--
|
1791
|
+
# rdoc-file=string.c
|
1792
|
+
# - getbyte(index) -> integer
|
1793
|
+
# -->
|
1794
|
+
# Returns the byte at zero-based `index` as an integer:
|
1795
|
+
#
|
1796
|
+
# s = 'abcde' # => "abcde"
|
1797
|
+
# s.getbyte(0) # => 97
|
1798
|
+
# s.getbyte(1) # => 98
|
1799
|
+
#
|
1800
|
+
# Related: String#setbyte.
|
792
1801
|
#
|
793
1802
|
def getbyte: (int index) -> Integer?
|
794
1803
|
|
1804
|
+
# <!--
|
1805
|
+
# rdoc-file=string.c
|
1806
|
+
# - str.grapheme_clusters -> an_array
|
1807
|
+
# -->
|
795
1808
|
# Returns an array of grapheme clusters in *str*. This is a shorthand for
|
796
1809
|
# `str.each_grapheme_cluster.to_a`.
|
797
1810
|
#
|
@@ -800,69 +1813,60 @@ class String
|
|
800
1813
|
#
|
801
1814
|
def grapheme_clusters: () -> ::Array[::String]
|
802
1815
|
|
803
|
-
#
|
804
|
-
#
|
805
|
-
#
|
806
|
-
#
|
807
|
-
#
|
808
|
-
#
|
809
|
-
#
|
810
|
-
#
|
811
|
-
#
|
812
|
-
#
|
813
|
-
#
|
814
|
-
#
|
815
|
-
#
|
816
|
-
# to the current match.
|
817
|
-
#
|
818
|
-
# If the second argument is a Hash, and the matched text is one of its keys, the
|
819
|
-
# corresponding value is the replacement string.
|
820
|
-
#
|
821
|
-
# In the block form, the current match string is passed in as a parameter, and
|
822
|
-
# variables such as `$1`, `$2`, `$``, `$&`, and `$'` will be set appropriately.
|
823
|
-
# (See regexp.rdoc for details.) The value returned by the block will be
|
824
|
-
# substituted for the match on each call.
|
825
|
-
#
|
826
|
-
# When neither a block nor a second argument is supplied, an Enumerator is
|
827
|
-
# returned.
|
828
|
-
#
|
829
|
-
# "hello".gsub(/[aeiou]/, '*') #=> "h*ll*"
|
830
|
-
# "hello".gsub(/([aeiou])/, '<\1>') #=> "h<e>ll<o>"
|
831
|
-
# "hello".gsub(/./) {|s| s.ord.to_s + ' '} #=> "104 101 108 108 111 "
|
832
|
-
# "hello".gsub(/(?<foo>[aeiou])/, '{\k<foo>}') #=> "h{e}ll{o}"
|
833
|
-
# 'hello'.gsub(/[eo]/, 'e' => 3, 'o' => '*') #=> "h3ll*"
|
834
|
-
#
|
835
|
-
# Note that a string literal consumes backslashes. (See syntax/literals.rdoc for
|
836
|
-
# details on string literals.) Back-references are typically preceded by an
|
837
|
-
# additional backslash. For example, if you want to write a back-reference `\&`
|
838
|
-
# in `replacement` with a double-quoted string literal, you need to write:
|
839
|
-
# `"..\\\\&.."`. If you want to write a non-back-reference string `\&` in
|
840
|
-
# `replacement`, you need first to escape the backslash to prevent this method
|
841
|
-
# from interpreting it as a back-reference, and then you need to escape the
|
842
|
-
# backslashes again to prevent a string literal from consuming them:
|
843
|
-
# `"..\\\\\\\\&.."`. You may want to use the block form to avoid a lot of
|
844
|
-
# backslashes.
|
1816
|
+
# <!--
|
1817
|
+
# rdoc-file=string.c
|
1818
|
+
# - gsub(pattern, replacement) -> new_string
|
1819
|
+
# - gsub(pattern) {|match| ... } -> new_string
|
1820
|
+
# - gsub(pattern) -> enumerator
|
1821
|
+
# -->
|
1822
|
+
# Returns a copy of `self` with all occurrences of the given `pattern` replaced.
|
1823
|
+
#
|
1824
|
+
# See [Substitution Methods](#class-String-label-Substitution+Methods).
|
1825
|
+
#
|
1826
|
+
# Returns an Enumerator if no `replacement` and no block given.
|
1827
|
+
#
|
1828
|
+
# Related: String#sub, String#sub!, String#gsub!.
|
845
1829
|
#
|
846
1830
|
def gsub: (Regexp | string pattern, string replacement) -> String
|
847
1831
|
| (Regexp | string pattern, Hash[String, String] hash) -> String
|
848
1832
|
| (Regexp | string pattern) { (String match) -> _ToS } -> String
|
849
1833
|
| (Regexp | string pattern) -> ::Enumerator[String, self]
|
850
1834
|
|
851
|
-
#
|
852
|
-
#
|
853
|
-
#
|
1835
|
+
# <!--
|
1836
|
+
# rdoc-file=string.c
|
1837
|
+
# - gsub!(pattern, replacement) -> self or nil
|
1838
|
+
# - gsub!(pattern) {|match| ... } -> self or nil
|
1839
|
+
# - gsub!(pattern) -> an_enumerator
|
1840
|
+
# -->
|
1841
|
+
# Performs the specified substring replacement(s) on `self`; returns `self` if
|
1842
|
+
# any replacement occurred, `nil` otherwise.
|
1843
|
+
#
|
1844
|
+
# See [Substitution Methods](#class-String-label-Substitution+Methods).
|
1845
|
+
#
|
1846
|
+
# Returns an Enumerator if no `replacement` and no block given.
|
1847
|
+
#
|
1848
|
+
# Related: String#sub, String#gsub, String#sub!.
|
854
1849
|
#
|
855
1850
|
def gsub!: (Regexp | string pattern, string replacement) -> String?
|
856
1851
|
| (Regexp | string pattern, Hash[String, String] hash) -> String?
|
857
1852
|
| (Regexp | string pattern) { (String match) -> _ToS } -> String?
|
858
1853
|
| (Regexp | string pattern) -> ::Enumerator[String, self]
|
859
1854
|
|
860
|
-
#
|
1855
|
+
# <!--
|
1856
|
+
# rdoc-file=string.c
|
1857
|
+
# - hash -> integer
|
1858
|
+
# -->
|
1859
|
+
# Returns the integer hash value for `self`. The value is based on the length,
|
1860
|
+
# content and encoding of `self`.
|
861
1861
|
#
|
862
|
-
#
|
1862
|
+
# Related: Object#hash.
|
863
1863
|
#
|
864
1864
|
def hash: () -> Integer
|
865
1865
|
|
1866
|
+
# <!--
|
1867
|
+
# rdoc-file=string.c
|
1868
|
+
# - str.hex -> integer
|
1869
|
+
# -->
|
866
1870
|
# Treats leading characters from *str* as a string of hexadecimal digits (with
|
867
1871
|
# an optional sign and an optional `0x`) and returns the corresponding number.
|
868
1872
|
# Zero is returned on error.
|
@@ -874,48 +1878,94 @@ class String
|
|
874
1878
|
#
|
875
1879
|
def hex: () -> Integer
|
876
1880
|
|
877
|
-
#
|
1881
|
+
# <!--
|
1882
|
+
# rdoc-file=string.c
|
1883
|
+
# - include? other_string -> true or false
|
1884
|
+
# -->
|
1885
|
+
# Returns `true` if `self` contains `other_string`, `false` otherwise:
|
878
1886
|
#
|
879
|
-
#
|
880
|
-
#
|
881
|
-
#
|
1887
|
+
# s = 'foo'
|
1888
|
+
# s.include?('f') # => true
|
1889
|
+
# s.include?('fo') # => true
|
1890
|
+
# s.include?('food') # => false
|
882
1891
|
#
|
883
1892
|
def include?: (string other_str) -> bool
|
884
1893
|
|
885
|
-
#
|
886
|
-
#
|
887
|
-
#
|
1894
|
+
# <!--
|
1895
|
+
# rdoc-file=string.c
|
1896
|
+
# - index(substring, offset = 0) -> integer or nil
|
1897
|
+
# - index(regexp, offset = 0) -> integer or nil
|
1898
|
+
# -->
|
1899
|
+
# Returns the Integer index of the first occurrence of the given `substring`, or
|
1900
|
+
# `nil` if none found:
|
1901
|
+
#
|
1902
|
+
# 'foo'.index('f') # => 0
|
1903
|
+
# 'foo'.index('o') # => 1
|
1904
|
+
# 'foo'.index('oo') # => 1
|
1905
|
+
# 'foo'.index('ooo') # => nil
|
1906
|
+
#
|
1907
|
+
# Returns the Integer index of the first match for the given Regexp `regexp`, or
|
1908
|
+
# `nil` if none found:
|
1909
|
+
#
|
1910
|
+
# 'foo'.index(/f/) # => 0
|
1911
|
+
# 'foo'.index(/o/) # => 1
|
1912
|
+
# 'foo'.index(/oo/) # => 1
|
1913
|
+
# 'foo'.index(/ooo/) # => nil
|
888
1914
|
#
|
889
|
-
#
|
890
|
-
#
|
891
|
-
#
|
892
|
-
#
|
893
|
-
#
|
1915
|
+
# Integer argument `offset`, if given, specifies the position in the string to
|
1916
|
+
# begin the search:
|
1917
|
+
#
|
1918
|
+
# 'foo'.index('o', 1) # => 1
|
1919
|
+
# 'foo'.index('o', 2) # => 2
|
1920
|
+
# 'foo'.index('o', 3) # => nil
|
1921
|
+
#
|
1922
|
+
# If `offset` is negative, counts backward from the end of `self`:
|
1923
|
+
#
|
1924
|
+
# 'foo'.index('o', -1) # => 2
|
1925
|
+
# 'foo'.index('o', -2) # => 1
|
1926
|
+
# 'foo'.index('o', -3) # => 1
|
1927
|
+
# 'foo'.index('o', -4) # => nil
|
1928
|
+
#
|
1929
|
+
# Related: String#rindex.
|
894
1930
|
#
|
895
1931
|
def index: (Regexp | string substr_or_regexp, ?int offset) -> Integer?
|
896
1932
|
|
897
|
-
#
|
898
|
-
#
|
899
|
-
#
|
900
|
-
#
|
1933
|
+
# <!--
|
1934
|
+
# rdoc-file=string.c
|
1935
|
+
# - insert(index, other_string) -> self
|
1936
|
+
# -->
|
1937
|
+
# Inserts the given `other_string` into `self`; returns `self`.
|
1938
|
+
#
|
1939
|
+
# If the Integer `index` is positive, inserts `other_string` at offset `index`:
|
1940
|
+
#
|
1941
|
+
# 'foo'.insert(1, 'bar') # => "fbaroo"
|
1942
|
+
#
|
1943
|
+
# If the Integer `index` is negative, counts backward from the end of `self` and
|
1944
|
+
# inserts `other_string` at offset `index+1` (that is, *after* `self[index]`):
|
901
1945
|
#
|
902
|
-
#
|
903
|
-
# "abcd".insert(3, 'X') #=> "abcXd"
|
904
|
-
# "abcd".insert(4, 'X') #=> "abcdX"
|
905
|
-
# "abcd".insert(-3, 'X') #=> "abXcd"
|
906
|
-
# "abcd".insert(-1, 'X') #=> "abcdX"
|
1946
|
+
# 'foo'.insert(-2, 'bar') # => "fobaro"
|
907
1947
|
#
|
908
1948
|
def insert: (int index, string other_str) -> String
|
909
1949
|
|
910
|
-
#
|
911
|
-
#
|
1950
|
+
# <!--
|
1951
|
+
# rdoc-file=string.c
|
1952
|
+
# - inspect -> string
|
1953
|
+
# -->
|
1954
|
+
# Returns a printable version of `self`, enclosed in double-quotes, and with
|
1955
|
+
# special characters escaped:
|
912
1956
|
#
|
913
|
-
#
|
914
|
-
#
|
915
|
-
#
|
1957
|
+
# s = "foo\tbar\tbaz\n"
|
1958
|
+
# # => "foo\tbar\tbaz\n"
|
1959
|
+
# s.inspect
|
1960
|
+
# # => "\"foo\\tbar\\tbaz\\n\""
|
916
1961
|
#
|
917
1962
|
def inspect: () -> String
|
918
1963
|
|
1964
|
+
# <!--
|
1965
|
+
# rdoc-file=string.c
|
1966
|
+
# - str.intern -> symbol
|
1967
|
+
# - str.to_sym -> symbol
|
1968
|
+
# -->
|
919
1969
|
# Returns the Symbol corresponding to *str*, creating the symbol if it did not
|
920
1970
|
# previously exist. See Symbol#id2name.
|
921
1971
|
#
|
@@ -932,10 +1982,25 @@ class String
|
|
932
1982
|
#
|
933
1983
|
def intern: () -> Symbol
|
934
1984
|
|
935
|
-
#
|
1985
|
+
# <!--
|
1986
|
+
# rdoc-file=string.c
|
1987
|
+
# - length -> integer
|
1988
|
+
# -->
|
1989
|
+
# Returns the count of characters (not bytes) in `self`:
|
1990
|
+
#
|
1991
|
+
# "\x80\u3042".length # => 2
|
1992
|
+
# "hello".length # => 5
|
1993
|
+
#
|
1994
|
+
# String#size is an alias for String#length.
|
1995
|
+
#
|
1996
|
+
# Related: String#bytesize.
|
936
1997
|
#
|
937
1998
|
def length: () -> Integer
|
938
1999
|
|
2000
|
+
# <!--
|
2001
|
+
# rdoc-file=string.c
|
2002
|
+
# - str.lines(separator=$/, chomp: false) -> an_array
|
2003
|
+
# -->
|
939
2004
|
# Returns an array of lines in *str* split using the supplied record separator
|
940
2005
|
# (`$/` by default). This is a shorthand for `str.each_line(separator,
|
941
2006
|
# getline_args).to_a`.
|
@@ -951,6 +2016,10 @@ class String
|
|
951
2016
|
#
|
952
2017
|
def lines: (?string separator, ?chomp: boolish) -> Array[String]
|
953
2018
|
|
2019
|
+
# <!--
|
2020
|
+
# rdoc-file=string.c
|
2021
|
+
# - str.ljust(integer, padstr=' ') -> new_str
|
2022
|
+
# -->
|
954
2023
|
# If *integer* is greater than the length of *str*, returns a new String of
|
955
2024
|
# length *integer* with *str* left justified and padded with *padstr*;
|
956
2025
|
# otherwise, returns *str*.
|
@@ -961,6 +2030,10 @@ class String
|
|
961
2030
|
#
|
962
2031
|
def ljust: (int integer, ?string padstr) -> String
|
963
2032
|
|
2033
|
+
# <!--
|
2034
|
+
# rdoc-file=string.c
|
2035
|
+
# - str.lstrip -> new_str
|
2036
|
+
# -->
|
964
2037
|
# Returns a copy of the receiver with leading whitespace removed. See also
|
965
2038
|
# String#rstrip and String#strip.
|
966
2039
|
#
|
@@ -971,6 +2044,10 @@ class String
|
|
971
2044
|
#
|
972
2045
|
def lstrip: () -> String
|
973
2046
|
|
2047
|
+
# <!--
|
2048
|
+
# rdoc-file=string.c
|
2049
|
+
# - str.lstrip! -> self or nil
|
2050
|
+
# -->
|
974
2051
|
# Removes leading whitespace from the receiver. Returns the altered receiver, or
|
975
2052
|
# `nil` if no change was made. See also String#rstrip! and String#strip!.
|
976
2053
|
#
|
@@ -982,68 +2059,138 @@ class String
|
|
982
2059
|
#
|
983
2060
|
def lstrip!: () -> self?
|
984
2061
|
|
985
|
-
#
|
986
|
-
#
|
987
|
-
#
|
2062
|
+
# <!--
|
2063
|
+
# rdoc-file=string.c
|
2064
|
+
# - match(pattern, offset = 0) -> matchdata or nil
|
2065
|
+
# - match(pattern, offset = 0) {|matchdata| ... } -> object
|
2066
|
+
# -->
|
2067
|
+
# Returns a Matchdata object (or `nil`) based on `self` and the given `pattern`.
|
2068
|
+
#
|
2069
|
+
# Note: also updates [Regexp-related global
|
2070
|
+
# variables](Regexp.html#class-Regexp-label-Special+global+variables).
|
2071
|
+
#
|
2072
|
+
# * Computes `regexp` by converting `pattern` (if not already a Regexp).
|
2073
|
+
# regexp = Regexp.new(pattern)
|
2074
|
+
#
|
2075
|
+
# * Computes `matchdata`, which will be either a MatchData object or `nil`
|
2076
|
+
# (see Regexp#match):
|
2077
|
+
# matchdata = <tt>regexp.match(self)
|
988
2078
|
#
|
989
|
-
# 'hello'.match('(.)\1') #=> #<MatchData "ll" 1:"l">
|
990
|
-
# 'hello'.match('(.)\1')[0] #=> "ll"
|
991
|
-
# 'hello'.match(/(.)\1/)[0] #=> "ll"
|
992
|
-
# 'hello'.match(/(.)\1/, 3) #=> nil
|
993
|
-
# 'hello'.match('xx') #=> nil
|
994
2079
|
#
|
995
|
-
#
|
996
|
-
# that you can write
|
2080
|
+
# With no block given, returns the computed `matchdata`:
|
997
2081
|
#
|
998
|
-
#
|
2082
|
+
# 'foo'.match('f') # => #<MatchData "f">
|
2083
|
+
# 'foo'.match('o') # => #<MatchData "o">
|
2084
|
+
# 'foo'.match('x') # => nil
|
999
2085
|
#
|
1000
|
-
#
|
2086
|
+
# If Integer argument `offset` is given, the search begins at index `offset`:
|
1001
2087
|
#
|
1002
|
-
#
|
1003
|
-
#
|
1004
|
-
# end
|
2088
|
+
# 'foo'.match('f', 1) # => nil
|
2089
|
+
# 'foo'.match('o', 1) # => #<MatchData "o">
|
1005
2090
|
#
|
1006
|
-
#
|
2091
|
+
# With a block given, calls the block with the computed `matchdata` and returns
|
2092
|
+
# the block's return value:
|
2093
|
+
#
|
2094
|
+
# 'foo'.match(/o/) {|matchdata| matchdata } # => #<MatchData "o">
|
2095
|
+
# 'foo'.match(/x/) {|matchdata| matchdata } # => nil
|
2096
|
+
# 'foo'.match(/f/, 1) {|matchdata| matchdata } # => nil
|
1007
2097
|
#
|
1008
2098
|
def match: (Regexp | string pattern, ?int pos) -> MatchData?
|
1009
2099
|
| [A] (Regexp | string pattern, ?int pos) { (MatchData) -> A } -> A
|
1010
2100
|
|
1011
|
-
#
|
1012
|
-
#
|
1013
|
-
#
|
1014
|
-
#
|
2101
|
+
# <!--
|
2102
|
+
# rdoc-file=string.c
|
2103
|
+
# - match?(pattern, offset = 0) -> true or false
|
2104
|
+
# -->
|
2105
|
+
# Returns `true` or `false` based on whether a match is found for `self` and
|
2106
|
+
# `pattern`.
|
2107
|
+
#
|
2108
|
+
# Note: does not update [Regexp-related global
|
2109
|
+
# variables](Regexp.html#class-Regexp-label-Special+global+variables).
|
2110
|
+
#
|
2111
|
+
# Computes `regexp` by converting `pattern` (if not already a Regexp).
|
2112
|
+
# regexp = Regexp.new(pattern)
|
2113
|
+
#
|
2114
|
+
# Returns `true` if `self+.match(regexp)` returns a Matchdata object, `false`
|
2115
|
+
# otherwise:
|
1015
2116
|
#
|
1016
|
-
#
|
1017
|
-
#
|
1018
|
-
#
|
1019
|
-
#
|
2117
|
+
# 'foo'.match?(/o/) # => true
|
2118
|
+
# 'foo'.match?('o') # => true
|
2119
|
+
# 'foo'.match?(/x/) # => false
|
2120
|
+
#
|
2121
|
+
# If Integer argument `offset` is given, the search begins at index `offset`:
|
2122
|
+
# 'foo'.match?('f', 1) # => false
|
2123
|
+
# 'foo'.match?('o', 1) # => true
|
1020
2124
|
#
|
1021
2125
|
def match?: (Regexp | string pattern, ?int pos) -> bool
|
1022
2126
|
|
1023
|
-
#
|
1024
|
-
#
|
1025
|
-
#
|
1026
|
-
#
|
1027
|
-
#
|
1028
|
-
#
|
2127
|
+
# <!-- rdoc-file=string.c -->
|
2128
|
+
# Returns the successor to `self`. The successor is calculated by incrementing
|
2129
|
+
# characters.
|
2130
|
+
#
|
2131
|
+
# The first character to be incremented is the rightmost alphanumeric: or, if no
|
2132
|
+
# alphanumerics, the rightmost character:
|
2133
|
+
#
|
2134
|
+
# 'THX1138'.succ # => "THX1139"
|
2135
|
+
# '<<koala>>'.succ # => "<<koalb>>"
|
2136
|
+
# '***'.succ # => '**+'
|
2137
|
+
#
|
2138
|
+
# The successor to a digit is another digit, "carrying" to the next-left
|
2139
|
+
# character for a "rollover" from 9 to 0, and prepending another digit if
|
2140
|
+
# necessary:
|
2141
|
+
#
|
2142
|
+
# '00'.succ # => "01"
|
2143
|
+
# '09'.succ # => "10"
|
2144
|
+
# '99'.succ # => "100"
|
2145
|
+
#
|
2146
|
+
# The successor to a letter is another letter of the same case, carrying to the
|
2147
|
+
# next-left character for a rollover, and prepending another same-case letter if
|
2148
|
+
# necessary:
|
2149
|
+
#
|
2150
|
+
# 'aa'.succ # => "ab"
|
2151
|
+
# 'az'.succ # => "ba"
|
2152
|
+
# 'zz'.succ # => "aaa"
|
2153
|
+
# 'AA'.succ # => "AB"
|
2154
|
+
# 'AZ'.succ # => "BA"
|
2155
|
+
# 'ZZ'.succ # => "AAA"
|
2156
|
+
#
|
2157
|
+
# The successor to a non-alphanumeric character is the next character in the
|
2158
|
+
# underlying character set's collating sequence, carrying to the next-left
|
2159
|
+
# character for a rollover, and prepending another character if necessary:
|
2160
|
+
#
|
2161
|
+
# s = 0.chr * 3
|
2162
|
+
# s # => "\x00\x00\x00"
|
2163
|
+
# s.succ # => "\x00\x00\x01"
|
2164
|
+
# s = 255.chr * 3
|
2165
|
+
# s # => "\xFF\xFF\xFF"
|
2166
|
+
# s.succ # => "\x01\x00\x00\x00"
|
2167
|
+
#
|
2168
|
+
# Carrying can occur between and among mixtures of alphanumeric characters:
|
1029
2169
|
#
|
1030
|
-
#
|
1031
|
-
#
|
1032
|
-
#
|
2170
|
+
# s = 'zz99zz99'
|
2171
|
+
# s.succ # => "aaa00aa00"
|
2172
|
+
# s = '99zz99zz'
|
2173
|
+
# s.succ # => "100aa00aa"
|
1033
2174
|
#
|
1034
|
-
#
|
1035
|
-
#
|
1036
|
-
#
|
1037
|
-
#
|
1038
|
-
#
|
1039
|
-
# "***".succ #=> "**+"
|
2175
|
+
# The successor to an empty String is a new empty String:
|
2176
|
+
#
|
2177
|
+
# ''.succ # => ""
|
2178
|
+
#
|
2179
|
+
# String#next is an alias for String#succ.
|
1040
2180
|
#
|
1041
2181
|
def next: () -> String
|
1042
2182
|
|
1043
|
-
#
|
2183
|
+
# <!-- rdoc-file=string.c -->
|
2184
|
+
# Equivalent to String#succ, but modifies `self` in place; returns `self`.
|
2185
|
+
#
|
2186
|
+
# String#next! is an alias for String#succ!.
|
1044
2187
|
#
|
1045
2188
|
def next!: () -> self
|
1046
2189
|
|
2190
|
+
# <!--
|
2191
|
+
# rdoc-file=string.c
|
2192
|
+
# - str.oct -> integer
|
2193
|
+
# -->
|
1047
2194
|
# Treats leading characters of *str* as a string of octal digits (with an
|
1048
2195
|
# optional sign) and returns the corresponding number. Returns 0 if the
|
1049
2196
|
# conversion fails.
|
@@ -1057,12 +2204,21 @@ class String
|
|
1057
2204
|
#
|
1058
2205
|
def oct: () -> Integer
|
1059
2206
|
|
2207
|
+
# <!--
|
2208
|
+
# rdoc-file=string.c
|
2209
|
+
# - str.ord -> integer
|
2210
|
+
# -->
|
1060
2211
|
# Returns the Integer ordinal of a one-character string.
|
1061
2212
|
#
|
1062
2213
|
# "a".ord #=> 97
|
1063
2214
|
#
|
1064
2215
|
def ord: () -> Integer
|
1065
2216
|
|
2217
|
+
# <!--
|
2218
|
+
# rdoc-file=string.c
|
2219
|
+
# - str.partition(sep) -> [head, sep, tail]
|
2220
|
+
# - str.partition(regexp) -> [head, match, tail]
|
2221
|
+
# -->
|
1066
2222
|
# Searches *sep* or pattern (*regexp*) in the string and returns the part before
|
1067
2223
|
# it, the match, and the part after it. If it is not found, returns two empty
|
1068
2224
|
# strings and *str*.
|
@@ -1073,46 +2229,112 @@ class String
|
|
1073
2229
|
#
|
1074
2230
|
def partition: (Regexp | string sep_or_regexp) -> [ String, String, String ]
|
1075
2231
|
|
1076
|
-
#
|
2232
|
+
# <!--
|
2233
|
+
# rdoc-file=string.c
|
2234
|
+
# - prepend(*other_strings) -> string
|
2235
|
+
# -->
|
2236
|
+
# Prepends each string in `other_strings` to `self` and returns `self`:
|
1077
2237
|
#
|
1078
|
-
#
|
1079
|
-
#
|
1080
|
-
#
|
2238
|
+
# s = 'foo'
|
2239
|
+
# s.prepend('bar', 'baz') # => "barbazfoo"
|
2240
|
+
# s # => "barbazfoo"
|
1081
2241
|
#
|
1082
|
-
#
|
2242
|
+
# Related: String#concat.
|
1083
2243
|
#
|
1084
2244
|
def prepend: (*string other_strs) -> String
|
1085
2245
|
|
1086
|
-
#
|
2246
|
+
# <!-- rdoc-file=string.c -->
|
2247
|
+
# Replaces the contents of `self` with the contents of `other_string`:
|
1087
2248
|
#
|
1088
|
-
# s =
|
1089
|
-
# s.replace
|
2249
|
+
# s = 'foo' # => "foo"
|
2250
|
+
# s.replace('bar') # => "bar"
|
1090
2251
|
#
|
1091
2252
|
def replace: (string other_str) -> String
|
1092
2253
|
|
1093
|
-
#
|
2254
|
+
# <!--
|
2255
|
+
# rdoc-file=string.c
|
2256
|
+
# - reverse -> string
|
2257
|
+
# -->
|
2258
|
+
# Returns a new string with the characters from `self` in reverse order.
|
1094
2259
|
#
|
1095
|
-
#
|
2260
|
+
# 'stressed'.reverse # => "desserts"
|
1096
2261
|
#
|
1097
2262
|
def reverse: () -> String
|
1098
2263
|
|
1099
|
-
#
|
2264
|
+
# <!--
|
2265
|
+
# rdoc-file=string.c
|
2266
|
+
# - reverse! -> self
|
2267
|
+
# -->
|
2268
|
+
# Returns `self` with its characters reversed:
|
2269
|
+
#
|
2270
|
+
# s = 'stressed'
|
2271
|
+
# s.reverse! # => "desserts"
|
2272
|
+
# s # => "desserts"
|
1100
2273
|
#
|
1101
2274
|
def reverse!: () -> self
|
1102
2275
|
|
1103
|
-
#
|
1104
|
-
#
|
1105
|
-
#
|
1106
|
-
#
|
2276
|
+
# <!--
|
2277
|
+
# rdoc-file=string.c
|
2278
|
+
# - rindex(substring, offset = self.length) -> integer or nil
|
2279
|
+
# - rindex(regexp, offset = self.length) -> integer or nil
|
2280
|
+
# -->
|
2281
|
+
# Returns the Integer index of the *last* occurrence of the given `substring`,
|
2282
|
+
# or `nil` if none found:
|
2283
|
+
#
|
2284
|
+
# 'foo'.rindex('f') # => 0
|
2285
|
+
# 'foo'.rindex('o') # => 2
|
2286
|
+
# 'foo'.rindex('oo') # => 1
|
2287
|
+
# 'foo'.rindex('ooo') # => nil
|
2288
|
+
#
|
2289
|
+
# Returns the Integer index of the *last* match for the given Regexp `regexp`,
|
2290
|
+
# or `nil` if none found:
|
2291
|
+
#
|
2292
|
+
# 'foo'.rindex(/f/) # => 0
|
2293
|
+
# 'foo'.rindex(/o/) # => 2
|
2294
|
+
# 'foo'.rindex(/oo/) # => 1
|
2295
|
+
# 'foo'.rindex(/ooo/) # => nil
|
2296
|
+
#
|
2297
|
+
# The *last* match means starting at the possible last position, not the last of
|
2298
|
+
# longest matches.
|
2299
|
+
#
|
2300
|
+
# 'foo'.rindex(/o+/) # => 2
|
2301
|
+
# $~ #=> #<MatchData "o">
|
2302
|
+
#
|
2303
|
+
# To get the last longest match, needs to combine with negative lookbehind.
|
2304
|
+
#
|
2305
|
+
# 'foo'.rindex(/(?<!o)o+/) # => 1
|
2306
|
+
# $~ #=> #<MatchData "oo">
|
2307
|
+
#
|
2308
|
+
# Or String#index with negative lookforward.
|
2309
|
+
#
|
2310
|
+
# 'foo'.index(/o+(?!.*o)/) # => 1
|
2311
|
+
# $~ #=> #<MatchData "oo">
|
2312
|
+
#
|
2313
|
+
# Integer argument `offset`, if given and non-negative, specifies the maximum
|
2314
|
+
# starting position in the
|
2315
|
+
# string to _end_ the search:
|
1107
2316
|
#
|
1108
|
-
#
|
1109
|
-
#
|
1110
|
-
#
|
1111
|
-
#
|
1112
|
-
#
|
2317
|
+
# 'foo'.rindex('o', 0) # => nil
|
2318
|
+
# 'foo'.rindex('o', 1) # => 1
|
2319
|
+
# 'foo'.rindex('o', 2) # => 2
|
2320
|
+
# 'foo'.rindex('o', 3) # => 2
|
2321
|
+
#
|
2322
|
+
# If `offset` is a negative Integer, the maximum starting position in the string
|
2323
|
+
# to *end* the search is the sum of the string's length and `offset`:
|
2324
|
+
#
|
2325
|
+
# 'foo'.rindex('o', -1) # => 2
|
2326
|
+
# 'foo'.rindex('o', -2) # => 1
|
2327
|
+
# 'foo'.rindex('o', -3) # => nil
|
2328
|
+
# 'foo'.rindex('o', -4) # => nil
|
2329
|
+
#
|
2330
|
+
# Related: String#index.
|
1113
2331
|
#
|
1114
2332
|
def rindex: (string | Regexp substr_or_regexp, ?int pos) -> Integer?
|
1115
2333
|
|
2334
|
+
# <!--
|
2335
|
+
# rdoc-file=string.c
|
2336
|
+
# - str.rjust(integer, padstr=' ') -> new_str
|
2337
|
+
# -->
|
1116
2338
|
# If *integer* is greater than the length of *str*, returns a new String of
|
1117
2339
|
# length *integer* with *str* right justified and padded with *padstr*;
|
1118
2340
|
# otherwise, returns *str*.
|
@@ -1123,6 +2345,11 @@ class String
|
|
1123
2345
|
#
|
1124
2346
|
def rjust: (int integer, ?string padstr) -> String
|
1125
2347
|
|
2348
|
+
# <!--
|
2349
|
+
# rdoc-file=string.c
|
2350
|
+
# - str.rpartition(sep) -> [head, sep, tail]
|
2351
|
+
# - str.rpartition(regexp) -> [head, match, tail]
|
2352
|
+
# -->
|
1126
2353
|
# Searches *sep* or pattern (*regexp*) in the string from the end of the string,
|
1127
2354
|
# and returns the part before it, the match, and the part after it. If it is not
|
1128
2355
|
# found, returns two empty strings and *str*.
|
@@ -1131,8 +2358,26 @@ class String
|
|
1131
2358
|
# "hello".rpartition("x") #=> ["", "", "hello"]
|
1132
2359
|
# "hello".rpartition(/.l/) #=> ["he", "ll", "o"]
|
1133
2360
|
#
|
2361
|
+
# The match from the end means starting at the possible last position, not the
|
2362
|
+
# last of longest matches.
|
2363
|
+
#
|
2364
|
+
# "hello".rpartition(/l+/) #=> ["hel", "l", "o"]
|
2365
|
+
#
|
2366
|
+
# To partition at the last longest match, needs to combine with negative
|
2367
|
+
# lookbehind.
|
2368
|
+
#
|
2369
|
+
# "hello".rpartition(/(?<!l)l+/) #=> ["he", "ll", "o"]
|
2370
|
+
#
|
2371
|
+
# Or String#partition with negative lookforward.
|
2372
|
+
#
|
2373
|
+
# "hello".partition(/l+(?!.*l)/) #=> ["he", "ll", "o"]
|
2374
|
+
#
|
1134
2375
|
def rpartition: (string | Regexp sep_or_regexp) -> [ String, String, String ]
|
1135
2376
|
|
2377
|
+
# <!--
|
2378
|
+
# rdoc-file=string.c
|
2379
|
+
# - str.rstrip -> new_str
|
2380
|
+
# -->
|
1136
2381
|
# Returns a copy of the receiver with trailing whitespace removed. See also
|
1137
2382
|
# String#lstrip and String#strip.
|
1138
2383
|
#
|
@@ -1143,6 +2388,10 @@ class String
|
|
1143
2388
|
#
|
1144
2389
|
def rstrip: () -> String
|
1145
2390
|
|
2391
|
+
# <!--
|
2392
|
+
# rdoc-file=string.c
|
2393
|
+
# - str.rstrip! -> self or nil
|
2394
|
+
# -->
|
1146
2395
|
# Removes trailing whitespace from the receiver. Returns the altered receiver,
|
1147
2396
|
# or `nil` if no change was made. See also String#lstrip! and String#strip!.
|
1148
2397
|
#
|
@@ -1154,6 +2403,11 @@ class String
|
|
1154
2403
|
#
|
1155
2404
|
def rstrip!: () -> self?
|
1156
2405
|
|
2406
|
+
# <!--
|
2407
|
+
# rdoc-file=string.c
|
2408
|
+
# - str.scan(pattern) -> array
|
2409
|
+
# - str.scan(pattern) {|match, ...| block } -> str
|
2410
|
+
# -->
|
1157
2411
|
# Both forms iterate through *str*, matching the pattern (which may be a Regexp
|
1158
2412
|
# or a String). For each match, a result is generated and either added to the
|
1159
2413
|
# result array or passed to the block. If the pattern contains no groups, each
|
@@ -1182,104 +2436,182 @@ class String
|
|
1182
2436
|
def scan: (Regexp | string pattern) -> Array[String | Array[String]]
|
1183
2437
|
| (Regexp | string pattern) { (String | Array[String]) -> void } -> self
|
1184
2438
|
|
2439
|
+
# <!--
|
2440
|
+
# rdoc-file=string.c
|
2441
|
+
# - str.scrub -> new_str
|
2442
|
+
# - str.scrub(repl) -> new_str
|
2443
|
+
# - str.scrub{|bytes|} -> new_str
|
2444
|
+
# -->
|
1185
2445
|
# If the string is invalid byte sequence then replace invalid bytes with given
|
1186
2446
|
# replacement character, else returns self. If block is given, replace invalid
|
1187
2447
|
# bytes with returned value of the block.
|
1188
2448
|
#
|
1189
2449
|
# "abc\u3042\x81".scrub #=> "abc\u3042\uFFFD"
|
1190
2450
|
# "abc\u3042\x81".scrub("*") #=> "abc\u3042*"
|
1191
|
-
# "abc\u3042\xE3\x80".scrub{|bytes| '<'+bytes.
|
2451
|
+
# "abc\u3042\xE3\x80".scrub{|bytes| '<'+bytes.unpack1('H*')+'>' } #=> "abc\u3042<e380>"
|
1192
2452
|
#
|
1193
2453
|
def scrub: (?string repl) -> String
|
1194
2454
|
| () { (String bytes) -> string } -> String
|
1195
2455
|
|
2456
|
+
# <!--
|
2457
|
+
# rdoc-file=string.c
|
2458
|
+
# - str.scrub! -> str
|
2459
|
+
# - str.scrub!(repl) -> str
|
2460
|
+
# - str.scrub!{|bytes|} -> str
|
2461
|
+
# -->
|
1196
2462
|
# If the string is invalid byte sequence then replace invalid bytes with given
|
1197
2463
|
# replacement character, else returns self. If block is given, replace invalid
|
1198
2464
|
# bytes with returned value of the block.
|
1199
2465
|
#
|
1200
2466
|
# "abc\u3042\x81".scrub! #=> "abc\u3042\uFFFD"
|
1201
2467
|
# "abc\u3042\x81".scrub!("*") #=> "abc\u3042*"
|
1202
|
-
# "abc\u3042\xE3\x80".scrub!{|bytes| '<'+bytes.
|
2468
|
+
# "abc\u3042\xE3\x80".scrub!{|bytes| '<'+bytes.unpack1('H*')+'>' } #=> "abc\u3042<e380>"
|
1203
2469
|
#
|
1204
2470
|
def scrub!: (?string repl) -> self
|
1205
2471
|
| () { (String bytes) -> string } -> self
|
1206
2472
|
|
1207
|
-
#
|
2473
|
+
# <!--
|
2474
|
+
# rdoc-file=string.c
|
2475
|
+
# - setbyte(index, integer) -> integer
|
2476
|
+
# -->
|
2477
|
+
# Sets the byte at zero-based `index` to `integer`; returns `integer`:
|
2478
|
+
#
|
2479
|
+
# s = 'abcde' # => "abcde"
|
2480
|
+
# s.setbyte(0, 98) # => 98
|
2481
|
+
# s # => "bbcde"
|
2482
|
+
#
|
2483
|
+
# Related: String#getbyte.
|
1208
2484
|
#
|
1209
2485
|
def setbyte: (int index, int integer) -> int
|
1210
2486
|
|
1211
|
-
#
|
2487
|
+
# <!-- rdoc-file=string.c -->
|
2488
|
+
# Returns the count of characters (not bytes) in `self`:
|
2489
|
+
#
|
2490
|
+
# "\x80\u3042".length # => 2
|
2491
|
+
# "hello".length # => 5
|
2492
|
+
#
|
2493
|
+
# String#size is an alias for String#length.
|
2494
|
+
#
|
2495
|
+
# Related: String#bytesize.
|
1212
2496
|
#
|
1213
2497
|
alias size length
|
1214
2498
|
|
1215
|
-
#
|
1216
|
-
#
|
1217
|
-
#
|
1218
|
-
#
|
1219
|
-
#
|
2499
|
+
# <!-- rdoc-file=string.c -->
|
2500
|
+
# Returns the substring of `self` specified by the arguments.
|
2501
|
+
#
|
2502
|
+
# When the single Integer argument `index` is given, returns the 1-character
|
2503
|
+
# substring found in `self` at offset `index`:
|
2504
|
+
#
|
2505
|
+
# 'bar'[2] # => "r"
|
2506
|
+
#
|
2507
|
+
# Counts backward from the end of `self` if `index` is negative:
|
2508
|
+
#
|
2509
|
+
# 'foo'[-3] # => "f"
|
2510
|
+
#
|
2511
|
+
# Returns `nil` if `index` is out of range:
|
2512
|
+
#
|
2513
|
+
# 'foo'[3] # => nil
|
2514
|
+
# 'foo'[-4] # => nil
|
2515
|
+
#
|
2516
|
+
# When the two Integer arguments `start` and `length` are given, returns the
|
2517
|
+
# substring of the given `length` found in `self` at offset `start`:
|
2518
|
+
#
|
2519
|
+
# 'foo'[0, 2] # => "fo"
|
2520
|
+
# 'foo'[0, 0] # => ""
|
2521
|
+
#
|
2522
|
+
# Counts backward from the end of `self` if `start` is negative:
|
2523
|
+
#
|
2524
|
+
# 'foo'[-2, 2] # => "oo"
|
2525
|
+
#
|
2526
|
+
# Special case: returns a new empty String if `start` is equal to the length of
|
2527
|
+
# `self`:
|
2528
|
+
#
|
2529
|
+
# 'foo'[3, 2] # => ""
|
2530
|
+
#
|
2531
|
+
# Returns `nil` if `start` is out of range:
|
1220
2532
|
#
|
1221
|
-
#
|
1222
|
-
#
|
1223
|
-
# before a character and an index matching the string's size. Additionally, an
|
1224
|
-
# empty string is returned when the starting index for a character range is at
|
1225
|
-
# the end of the string.
|
2533
|
+
# 'foo'[4, 2] # => nil
|
2534
|
+
# 'foo'[-4, 2] # => nil
|
1226
2535
|
#
|
1227
|
-
# Returns
|
1228
|
-
# negative.
|
2536
|
+
# Returns the trailing substring of `self` if `length` is large:
|
1229
2537
|
#
|
1230
|
-
#
|
1231
|
-
# a `capture` follows the regular expression, which may be a capture group index
|
1232
|
-
# or name, follows the regular expression that component of the MatchData is
|
1233
|
-
# returned instead.
|
2538
|
+
# 'foo'[1, 50] # => "oo"
|
1234
2539
|
#
|
1235
|
-
#
|
2540
|
+
# Returns `nil` if `length` is negative:
|
1236
2541
|
#
|
1237
|
-
#
|
1238
|
-
# cannot be found.
|
2542
|
+
# 'foo'[0, -1] # => nil
|
1239
2543
|
#
|
1240
|
-
#
|
2544
|
+
# When the single Range argument `range` is given, derives `start` and `length`
|
2545
|
+
# values from the given `range`, and returns values as above:
|
1241
2546
|
#
|
1242
|
-
#
|
1243
|
-
#
|
1244
|
-
# a[2..3] #=> "ll"
|
2547
|
+
# * `'foo'[0..1]` is equivalent to `'foo'[0, 2]`.
|
2548
|
+
# * `'foo'[0...1]` is equivalent to `'foo'[0, 1]`.
|
1245
2549
|
#
|
1246
|
-
# a[-3, 2] #=> "er"
|
1247
|
-
# a[7..-2] #=> "her"
|
1248
|
-
# a[-4..-2] #=> "her"
|
1249
|
-
# a[-2..-4] #=> ""
|
1250
2550
|
#
|
1251
|
-
#
|
1252
|
-
#
|
1253
|
-
# a[12, 0] #=> nil
|
1254
|
-
# a[12..-1] #=> nil
|
2551
|
+
# When the Regexp argument `regexp` is given, and the `capture` argument is `0`,
|
2552
|
+
# returns the first matching substring found in `self`, or `nil` if none found:
|
1255
2553
|
#
|
1256
|
-
#
|
1257
|
-
#
|
1258
|
-
#
|
1259
|
-
#
|
2554
|
+
# 'foo'[/o/] # => "o"
|
2555
|
+
# 'foo'[/x/] # => nil
|
2556
|
+
# s = 'hello there'
|
2557
|
+
# s[/[aeiou](.)\1/] # => "ell"
|
2558
|
+
# s[/[aeiou](.)\1/, 0] # => "ell"
|
1260
2559
|
#
|
1261
|
-
#
|
1262
|
-
#
|
2560
|
+
# If argument `capture` is given and not `0`, it should be either an Integer
|
2561
|
+
# capture group index or a String or Symbol capture group name; the method call
|
2562
|
+
# returns only the specified capture (see [Regexp
|
2563
|
+
# Capturing](Regexp.html#class-Regexp-label-Capturing)):
|
1263
2564
|
#
|
1264
|
-
#
|
1265
|
-
#
|
2565
|
+
# s = 'hello there'
|
2566
|
+
# s[/[aeiou](.)\1/, 1] # => "l"
|
2567
|
+
# s[/(?<vowel>[aeiou])(?<non_vowel>[^aeiou])/, "non_vowel"] # => "l"
|
2568
|
+
# s[/(?<vowel>[aeiou])(?<non_vowel>[^aeiou])/, :vowel] # => "e"
|
2569
|
+
#
|
2570
|
+
# If an invalid capture group index is given, `nil` is returned. If an invalid
|
2571
|
+
# capture group name is given, `IndexError` is raised.
|
2572
|
+
#
|
2573
|
+
# When the single String argument `substring` is given, returns the substring
|
2574
|
+
# from `self` if found, otherwise `nil`:
|
2575
|
+
#
|
2576
|
+
# 'foo'['oo'] # => "oo"
|
2577
|
+
# 'foo'['xx'] # => nil
|
2578
|
+
#
|
2579
|
+
# String#slice is an alias for String#[].
|
1266
2580
|
#
|
1267
2581
|
alias slice []
|
1268
2582
|
|
1269
|
-
#
|
2583
|
+
# <!--
|
2584
|
+
# rdoc-file=string.c
|
2585
|
+
# - slice!(index) -> new_string or nil
|
2586
|
+
# - slice!(start, length) -> new_string or nil
|
2587
|
+
# - slice!(range) -> new_string or nil
|
2588
|
+
# - slice!(regexp, capture = 0) -> new_string or nil
|
2589
|
+
# - slice!(substring) -> new_string or nil
|
2590
|
+
# -->
|
2591
|
+
# Removes the substring of `self` specified by the arguments; returns the
|
2592
|
+
# removed substring.
|
1270
2593
|
#
|
1271
|
-
#
|
2594
|
+
# See String#[] for details about the arguments that specify the substring.
|
2595
|
+
#
|
2596
|
+
# A few examples:
|
2597
|
+
#
|
2598
|
+
# string = "This is a string"
|
1272
2599
|
# string.slice!(2) #=> "i"
|
1273
2600
|
# string.slice!(3..6) #=> " is "
|
1274
2601
|
# string.slice!(/s.*t/) #=> "sa st"
|
1275
2602
|
# string.slice!("r") #=> "r"
|
1276
|
-
# string #=> "
|
2603
|
+
# string #=> "Thing"
|
1277
2604
|
#
|
1278
2605
|
def slice!: (int integer, ?int integer) -> String?
|
1279
2606
|
| (Range[Integer] | Range[Integer?] range) -> String?
|
1280
2607
|
| (Regexp regexp, ?int | String capture) -> String?
|
1281
2608
|
| (String other_str) -> String?
|
1282
2609
|
|
2610
|
+
# <!--
|
2611
|
+
# rdoc-file=string.c
|
2612
|
+
# - str.split(pattern=nil, [limit]) -> an_array
|
2613
|
+
# - str.split(pattern=nil, [limit]) {|sub| block } -> str
|
2614
|
+
# -->
|
1283
2615
|
# Divides *str* into substrings based on a delimiter, returning an array of
|
1284
2616
|
# these substrings.
|
1285
2617
|
#
|
@@ -1328,6 +2660,10 @@ class String
|
|
1328
2660
|
def split: (?Regexp | string pattern, ?int limit) -> Array[String]
|
1329
2661
|
| (?Regexp | string pattern, ?int limit) { (String) -> void } -> self
|
1330
2662
|
|
2663
|
+
# <!--
|
2664
|
+
# rdoc-file=string.c
|
2665
|
+
# - str.squeeze([other_str]*) -> new_str
|
2666
|
+
# -->
|
1331
2667
|
# Builds a set of characters from the *other_str* parameter(s) using the
|
1332
2668
|
# procedure described for String#count. Returns a new string where runs of the
|
1333
2669
|
# same character that occur in this set are replaced by a single character. If
|
@@ -1340,11 +2676,19 @@ class String
|
|
1340
2676
|
#
|
1341
2677
|
def squeeze: (*string other_str) -> String
|
1342
2678
|
|
2679
|
+
# <!--
|
2680
|
+
# rdoc-file=string.c
|
2681
|
+
# - str.squeeze!([other_str]*) -> str or nil
|
2682
|
+
# -->
|
1343
2683
|
# Squeezes *str* in place, returning either *str*, or `nil` if no changes were
|
1344
2684
|
# made.
|
1345
2685
|
#
|
1346
2686
|
def squeeze!: (*string other_str) -> self?
|
1347
2687
|
|
2688
|
+
# <!--
|
2689
|
+
# rdoc-file=string.c
|
2690
|
+
# - str.start_with?([prefixes]+) -> true or false
|
2691
|
+
# -->
|
1348
2692
|
# Returns true if `str` starts with one of the `prefixes` given. Each of the
|
1349
2693
|
# `prefixes` should be a String or a Regexp.
|
1350
2694
|
#
|
@@ -1357,6 +2701,10 @@ class String
|
|
1357
2701
|
#
|
1358
2702
|
def start_with?: (*string prefixes) -> bool
|
1359
2703
|
|
2704
|
+
# <!--
|
2705
|
+
# rdoc-file=string.c
|
2706
|
+
# - str.strip -> new_str
|
2707
|
+
# -->
|
1360
2708
|
# Returns a copy of the receiver with leading and trailing whitespace removed.
|
1361
2709
|
#
|
1362
2710
|
# Whitespace is defined as any of the following characters: null, horizontal
|
@@ -1369,6 +2717,10 @@ class String
|
|
1369
2717
|
#
|
1370
2718
|
def strip: () -> String
|
1371
2719
|
|
2720
|
+
# <!--
|
2721
|
+
# rdoc-file=string.c
|
2722
|
+
# - str.strip! -> self or nil
|
2723
|
+
# -->
|
1372
2724
|
# Removes leading and trailing whitespace from the receiver. Returns the altered
|
1373
2725
|
# receiver, or `nil` if there was no change.
|
1374
2726
|
#
|
@@ -1379,82 +2731,109 @@ class String
|
|
1379
2731
|
#
|
1380
2732
|
def strip!: () -> self?
|
1381
2733
|
|
1382
|
-
#
|
1383
|
-
#
|
1384
|
-
#
|
1385
|
-
#
|
1386
|
-
#
|
1387
|
-
#
|
1388
|
-
#
|
1389
|
-
#
|
1390
|
-
#
|
1391
|
-
#
|
1392
|
-
#
|
1393
|
-
# `\&`. `\\\` is interpreted as an escape, i.e., a single backslash. Note that,
|
1394
|
-
# within `replacement` the special match variables, such as `$&`, will not refer
|
1395
|
-
# to the current match.
|
1396
|
-
#
|
1397
|
-
# If the second argument is a Hash, and the matched text is one of its keys, the
|
1398
|
-
# corresponding value is the replacement string.
|
1399
|
-
#
|
1400
|
-
# In the block form, the current match string is passed in as a parameter, and
|
1401
|
-
# variables such as `$1`, `$2`, `$``, `$&`, and `$'` will be set appropriately.
|
1402
|
-
# (See regexp.rdoc for details.) The value returned by the block will be
|
1403
|
-
# substituted for the match on each call.
|
1404
|
-
#
|
1405
|
-
# "hello".sub(/[aeiou]/, '*') #=> "h*llo"
|
1406
|
-
# "hello".sub(/([aeiou])/, '<\1>') #=> "h<e>llo"
|
1407
|
-
# "hello".sub(/./) {|s| s.ord.to_s + ' ' } #=> "104 ello"
|
1408
|
-
# "hello".sub(/(?<foo>[aeiou])/, '*\k<foo>*') #=> "h*e*llo"
|
1409
|
-
# 'Is SHELL your preferred shell?'.sub(/[[:upper:]]{2,}/, ENV)
|
1410
|
-
# #=> "Is /bin/bash your preferred shell?"
|
1411
|
-
#
|
1412
|
-
# Note that a string literal consumes backslashes. (See syntax/literals.rdoc for
|
1413
|
-
# details about string literals.) Back-references are typically preceded by an
|
1414
|
-
# additional backslash. For example, if you want to write a back-reference `\&`
|
1415
|
-
# in `replacement` with a double-quoted string literal, you need to write:
|
1416
|
-
# `"..\\\\&.."`. If you want to write a non-back-reference string `\&` in
|
1417
|
-
# `replacement`, you need first to escape the backslash to prevent this method
|
1418
|
-
# from interpreting it as a back-reference, and then you need to escape the
|
1419
|
-
# backslashes again to prevent a string literal from consuming them:
|
1420
|
-
# `"..\\\\\\\\&.."`. You may want to use the block form to avoid a lot of
|
1421
|
-
# backslashes.
|
2734
|
+
# <!--
|
2735
|
+
# rdoc-file=string.c
|
2736
|
+
# - sub(pattern, replacement) -> new_string
|
2737
|
+
# - sub(pattern) {|match| ... } -> new_string
|
2738
|
+
# -->
|
2739
|
+
# Returns a copy of `self` with only the first occurrence (not all occurrences)
|
2740
|
+
# of the given `pattern` replaced.
|
2741
|
+
#
|
2742
|
+
# See [Substitution Methods](#class-String-label-Substitution+Methods).
|
2743
|
+
#
|
2744
|
+
# Related: String#sub!, String#gsub, String#gsub!.
|
1422
2745
|
#
|
1423
2746
|
def sub: (Regexp | string pattern, string | Hash[String, String] replacement) -> String
|
1424
2747
|
| (Regexp | string pattern) { (String match) -> _ToS } -> String
|
1425
2748
|
|
1426
|
-
#
|
2749
|
+
# <!--
|
2750
|
+
# rdoc-file=string.c
|
2751
|
+
# - sub!(pattern, replacement) -> self or nil
|
2752
|
+
# - sub!(pattern) {|match| ... } -> self or nil
|
2753
|
+
# -->
|
2754
|
+
# Returns `self` with only the first occurrence (not all occurrences) of the
|
2755
|
+
# given `pattern` replaced.
|
2756
|
+
#
|
2757
|
+
# See [Substitution Methods](#class-String-label-Substitution+Methods).
|
1427
2758
|
#
|
1428
|
-
#
|
1429
|
-
# performed.
|
2759
|
+
# Related: String#sub, String#gsub, String#gsub!.
|
1430
2760
|
#
|
1431
2761
|
def sub!: (Regexp | string pattern, string | Hash[String, String] replacement) -> self?
|
1432
2762
|
| (Regexp | string pattern) { (String match) -> _ToS } -> String?
|
1433
2763
|
|
1434
|
-
#
|
1435
|
-
#
|
1436
|
-
#
|
1437
|
-
#
|
1438
|
-
#
|
1439
|
-
#
|
2764
|
+
# <!--
|
2765
|
+
# rdoc-file=string.c
|
2766
|
+
# - succ -> new_str
|
2767
|
+
# -->
|
2768
|
+
# Returns the successor to `self`. The successor is calculated by incrementing
|
2769
|
+
# characters.
|
2770
|
+
#
|
2771
|
+
# The first character to be incremented is the rightmost alphanumeric: or, if no
|
2772
|
+
# alphanumerics, the rightmost character:
|
2773
|
+
#
|
2774
|
+
# 'THX1138'.succ # => "THX1139"
|
2775
|
+
# '<<koala>>'.succ # => "<<koalb>>"
|
2776
|
+
# '***'.succ # => '**+'
|
2777
|
+
#
|
2778
|
+
# The successor to a digit is another digit, "carrying" to the next-left
|
2779
|
+
# character for a "rollover" from 9 to 0, and prepending another digit if
|
2780
|
+
# necessary:
|
2781
|
+
#
|
2782
|
+
# '00'.succ # => "01"
|
2783
|
+
# '09'.succ # => "10"
|
2784
|
+
# '99'.succ # => "100"
|
2785
|
+
#
|
2786
|
+
# The successor to a letter is another letter of the same case, carrying to the
|
2787
|
+
# next-left character for a rollover, and prepending another same-case letter if
|
2788
|
+
# necessary:
|
1440
2789
|
#
|
1441
|
-
#
|
1442
|
-
#
|
1443
|
-
#
|
2790
|
+
# 'aa'.succ # => "ab"
|
2791
|
+
# 'az'.succ # => "ba"
|
2792
|
+
# 'zz'.succ # => "aaa"
|
2793
|
+
# 'AA'.succ # => "AB"
|
2794
|
+
# 'AZ'.succ # => "BA"
|
2795
|
+
# 'ZZ'.succ # => "AAA"
|
1444
2796
|
#
|
1445
|
-
#
|
1446
|
-
#
|
1447
|
-
#
|
1448
|
-
#
|
1449
|
-
#
|
1450
|
-
#
|
2797
|
+
# The successor to a non-alphanumeric character is the next character in the
|
2798
|
+
# underlying character set's collating sequence, carrying to the next-left
|
2799
|
+
# character for a rollover, and prepending another character if necessary:
|
2800
|
+
#
|
2801
|
+
# s = 0.chr * 3
|
2802
|
+
# s # => "\x00\x00\x00"
|
2803
|
+
# s.succ # => "\x00\x00\x01"
|
2804
|
+
# s = 255.chr * 3
|
2805
|
+
# s # => "\xFF\xFF\xFF"
|
2806
|
+
# s.succ # => "\x01\x00\x00\x00"
|
2807
|
+
#
|
2808
|
+
# Carrying can occur between and among mixtures of alphanumeric characters:
|
2809
|
+
#
|
2810
|
+
# s = 'zz99zz99'
|
2811
|
+
# s.succ # => "aaa00aa00"
|
2812
|
+
# s = '99zz99zz'
|
2813
|
+
# s.succ # => "100aa00aa"
|
2814
|
+
#
|
2815
|
+
# The successor to an empty String is a new empty String:
|
2816
|
+
#
|
2817
|
+
# ''.succ # => ""
|
2818
|
+
#
|
2819
|
+
# String#next is an alias for String#succ.
|
1451
2820
|
#
|
1452
2821
|
def succ: () -> String
|
1453
2822
|
|
1454
|
-
#
|
2823
|
+
# <!--
|
2824
|
+
# rdoc-file=string.c
|
2825
|
+
# - succ! -> self
|
2826
|
+
# -->
|
2827
|
+
# Equivalent to String#succ, but modifies `self` in place; returns `self`.
|
2828
|
+
#
|
2829
|
+
# String#next! is an alias for String#succ!.
|
1455
2830
|
#
|
1456
2831
|
def succ!: () -> String
|
1457
2832
|
|
2833
|
+
# <!--
|
2834
|
+
# rdoc-file=string.c
|
2835
|
+
# - str.sum(n=16) -> integer
|
2836
|
+
# -->
|
1458
2837
|
# Returns a basic *n*-bit checksum of the characters in *str*, where *n* is the
|
1459
2838
|
# optional Integer parameter, defaulting to 16. The result is simply the sum of
|
1460
2839
|
# the binary value of each byte in *str* modulo `2**n - 1`. This is not a
|
@@ -1462,29 +2841,52 @@ class String
|
|
1462
2841
|
#
|
1463
2842
|
def sum: (?int n) -> Integer
|
1464
2843
|
|
1465
|
-
#
|
1466
|
-
#
|
2844
|
+
# <!--
|
2845
|
+
# rdoc-file=string.c
|
2846
|
+
# - swapcase(*options) -> string
|
2847
|
+
# -->
|
2848
|
+
# Returns a string containing the characters in `self`, with cases reversed;
|
2849
|
+
# each uppercase character is downcased; each lowercase character is upcased:
|
1467
2850
|
#
|
1468
|
-
#
|
2851
|
+
# s = 'Hello World!' # => "Hello World!"
|
2852
|
+
# s.swapcase # => "hELLO wORLD!"
|
1469
2853
|
#
|
1470
|
-
#
|
1471
|
-
#
|
2854
|
+
# The casing may be affected by the given `options`; see [Case
|
2855
|
+
# Mapping](doc/case_mapping_rdoc.html).
|
2856
|
+
#
|
2857
|
+
# Related: String#swapcase!.
|
1472
2858
|
#
|
1473
2859
|
def swapcase: () -> String
|
1474
2860
|
| (:ascii | :lithuanian | :turkic) -> String
|
1475
2861
|
| (:lithuanian, :turkic) -> String
|
1476
2862
|
| (:turkic, :lithuanian) -> String
|
1477
2863
|
|
1478
|
-
#
|
1479
|
-
#
|
2864
|
+
# <!--
|
2865
|
+
# rdoc-file=string.c
|
2866
|
+
# - swapcase!(*options) -> self or nil
|
2867
|
+
# -->
|
2868
|
+
# Upcases each lowercase character in `self`; downcases uppercase character;
|
2869
|
+
# returns `self` if any changes were made, `nil` otherwise:
|
2870
|
+
#
|
2871
|
+
# s = 'Hello World!' # => "Hello World!"
|
2872
|
+
# s.swapcase! # => "hELLO wORLD!"
|
2873
|
+
# s # => "Hello World!"
|
2874
|
+
# ''.swapcase! # => nil
|
2875
|
+
#
|
2876
|
+
# The casing may be affected by the given `options`; see [Case
|
2877
|
+
# Mapping](doc/case_mapping_rdoc.html).
|
1480
2878
|
#
|
1481
|
-
#
|
2879
|
+
# Related: String#swapcase.
|
1482
2880
|
#
|
1483
2881
|
def swapcase!: () -> self?
|
1484
2882
|
| (:ascii | :lithuanian | :turkic) -> self?
|
1485
2883
|
| (:lithuanian, :turkic) -> self?
|
1486
2884
|
| (:turkic, :lithuanian) -> self?
|
1487
2885
|
|
2886
|
+
# <!--
|
2887
|
+
# rdoc-file=complex.c
|
2888
|
+
# - str.to_c -> complex
|
2889
|
+
# -->
|
1488
2890
|
# Returns a complex which denotes the string form. The parser ignores leading
|
1489
2891
|
# whitespaces and trailing garbage. Any digit sequences can be separated by an
|
1490
2892
|
# underscore. Returns zero for null or garbage string.
|
@@ -1505,34 +2907,51 @@ class String
|
|
1505
2907
|
#
|
1506
2908
|
def to_c: () -> Complex
|
1507
2909
|
|
1508
|
-
#
|
1509
|
-
#
|
1510
|
-
#
|
1511
|
-
#
|
2910
|
+
# <!--
|
2911
|
+
# rdoc-file=string.c
|
2912
|
+
# - to_f -> float
|
2913
|
+
# -->
|
2914
|
+
# Returns the result of interpreting leading characters in `self` as a Float:
|
2915
|
+
#
|
2916
|
+
# '3.14159'.to_f # => 3.14159
|
2917
|
+
# '1.234e-2'.to_f # => 0.01234
|
2918
|
+
#
|
2919
|
+
# Characters past a leading valid number (in the given `base`) are ignored:
|
2920
|
+
#
|
2921
|
+
# '3.14 (pi to two places)'.to_f # => 3.14
|
1512
2922
|
#
|
1513
|
-
#
|
1514
|
-
#
|
1515
|
-
#
|
2923
|
+
# Returns zero if there is no leading valid number:
|
2924
|
+
#
|
2925
|
+
# 'abcdef'.to_f # => 0.0
|
1516
2926
|
#
|
1517
2927
|
def to_f: () -> Float
|
1518
2928
|
|
1519
|
-
#
|
1520
|
-
#
|
1521
|
-
#
|
1522
|
-
#
|
1523
|
-
#
|
1524
|
-
#
|
1525
|
-
#
|
1526
|
-
#
|
1527
|
-
#
|
1528
|
-
#
|
1529
|
-
#
|
1530
|
-
#
|
1531
|
-
#
|
1532
|
-
#
|
2929
|
+
# <!--
|
2930
|
+
# rdoc-file=string.c
|
2931
|
+
# - to_i(base = 10) -> integer
|
2932
|
+
# -->
|
2933
|
+
# Returns the result of interpreting leading characters in `self` as an integer
|
2934
|
+
# in the given `base` (which must be in (2..36)):
|
2935
|
+
#
|
2936
|
+
# '123456'.to_i # => 123456
|
2937
|
+
# '123def'.to_i(16) # => 1195503
|
2938
|
+
#
|
2939
|
+
# Characters past a leading valid number (in the given `base`) are ignored:
|
2940
|
+
#
|
2941
|
+
# '12.345'.to_i # => 12
|
2942
|
+
# '12345'.to_i(2) # => 1
|
2943
|
+
#
|
2944
|
+
# Returns zero if there is no leading valid number:
|
2945
|
+
#
|
2946
|
+
# 'abcdef'.to_i # => 0
|
2947
|
+
# '2'.to_i(2) # => 0
|
1533
2948
|
#
|
1534
2949
|
def to_i: (?int base) -> Integer
|
1535
2950
|
|
2951
|
+
# <!--
|
2952
|
+
# rdoc-file=rational.c
|
2953
|
+
# - str.to_r -> rational
|
2954
|
+
# -->
|
1536
2955
|
# Returns the result of interpreting leading characters in `str` as a rational.
|
1537
2956
|
# Leading whitespace and extraneous characters past the end of a valid number
|
1538
2957
|
# are ignored. Digit sequences can be separated by an underscore. If there is
|
@@ -1558,18 +2977,26 @@ class String
|
|
1558
2977
|
#
|
1559
2978
|
def to_r: () -> Rational
|
1560
2979
|
|
1561
|
-
#
|
2980
|
+
# <!--
|
2981
|
+
# rdoc-file=string.c
|
2982
|
+
# - to_s -> self or string
|
2983
|
+
# -->
|
2984
|
+
# Returns `self` if `self` is a String, or `self` converted to a String if
|
2985
|
+
# `self` is a subclass of String.
|
1562
2986
|
#
|
1563
|
-
#
|
2987
|
+
# String#to_str is an alias for String#to_s.
|
1564
2988
|
#
|
1565
2989
|
def to_s: () -> String
|
1566
2990
|
|
1567
|
-
#
|
2991
|
+
# <!-- rdoc-file=string.c -->
|
2992
|
+
# Returns `self` if `self` is a String, or `self` converted to a String if
|
2993
|
+
# `self` is a subclass of String.
|
1568
2994
|
#
|
1569
|
-
#
|
2995
|
+
# String#to_str is an alias for String#to_s.
|
1570
2996
|
#
|
1571
2997
|
def to_str: () -> String
|
1572
2998
|
|
2999
|
+
# <!-- rdoc-file=string.c -->
|
1573
3000
|
# Returns the Symbol corresponding to *str*, creating the symbol if it did not
|
1574
3001
|
# previously exist. See Symbol#id2name.
|
1575
3002
|
#
|
@@ -1586,6 +3013,10 @@ class String
|
|
1586
3013
|
#
|
1587
3014
|
def to_sym: () -> Symbol
|
1588
3015
|
|
3016
|
+
# <!--
|
3017
|
+
# rdoc-file=string.c
|
3018
|
+
# - str.tr(from_str, to_str) => new_str
|
3019
|
+
# -->
|
1589
3020
|
# Returns a copy of `str` with the characters in `from_str` replaced by the
|
1590
3021
|
# corresponding characters in `to_str`. If `to_str` is shorter than `from_str`,
|
1591
3022
|
# it is padded with its last character in order to maintain the correspondence.
|
@@ -1617,11 +3048,19 @@ class String
|
|
1617
3048
|
#
|
1618
3049
|
def tr: (string from_str, string to_str) -> String
|
1619
3050
|
|
3051
|
+
# <!--
|
3052
|
+
# rdoc-file=string.c
|
3053
|
+
# - str.tr!(from_str, to_str) -> str or nil
|
3054
|
+
# -->
|
1620
3055
|
# Translates *str* in place, using the same rules as String#tr. Returns *str*,
|
1621
3056
|
# or `nil` if no changes were made.
|
1622
3057
|
#
|
1623
3058
|
def tr!: (string from_str, string to_str) -> String?
|
1624
3059
|
|
3060
|
+
# <!--
|
3061
|
+
# rdoc-file=string.c
|
3062
|
+
# - str.tr_s(from_str, to_str) -> new_str
|
3063
|
+
# -->
|
1625
3064
|
# Processes a copy of *str* as described under String#tr, then removes duplicate
|
1626
3065
|
# characters in regions that were affected by the translation.
|
1627
3066
|
#
|
@@ -1631,18 +3070,34 @@ class String
|
|
1631
3070
|
#
|
1632
3071
|
def tr_s: (string from_str, string to_str) -> String
|
1633
3072
|
|
3073
|
+
# <!--
|
3074
|
+
# rdoc-file=string.c
|
3075
|
+
# - str.tr_s!(from_str, to_str) -> str or nil
|
3076
|
+
# -->
|
1634
3077
|
# Performs String#tr_s processing on *str* in place, returning *str*, or `nil`
|
1635
3078
|
# if no changes were made.
|
1636
3079
|
#
|
1637
3080
|
def tr_s!: (string from_str, string to_str) -> String?
|
1638
3081
|
|
1639
|
-
#
|
1640
|
-
#
|
3082
|
+
# <!--
|
3083
|
+
# rdoc-file=string.c
|
3084
|
+
# - undump -> string
|
3085
|
+
# -->
|
3086
|
+
# Returns an unescaped version of `self`:
|
3087
|
+
#
|
3088
|
+
# s_orig = "\f\x00\xff\\\"" # => "\f\u0000\xFF\\\""
|
3089
|
+
# s_dumped = s_orig.dump # => "\"\\f\\x00\\xFF\\\\\\\"\""
|
3090
|
+
# s_undumped = s_dumped.undump # => "\f\u0000\xFF\\\""
|
3091
|
+
# s_undumped == s_orig # => true
|
1641
3092
|
#
|
1642
|
-
#
|
3093
|
+
# Related: String#dump (inverse of String#undump).
|
1643
3094
|
#
|
1644
3095
|
def undump: () -> String
|
1645
3096
|
|
3097
|
+
# <!--
|
3098
|
+
# rdoc-file=string.c
|
3099
|
+
# - str.unicode_normalize(form=:nfc)
|
3100
|
+
# -->
|
1646
3101
|
# Unicode Normalization---Returns a normalized form of `str`, using Unicode
|
1647
3102
|
# normalizations NFC, NFD, NFKC, or NFKD. The normalization form used is
|
1648
3103
|
# determined by `form`, which can be any of the four values `:nfc`, `:nfd`,
|
@@ -1661,11 +3116,19 @@ class String
|
|
1661
3116
|
#
|
1662
3117
|
def unicode_normalize: (?:nfc | :nfd | :nfkc | :nfkd) -> String
|
1663
3118
|
|
3119
|
+
# <!--
|
3120
|
+
# rdoc-file=string.c
|
3121
|
+
# - str.unicode_normalize!(form=:nfc)
|
3122
|
+
# -->
|
1664
3123
|
# Destructive version of String#unicode_normalize, doing Unicode normalization
|
1665
3124
|
# in place.
|
1666
3125
|
#
|
1667
3126
|
def unicode_normalize!: (?:nfc | :nfd | :nfkc | :nfkd) -> String
|
1668
3127
|
|
3128
|
+
# <!--
|
3129
|
+
# rdoc-file=string.c
|
3130
|
+
# - str.unicode_normalized?(form=:nfc)
|
3131
|
+
# -->
|
1669
3132
|
# Checks whether `str` is in Unicode normalization form `form`, which can be any
|
1670
3133
|
# of the four values `:nfc`, `:nfd`, `:nfkc`, or `:nfkd`. The default is `:nfc`.
|
1671
3134
|
#
|
@@ -1681,6 +3144,11 @@ class String
|
|
1681
3144
|
#
|
1682
3145
|
def unicode_normalized?: (?:nfc | :nfd | :nfkc | :nfkd) -> bool
|
1683
3146
|
|
3147
|
+
# <!--
|
3148
|
+
# rdoc-file=pack.rb
|
3149
|
+
# - str.unpack(format) -> anArray
|
3150
|
+
# - str.unpack(format, offset: anInteger) -> anArray
|
3151
|
+
# -->
|
1684
3152
|
# Decodes *str* (which may contain binary data) according to the format string,
|
1685
3153
|
# returning an array of each value extracted. The format string consists of a
|
1686
3154
|
# sequence of single-character directives, summarized in the table at the end of
|
@@ -1690,6 +3158,7 @@ class String
|
|
1690
3158
|
# underscore (```_`'') or exclamation mark (```!`'') to use the underlying
|
1691
3159
|
# platform's native size for the specified type; otherwise, it uses a
|
1692
3160
|
# platform-independent consistent size. Spaces are ignored in the format string.
|
3161
|
+
#
|
1693
3162
|
# See also String#unpack1, Array#pack.
|
1694
3163
|
#
|
1695
3164
|
# "abc \0\0abc \0\0".unpack('A6Z6') #=> ["abc", "abc "]
|
@@ -1736,14 +3205,14 @@ class String
|
|
1736
3205
|
# S> s> S!> s!> | Integer | same as the directives without ">" except
|
1737
3206
|
# L> l> L!> l!> | | big endian
|
1738
3207
|
# I!> i!> | |
|
1739
|
-
# Q> q> Q!> q!> | | "S>" is same as "n"
|
1740
|
-
# J> j> J!> j!> | | "L>" is same as "N"
|
3208
|
+
# Q> q> Q!> q!> | | "S>" is the same as "n"
|
3209
|
+
# J> j> J!> j!> | | "L>" is the same as "N"
|
1741
3210
|
# | |
|
1742
3211
|
# S< s< S!< s!< | Integer | same as the directives without "<" except
|
1743
3212
|
# L< l< L!< l!< | | little endian
|
1744
3213
|
# I!< i!< | |
|
1745
|
-
# Q< q< Q!< q!< | | "S<" is same as "v"
|
1746
|
-
# J< j< J!< j!< | | "L<" is same as "V"
|
3214
|
+
# Q< q< Q!< q!< | | "S<" is the same as "v"
|
3215
|
+
# J< j< J!< j!< | | "L<" is the same as "V"
|
1747
3216
|
# | |
|
1748
3217
|
# n | Integer | 16-bit unsigned, network (big-endian) byte order
|
1749
3218
|
# N | Integer | 32-bit unsigned, network (big-endian) byte order
|
@@ -1751,7 +3220,7 @@ class String
|
|
1751
3220
|
# V | Integer | 32-bit unsigned, VAX (little-endian) byte order
|
1752
3221
|
# | |
|
1753
3222
|
# U | Integer | UTF-8 character
|
1754
|
-
# w | Integer | BER-compressed integer (see Array
|
3223
|
+
# w | Integer | BER-compressed integer (see Array#pack)
|
1755
3224
|
#
|
1756
3225
|
# Float | |
|
1757
3226
|
# Directive | Returns | Meaning
|
@@ -1787,17 +3256,29 @@ class String
|
|
1787
3256
|
# X | --- | skip backward one byte
|
1788
3257
|
# x | --- | skip forward one byte
|
1789
3258
|
#
|
3259
|
+
# The keyword *offset* can be given to start the decoding after skipping the
|
3260
|
+
# specified amount of bytes:
|
3261
|
+
# "abc".unpack("C*") # => [97, 98, 99]
|
3262
|
+
# "abc".unpack("C*", offset: 2) # => [99]
|
3263
|
+
# "abc".unpack("C*", offset: 4) # => offset outside of string (ArgumentError)
|
3264
|
+
#
|
1790
3265
|
# HISTORY
|
1791
3266
|
#
|
1792
3267
|
# * J, J! j, and j! are available since Ruby 2.3.
|
1793
3268
|
# * Q_, Q!, q_, and q! are available since Ruby 2.1.
|
1794
3269
|
# * I!<, i!<, I!>, and i!> are available since Ruby 1.9.3.
|
1795
3270
|
#
|
1796
|
-
|
1797
|
-
def unpack: (String format) -> Array[Integer | Float | String | nil]
|
3271
|
+
def unpack: (String format, ?offset: Integer) -> Array[Integer | Float | String | nil]
|
1798
3272
|
|
3273
|
+
# <!--
|
3274
|
+
# rdoc-file=pack.rb
|
3275
|
+
# - str.unpack1(format) -> obj
|
3276
|
+
# - str.unpack1(format, offset: anInteger) -> obj
|
3277
|
+
# -->
|
1799
3278
|
# Decodes *str* (which may contain binary data) according to the format string,
|
1800
|
-
# returning the first value extracted.
|
3279
|
+
# returning the first value extracted.
|
3280
|
+
#
|
3281
|
+
# See also String#unpack, Array#pack.
|
1801
3282
|
#
|
1802
3283
|
# Contrast with String#unpack:
|
1803
3284
|
#
|
@@ -1807,64 +3288,102 @@ class String
|
|
1807
3288
|
# In that case data would be lost but often it's the case that the array only
|
1808
3289
|
# holds one value, especially when unpacking binary data. For instance:
|
1809
3290
|
#
|
1810
|
-
#
|
1811
|
-
#
|
3291
|
+
# "\xff\x00\x00\x00".unpack("l") #=> [255]
|
3292
|
+
# "\xff\x00\x00\x00".unpack1("l") #=> 255
|
1812
3293
|
#
|
1813
3294
|
# Thus unpack1 is convenient, makes clear the intention and signals the expected
|
1814
3295
|
# return value to those reading the code.
|
1815
3296
|
#
|
3297
|
+
# The keyword *offset* can be given to start the decoding after skipping the
|
3298
|
+
# specified amount of bytes:
|
3299
|
+
# "abc".unpack1("C*") # => 97
|
3300
|
+
# "abc".unpack1("C*", offset: 2) # => 99
|
3301
|
+
# "abc".unpack1("C*", offset: 4) # => offset outside of string (ArgumentError)
|
3302
|
+
#
|
1816
3303
|
def unpack1: (String format) -> (Integer | Float | String | nil)
|
1817
3304
|
|
1818
|
-
#
|
1819
|
-
#
|
3305
|
+
# <!--
|
3306
|
+
# rdoc-file=string.c
|
3307
|
+
# - upcase(*options) -> string
|
3308
|
+
# -->
|
3309
|
+
# Returns a string containing the upcased characters in `self`:
|
3310
|
+
#
|
3311
|
+
# s = 'Hello World!' # => "Hello World!"
|
3312
|
+
# s.upcase # => "HELLO WORLD!"
|
1820
3313
|
#
|
1821
|
-
#
|
3314
|
+
# The casing may be affected by the given `options`; see [Case
|
3315
|
+
# Mapping](doc/case_mapping_rdoc.html).
|
1822
3316
|
#
|
1823
|
-
#
|
3317
|
+
# Related: String#upcase!, String#downcase, String#downcase!.
|
1824
3318
|
#
|
1825
3319
|
def upcase: () -> String
|
1826
3320
|
| (:ascii | :lithuanian | :turkic) -> String
|
1827
3321
|
| (:lithuanian, :turkic) -> String
|
1828
3322
|
| (:turkic, :lithuanian) -> String
|
1829
3323
|
|
1830
|
-
#
|
3324
|
+
# <!--
|
3325
|
+
# rdoc-file=string.c
|
3326
|
+
# - upcase!(*options) -> self or nil
|
3327
|
+
# -->
|
3328
|
+
# Upcases the characters in `self`; returns `self` if any changes were made,
|
3329
|
+
# `nil` otherwise:
|
3330
|
+
#
|
3331
|
+
# s = 'Hello World!' # => "Hello World!"
|
3332
|
+
# s.upcase! # => "HELLO WORLD!"
|
3333
|
+
# s # => "HELLO WORLD!"
|
3334
|
+
# s.upcase! # => nil
|
1831
3335
|
#
|
1832
|
-
#
|
3336
|
+
# The casing may be affected by the given `options`; see [Case
|
3337
|
+
# Mapping](doc/case_mapping_rdoc.html).
|
3338
|
+
#
|
3339
|
+
# Related: String#upcase, String#downcase, String#downcase!.
|
1833
3340
|
#
|
1834
3341
|
def upcase!: () -> self?
|
1835
3342
|
| (:ascii | :lithuanian | :turkic) -> self?
|
1836
3343
|
| (:lithuanian, :turkic) -> self?
|
1837
3344
|
| (:turkic, :lithuanian) -> self?
|
1838
3345
|
|
1839
|
-
#
|
1840
|
-
#
|
1841
|
-
#
|
1842
|
-
#
|
1843
|
-
#
|
1844
|
-
#
|
1845
|
-
#
|
3346
|
+
# <!--
|
3347
|
+
# rdoc-file=string.c
|
3348
|
+
# - upto(other_string, exclusive = false) {|string| ... } -> self
|
3349
|
+
# - upto(other_string, exclusive = false) -> new_enumerator
|
3350
|
+
# -->
|
3351
|
+
# With a block given, calls the block with each String value returned by
|
3352
|
+
# successive calls to String#succ; the first value is `self`, the next is
|
3353
|
+
# `self.succ`, and so on; the sequence terminates when value `other_string` is
|
3354
|
+
# reached; returns `self`:
|
1846
3355
|
#
|
1847
|
-
#
|
1848
|
-
# for s in "a8".."b6"
|
1849
|
-
# print s, ' '
|
1850
|
-
# end
|
3356
|
+
# 'a8'.upto('b6') {|s| print s, ' ' } # => "a8"
|
1851
3357
|
#
|
1852
|
-
#
|
3358
|
+
# Output:
|
1853
3359
|
#
|
1854
3360
|
# a8 a9 b0 b1 b2 b3 b4 b5 b6
|
1855
|
-
# a8 a9 b0 b1 b2 b3 b4 b5 b6
|
1856
3361
|
#
|
1857
|
-
# If
|
1858
|
-
#
|
1859
|
-
#
|
3362
|
+
# If argument `exclusive` is given as a truthy object, the last value is
|
3363
|
+
# omitted:
|
3364
|
+
#
|
3365
|
+
# 'a8'.upto('b6', true) {|s| print s, ' ' } # => "a8"
|
3366
|
+
#
|
3367
|
+
# Output:
|
1860
3368
|
#
|
1861
|
-
#
|
1862
|
-
#
|
1863
|
-
#
|
3369
|
+
# a8 a9 b0 b1 b2 b3 b4 b5
|
3370
|
+
#
|
3371
|
+
# If `other_string` would not be reached, does not call the block:
|
3372
|
+
#
|
3373
|
+
# '25'.upto('5') {|s| fail s }
|
3374
|
+
# 'aa'.upto('a') {|s| fail s }
|
3375
|
+
#
|
3376
|
+
# With no block given, returns a new Enumerator:
|
3377
|
+
#
|
3378
|
+
# 'a8'.upto('b6') # => #<Enumerator: "a8":upto("b6")>
|
1864
3379
|
#
|
1865
3380
|
def upto: (string other_str, ?boolish exclusive) -> Enumerator[String, self]
|
1866
3381
|
| (string other_str, ?boolish exclusive) { (String s) -> void } -> self
|
1867
3382
|
|
3383
|
+
# <!--
|
3384
|
+
# rdoc-file=string.c
|
3385
|
+
# - str.valid_encoding? -> true or false
|
3386
|
+
# -->
|
1868
3387
|
# Returns true for a string which is encoded correctly.
|
1869
3388
|
#
|
1870
3389
|
# "\xc2\xa1".force_encoding("UTF-8").valid_encoding? #=> true
|
@@ -1875,22 +3394,59 @@ class String
|
|
1875
3394
|
|
1876
3395
|
private
|
1877
3396
|
|
1878
|
-
#
|
1879
|
-
#
|
1880
|
-
#
|
1881
|
-
# string
|
1882
|
-
#
|
1883
|
-
#
|
1884
|
-
#
|
1885
|
-
#
|
1886
|
-
#
|
3397
|
+
# <!--
|
3398
|
+
# rdoc-file=string.c
|
3399
|
+
# - String.new(string = '') -> new_string
|
3400
|
+
# - String.new(string = '', encoding: encoding) -> new_string
|
3401
|
+
# - String.new(string = '', capacity: size) -> new_string
|
3402
|
+
# -->
|
3403
|
+
# Returns a new String that is a copy of `string`.
|
3404
|
+
#
|
3405
|
+
# With no arguments, returns the empty string with the Encoding `ASCII-8BIT`:
|
3406
|
+
# s = String.new
|
3407
|
+
# s # => ""
|
3408
|
+
# s.encoding # => #<Encoding:ASCII-8BIT>
|
3409
|
+
#
|
3410
|
+
# With the single String argument `string`, returns a copy of `string` with the
|
3411
|
+
# same encoding as `string`:
|
3412
|
+
# s = String.new("Que veut dire \u{e7}a?")
|
3413
|
+
# s # => "Que veut dire \u{e7}a?"
|
3414
|
+
# s.encoding # => #<Encoding:UTF-8>
|
3415
|
+
#
|
3416
|
+
# Literal strings like `""` or here-documents always use [script
|
3417
|
+
# encoding](Encoding.html#class-Encoding-label-Script+encoding), unlike
|
3418
|
+
# String.new.
|
3419
|
+
#
|
3420
|
+
# With keyword `encoding`, returns a copy of `str` with the specified encoding:
|
3421
|
+
# s = String.new(encoding: 'ASCII')
|
3422
|
+
# s.encoding # => #<Encoding:US-ASCII>
|
3423
|
+
# s = String.new('foo', encoding: 'ASCII')
|
3424
|
+
# s.encoding # => #<Encoding:US-ASCII>
|
3425
|
+
#
|
3426
|
+
# Note that these are equivalent:
|
3427
|
+
# s0 = String.new('foo', encoding: 'ASCII')
|
3428
|
+
# s1 = 'foo'.force_encoding('ASCII')
|
3429
|
+
# s0.encoding == s1.encoding # => true
|
3430
|
+
#
|
3431
|
+
# With keyword `capacity`, returns a copy of `str`; the given `capacity` may set
|
3432
|
+
# the size of the internal buffer, which may affect performance:
|
3433
|
+
# String.new(capacity: 1) # => ""
|
3434
|
+
# String.new(capacity: 4096) # => ""
|
3435
|
+
#
|
3436
|
+
# The `string`, `encoding`, and `capacity` arguments may all be used together:
|
3437
|
+
#
|
3438
|
+
# String.new('hello', encoding: 'UTF-8', capacity: 25)
|
1887
3439
|
#
|
1888
3440
|
def initialize: (?string str, ?encoding: encoding, ?capacity: int) -> void
|
1889
3441
|
|
1890
|
-
#
|
3442
|
+
# <!--
|
3443
|
+
# rdoc-file=string.c
|
3444
|
+
# - replace(other_string) -> self
|
3445
|
+
# -->
|
3446
|
+
# Replaces the contents of `self` with the contents of `other_string`:
|
1891
3447
|
#
|
1892
|
-
# s =
|
1893
|
-
# s.replace
|
3448
|
+
# s = 'foo' # => "foo"
|
3449
|
+
# s.replace('bar') # => "bar"
|
1894
3450
|
#
|
1895
3451
|
alias initialize_copy replace
|
1896
3452
|
end
|