rbs 3.0.0.dev.2 → 3.0.0.dev.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/comments.yml +2 -1
- data/.github/workflows/ruby.yml +4 -0
- data/Gemfile.lock +11 -11
- data/Rakefile +2 -2
- data/Steepfile +1 -1
- data/core/array.rbs +573 -423
- data/core/basic_object.rbs +11 -39
- data/core/binding.rbs +1 -1
- data/core/builtin.rbs +8 -0
- data/core/class.rbs +37 -0
- data/core/comparable.rbs +7 -18
- data/core/complex.rbs +2 -2
- data/core/data.rbs +419 -0
- data/core/dir.rbs +52 -104
- data/core/encoding.rbs +22 -181
- data/core/enumerable.rbs +212 -175
- data/core/enumerator/product.rbs +96 -0
- data/core/enumerator.rbs +57 -8
- data/core/errors.rbs +8 -2
- data/core/exception.rbs +41 -0
- data/core/fiber.rbs +95 -12
- data/core/file.rbs +840 -275
- data/core/file_test.rbs +34 -19
- data/core/float.rbs +40 -96
- data/core/gc.rbs +15 -3
- data/core/hash.rbs +113 -175
- data/core/integer.rbs +85 -145
- data/core/io/buffer.rbs +187 -60
- data/core/io/wait.rbs +28 -16
- data/core/io.rbs +1859 -1389
- data/core/kernel.rbs +525 -961
- data/core/match_data.rbs +306 -142
- data/core/math.rbs +506 -234
- data/core/method.rbs +0 -24
- data/core/module.rbs +110 -17
- data/core/nil_class.rbs +2 -0
- data/core/numeric.rbs +76 -144
- data/core/object.rbs +88 -212
- data/core/proc.rbs +17 -5
- data/core/process.rbs +22 -5
- data/core/ractor.rbs +1 -1
- data/core/random.rbs +20 -3
- data/core/range.rbs +91 -89
- data/core/rational.rbs +2 -3
- data/core/rbs/unnamed/argf.rbs +177 -120
- data/core/rbs/unnamed/env_class.rbs +89 -163
- data/core/rbs/unnamed/random.rbs +36 -12
- data/core/refinement.rbs +8 -0
- data/core/regexp.rbs +462 -272
- data/core/ruby_vm.rbs +210 -0
- data/{stdlib/set/0 → core}/set.rbs +43 -47
- data/core/string.rbs +1403 -1332
- data/core/string_io.rbs +191 -107
- data/core/struct.rbs +67 -63
- data/core/symbol.rbs +187 -201
- data/core/thread.rbs +40 -35
- data/core/time.rbs +902 -826
- data/core/trace_point.rbs +55 -6
- data/core/unbound_method.rbs +48 -24
- data/docs/collection.md +4 -0
- data/docs/syntax.md +55 -0
- data/ext/rbs_extension/parser.c +5 -6
- data/lib/rbs/cli.rb +6 -1
- data/lib/rbs/collection/cleaner.rb +8 -1
- data/lib/rbs/collection/config/lockfile.rb +3 -1
- data/lib/rbs/collection/config/lockfile_generator.rb +16 -14
- data/lib/rbs/collection/config.rb +1 -1
- data/lib/rbs/collection/sources/git.rb +9 -2
- data/lib/rbs/collection/sources/local.rb +79 -0
- data/lib/rbs/collection/sources.rb +8 -1
- data/lib/rbs/environment.rb +6 -5
- data/lib/rbs/environment_loader.rb +3 -2
- data/lib/rbs/errors.rb +18 -0
- data/lib/rbs/locator.rb +26 -7
- data/lib/rbs/sorter.rb +2 -2
- data/lib/rbs/version.rb +1 -1
- data/sig/collection/sources.rbs +32 -3
- data/sig/environment.rbs +2 -3
- data/sig/locator.rbs +14 -2
- data/sig/shims/{abstract_syntax_tree.rbs → _abstract_syntax_tree.rbs} +0 -0
- data/stdlib/bigdecimal/0/big_decimal.rbs +16 -13
- data/stdlib/cgi/0/core.rbs +16 -0
- data/stdlib/coverage/0/coverage.rbs +50 -8
- data/stdlib/csv/0/csv.rbs +1 -1
- data/stdlib/date/0/date.rbs +856 -726
- data/stdlib/date/0/date_time.rbs +83 -210
- data/stdlib/erb/0/erb.rbs +13 -36
- data/stdlib/etc/0/etc.rbs +127 -20
- data/stdlib/fileutils/0/fileutils.rbs +1290 -381
- data/stdlib/logger/0/logger.rbs +466 -316
- data/stdlib/net-http/0/net-http.rbs +2211 -534
- data/stdlib/nkf/0/nkf.rbs +5 -5
- data/stdlib/objspace/0/objspace.rbs +31 -14
- data/stdlib/openssl/0/openssl.rbs +11 -7
- data/stdlib/optparse/0/optparse.rbs +20 -17
- data/stdlib/pathname/0/pathname.rbs +21 -4
- data/stdlib/pstore/0/pstore.rbs +378 -154
- data/stdlib/pty/0/pty.rbs +24 -8
- data/stdlib/ripper/0/ripper.rbs +1650 -0
- data/stdlib/socket/0/addrinfo.rbs +9 -15
- data/stdlib/socket/0/socket.rbs +36 -3
- data/stdlib/strscan/0/string_scanner.rbs +7 -5
- data/stdlib/tempfile/0/tempfile.rbs +104 -44
- data/stdlib/time/0/time.rbs +2 -2
- data/stdlib/uri/0/file.rbs +5 -0
- data/stdlib/uri/0/generic.rbs +2 -2
- data/stdlib/yaml/0/yaml.rbs +2 -2
- data/stdlib/zlib/0/zlib.rbs +1 -1
- metadata +8 -6
- data/core/deprecated.rbs +0 -9
- data/sig/shims/ripper.rbs +0 -8
data/core/string.rbs
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# <!-- rdoc-file=string.
|
1
|
+
# <!-- rdoc-file=string.rb -->
|
2
2
|
# A String object has an arbitrary sequence of bytes, typically representing
|
3
3
|
# text or binary data. A String object may be created using String::new or as
|
4
4
|
# literals.
|
@@ -8,14 +8,13 @@
|
|
8
8
|
#
|
9
9
|
# You can create a String object explicitly with:
|
10
10
|
#
|
11
|
-
# * A [string literal](
|
12
|
-
# * A [heredoc
|
13
|
-
# literal](doc/syntax/literals_rdoc.html#label-Here+Document+Literals).
|
11
|
+
# * A [string literal](rdoc-ref:syntax/literals.rdoc@String+Literals).
|
12
|
+
# * A [heredoc literal](rdoc-ref:syntax/literals.rdoc@Here+Document+Literals).
|
14
13
|
#
|
15
14
|
#
|
16
15
|
# You can convert certain objects to Strings with:
|
17
16
|
#
|
18
|
-
# * Method
|
17
|
+
# * Method #String.
|
19
18
|
#
|
20
19
|
#
|
21
20
|
# Some String methods modify `self`. Typically, a method whose name ends with
|
@@ -57,11 +56,11 @@
|
|
57
56
|
# Argument `pattern` is commonly a regular expression:
|
58
57
|
#
|
59
58
|
# s = 'hello'
|
60
|
-
# s.sub(/[aeiou]/, '*')
|
59
|
+
# s.sub(/[aeiou]/, '*')# => "h*llo"
|
61
60
|
# s.gsub(/[aeiou]/, '*') # => "h*ll*"
|
62
|
-
# s.gsub(/[aeiou]/, '')
|
63
|
-
# s.sub(/ell/, 'al')
|
64
|
-
# s.gsub(/xyzzy/, '*')
|
61
|
+
# s.gsub(/[aeiou]/, '')# => "hll"
|
62
|
+
# s.sub(/ell/, 'al') # => "halo"
|
63
|
+
# s.gsub(/xyzzy/, '*') # => "hello"
|
65
64
|
# 'THX1138'.gsub(/\d+/, '00') # => "THX00"
|
66
65
|
#
|
67
66
|
# When `pattern` is a string, all its characters are treated as ordinary
|
@@ -101,8 +100,8 @@
|
|
101
100
|
# Note that `\\\` is interpreted as an escape, i.e., a single backslash.
|
102
101
|
#
|
103
102
|
# Note also that a string literal consumes backslashes. See [String
|
104
|
-
# Literals](
|
105
|
-
#
|
103
|
+
# Literals](rdoc-ref:syntax/literals.rdoc@String+Literals) for details about
|
104
|
+
# string literals.
|
106
105
|
#
|
107
106
|
# A back-reference is typically preceded by an additional backslash. For
|
108
107
|
# example, if you want to write a back-reference `\&` in `replacement` with a
|
@@ -139,281 +138,334 @@
|
|
139
138
|
# Special match variables such as `$1`, `$2`, `$``, `$&`, and `$'` are set
|
140
139
|
# appropriately.
|
141
140
|
#
|
142
|
-
# ##
|
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:
|
141
|
+
# ## Whitespace in Strings
|
153
142
|
#
|
154
|
-
# *
|
155
|
-
#
|
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
|
143
|
+
# In class String, *whitespace* is defined as a contiguous sequence of
|
144
|
+
# characters consisting of any mixture of the following:
|
168
145
|
#
|
169
|
-
#
|
170
|
-
# :
|
146
|
+
# * NL (null): `"\x00"`, `"\u0000"`.
|
147
|
+
# * HT (horizontal tab): `"\x09"`, `"\t"`.
|
148
|
+
# * LF (line feed): `"\x0a"`, `"\n"`.
|
149
|
+
# * VT (vertical tab): `"\x0b"`, `"\v"`.
|
150
|
+
# * FF (form feed): `"\x0c"`, `"\f"`.
|
151
|
+
# * CR (carriage return): `"\x0d"`, `"\r"`.
|
152
|
+
# * SP (space): `"\x20"`, `" "`.
|
171
153
|
#
|
172
|
-
# ::try_convert
|
173
|
-
# : Returns a new string created from a given object.
|
174
154
|
#
|
155
|
+
# Whitespace is relevant for these methods:
|
175
156
|
#
|
157
|
+
# * #lstrip, #lstrip!: strip leading whitespace.
|
158
|
+
# * #rstrip, #rstrip!: strip trailing whitespace.
|
159
|
+
# * #strip, #strip!: strip leading and trailing whitespace.
|
176
160
|
#
|
177
|
-
# ### Methods for a Frozen/Unfrozen String
|
178
161
|
#
|
179
|
-
#
|
180
|
-
# : Returns a string that is not frozen: `self`, if not frozen; `self.dup`
|
181
|
-
# otherwise.
|
162
|
+
# ## String Slices
|
182
163
|
#
|
183
|
-
#
|
184
|
-
# : Returns a string that is frozen: `self`, if already frozen;
|
185
|
-
# `self.freeze` otherwise.
|
164
|
+
# A *slice* of a string is a substring that is selected by certain criteria.
|
186
165
|
#
|
187
|
-
#
|
188
|
-
# : Freezes `self`, if not already frozen; returns `self`.
|
166
|
+
# These instance methods make use of slicing:
|
189
167
|
#
|
168
|
+
# * String#[] (also aliased as String#slice) returns a slice copied from
|
169
|
+
# `self`.
|
170
|
+
# * String#[]= returns a copy of `self` with a slice replaced.
|
171
|
+
# * String#slice! returns `self` with a slice removed.
|
190
172
|
#
|
191
173
|
#
|
192
|
-
#
|
174
|
+
# Each of the above methods takes arguments that determine the slice to be
|
175
|
+
# copied or replaced.
|
193
176
|
#
|
194
|
-
#
|
177
|
+
# The arguments have several forms. For string `string`, the forms are:
|
195
178
|
#
|
196
|
-
#
|
197
|
-
#
|
179
|
+
# * `string[index]`.
|
180
|
+
# * `string[start, length]`.
|
181
|
+
# * `string[range]`.
|
182
|
+
# * `string[regexp, capture = 0]`.
|
183
|
+
# * `string[substring]`.
|
198
184
|
#
|
199
|
-
# #empty?
|
200
|
-
# : Returns `true` if `self.length` is zero; `false` otherwise.
|
201
185
|
#
|
202
|
-
#
|
203
|
-
# : Returns the count of bytes.
|
186
|
+
# **`string[index]`**
|
204
187
|
#
|
205
|
-
#
|
206
|
-
#
|
188
|
+
# When non-negative integer argument `index` is given, the slice is the
|
189
|
+
# 1-character substring found in `self` at character offset `index`:
|
207
190
|
#
|
191
|
+
# 'bar'[0] # => "b"
|
192
|
+
# 'bar'[2] # => "r"
|
193
|
+
# 'bar'[20] # => nil
|
194
|
+
# 'тест'[2] # => "с"
|
195
|
+
# 'こんにちは'[4] # => "は"
|
208
196
|
#
|
197
|
+
# When negative integer `index` is given, the slice begins at the offset given
|
198
|
+
# by counting backward from the end of `self`:
|
209
199
|
#
|
210
|
-
#
|
200
|
+
# 'bar'[-3] # => "b"
|
201
|
+
# 'bar'[-1] # => "r"
|
202
|
+
# 'bar'[-20] # => nil
|
211
203
|
#
|
212
|
-
#
|
213
|
-
# : Returns the index of the first substring that matches a given Regexp
|
214
|
-
# or other object; returns `nil` if no match is found.
|
204
|
+
# **`string[start, length]`**
|
215
205
|
#
|
216
|
-
#
|
217
|
-
#
|
218
|
-
#
|
206
|
+
# When non-negative integer arguments `start` and `length` are given, the slice
|
207
|
+
# begins at character offset `start`, if it exists, and continues for `length`
|
208
|
+
# characters, if available:
|
219
209
|
#
|
220
|
-
# #
|
221
|
-
#
|
222
|
-
#
|
210
|
+
# 'foo'[0, 2] # => "fo"
|
211
|
+
# 'тест'[1, 2] # => "ес"
|
212
|
+
# 'こんにちは'[2, 2] # => "にち"
|
213
|
+
# # Zero length.
|
214
|
+
# 'foo'[2, 0] # => ""
|
215
|
+
# # Length not entirely available.
|
216
|
+
# 'foo'[1, 200] # => "oo"
|
217
|
+
# # Start out of range.
|
218
|
+
# 'foo'[4, 2] # => nil
|
223
219
|
#
|
224
|
-
#
|
225
|
-
#
|
226
|
-
# otherwise.
|
220
|
+
# Special case: if `start` is equal to the length of `self`, the slice is a new
|
221
|
+
# empty string:
|
227
222
|
#
|
228
|
-
# #
|
229
|
-
#
|
230
|
-
# otherwise.
|
223
|
+
# 'foo'[3, 2] # => ""
|
224
|
+
# 'foo'[3, 200] # => ""
|
231
225
|
#
|
232
|
-
#
|
233
|
-
#
|
234
|
-
#
|
226
|
+
# When negative `start` and non-negative `length` are given, the slice beginning
|
227
|
+
# is determined by counting backward from the end of `self`, and the slice
|
228
|
+
# continues for `length` characters, if available:
|
235
229
|
#
|
236
|
-
# #
|
237
|
-
#
|
230
|
+
# 'foo'[-2, 2] # => "oo"
|
231
|
+
# 'foo'[-2, 200] # => "oo"
|
232
|
+
# # Start out of range.
|
233
|
+
# 'foo'[-4, 2] # => nil
|
238
234
|
#
|
239
|
-
#
|
240
|
-
# : Returns `true` if the string ends with any of the given substrings.
|
235
|
+
# When negative `length` is given, there is no slice:
|
241
236
|
#
|
237
|
+
# 'foo'[1, -1] # => nil
|
238
|
+
# 'foo'[-2, -1] # => nil
|
242
239
|
#
|
240
|
+
# **`string[range]`**
|
243
241
|
#
|
244
|
-
#
|
242
|
+
# When Range argument `range` is given, creates a substring of `string` using
|
243
|
+
# the indices in `range`. The slice is then determined as above:
|
245
244
|
#
|
246
|
-
# #
|
247
|
-
#
|
248
|
-
# string.
|
245
|
+
# 'foo'[0..1] # => "fo"
|
246
|
+
# 'foo'[0, 2] # => "fo"
|
249
247
|
#
|
250
|
-
# #
|
251
|
-
#
|
252
|
-
# otherwise.
|
248
|
+
# 'foo'[2...2] # => ""
|
249
|
+
# 'foo'[2, 0] # => ""
|
253
250
|
#
|
254
|
-
# #
|
255
|
-
#
|
256
|
-
# for its encoding.
|
251
|
+
# 'foo'[1..200] # => "oo"
|
252
|
+
# 'foo'[1, 200] # => "oo"
|
257
253
|
#
|
258
|
-
# #
|
259
|
-
#
|
260
|
-
# otherwise.
|
254
|
+
# 'foo'[4..5] # => nil
|
255
|
+
# 'foo'[4, 2] # => nil
|
261
256
|
#
|
257
|
+
# 'foo'[-4..-3] # => nil
|
258
|
+
# 'foo'[-4, 2] # => nil
|
262
259
|
#
|
260
|
+
# 'foo'[3..4] # => ""
|
261
|
+
# 'foo'[3, 2] # => ""
|
263
262
|
#
|
264
|
-
#
|
263
|
+
# 'foo'[-2..-1] # => "oo"
|
264
|
+
# 'foo'[-2, 2] # => "oo"
|
265
265
|
#
|
266
|
-
# #
|
267
|
-
#
|
266
|
+
# 'foo'[-2..197] # => "oo"
|
267
|
+
# 'foo'[-2, 200] # => "oo"
|
268
268
|
#
|
269
|
-
#
|
270
|
-
# : Returns the integer hash code.
|
269
|
+
# **`string[regexp, capture = 0]`**
|
271
270
|
#
|
271
|
+
# When the Regexp argument `regexp` is given, and the `capture` argument is `0`,
|
272
|
+
# the slice is the first matching substring found in `self`:
|
272
273
|
#
|
274
|
+
# 'foo'[/o/] # => "o"
|
275
|
+
# 'foo'[/x/] # => nil
|
276
|
+
# s = 'hello there'
|
277
|
+
# s[/[aeiou](.)\1/] # => "ell"
|
278
|
+
# s[/[aeiou](.)\1/, 0] # => "ell"
|
273
279
|
#
|
274
|
-
#
|
280
|
+
# If argument `capture` is given and not `0`, it should be either an capture
|
281
|
+
# group index (integer) or a capture group name (string or symbol); the slice is
|
282
|
+
# the specified capture (see Regexp@Capturing):
|
275
283
|
#
|
276
|
-
#
|
277
|
-
#
|
284
|
+
# s = 'hello there'
|
285
|
+
# s[/[aeiou](.)\1/, 1] # => "l"
|
286
|
+
# s[/(?<vowel>[aeiou])(?<non_vowel>[^aeiou])/, "non_vowel"] # => "l"
|
287
|
+
# s[/(?<vowel>[aeiou])(?<non_vowel>[^aeiou])/, :vowel] # => "e"
|
278
288
|
#
|
279
|
-
#
|
280
|
-
#
|
289
|
+
# If an invalid capture group index is given, there is no slice. If an invalid
|
290
|
+
# capture group name is given, `IndexError` is raised.
|
281
291
|
#
|
282
|
-
#
|
283
|
-
# : Returns -1, 0, or 1 as a given other string is smaller than, equal to,
|
284
|
-
# or larger than `self`.
|
292
|
+
# **`string[substring]`**
|
285
293
|
#
|
286
|
-
#
|
287
|
-
#
|
288
|
-
# than, equal to, or larger than `self`.
|
294
|
+
# When the single String argument `substring` is given, returns the substring
|
295
|
+
# from `self` if found, otherwise `nil`:
|
289
296
|
#
|
290
|
-
# #
|
291
|
-
#
|
292
|
-
# case folding; `false` otherwise.
|
297
|
+
# 'foo'['oo'] # => "oo"
|
298
|
+
# 'foo'['xx'] # => nil
|
293
299
|
#
|
300
|
+
# ## What's Here
|
294
301
|
#
|
302
|
+
# First, what's elsewhere. Class String:
|
295
303
|
#
|
296
|
-
#
|
304
|
+
# * Inherits from [class Object](rdoc-ref:Object@What-27s+Here).
|
305
|
+
# * Includes [module Comparable](rdoc-ref:Comparable@What-27s+Here).
|
297
306
|
#
|
298
|
-
# Each of these methods modifies `self`.
|
299
307
|
#
|
300
|
-
#
|
308
|
+
# Here, class String provides methods that are useful for:
|
301
309
|
#
|
302
|
-
#
|
303
|
-
#
|
310
|
+
# * [Creating a String](rdoc-ref:String@Methods+for+Creating+a+String)
|
311
|
+
# * [Frozen/Unfrozen
|
312
|
+
# Strings](rdoc-ref:String@Methods+for+a+Frozen-2FUnfrozen+String)
|
313
|
+
# * [Querying](rdoc-ref:String@Methods+for+Querying)
|
314
|
+
# * [Comparing](rdoc-ref:String@Methods+for+Comparing)
|
315
|
+
# * [Modifying a String](rdoc-ref:String@Methods+for+Modifying+a+String)
|
316
|
+
# * [Converting to New
|
317
|
+
# String](rdoc-ref:String@Methods+for+Converting+to+New+String)
|
318
|
+
# * [Converting to
|
319
|
+
# Non-String](rdoc-ref:String@Methods+for+Converting+to+Non--5CString)
|
320
|
+
# * [Iterating](rdoc-ref:String@Methods+for+Iterating)
|
304
321
|
#
|
305
|
-
# #<<
|
306
|
-
# : Returns `self` concatenated with a given string or integer.
|
307
322
|
#
|
323
|
+
# ### Methods for Creating a String
|
308
324
|
#
|
325
|
+
# * ::new: Returns a new string.
|
326
|
+
# * ::try_convert: Returns a new string created from a given object.
|
309
327
|
#
|
310
|
-
# *Substitution*
|
311
328
|
#
|
312
|
-
#
|
313
|
-
# : Replaces the first substring that matches a given pattern with a given
|
314
|
-
# replacement string; returns `self` if any changes, `nil` otherwise.
|
329
|
+
# ### Methods for a Frozen/Unfrozen String
|
315
330
|
#
|
316
|
-
#
|
317
|
-
#
|
318
|
-
#
|
331
|
+
# * #+@: Returns a string that is not frozen: `self`, if not frozen;
|
332
|
+
# `self.dup` otherwise.
|
333
|
+
# * #-@: Returns a string that is frozen: `self`, if already frozen;
|
334
|
+
# `self.freeze` otherwise.
|
335
|
+
# * #freeze: Freezes `self`, if not already frozen; returns `self`.
|
319
336
|
#
|
320
|
-
# #succ!, #next!
|
321
|
-
# : Returns `self` modified to become its own successor.
|
322
337
|
#
|
323
|
-
#
|
324
|
-
# : Returns `self` with its entire content replaced by a given string.
|
338
|
+
# ### Methods for Querying
|
325
339
|
#
|
326
|
-
#
|
327
|
-
# : Returns `self` with its characters in reverse order.
|
340
|
+
# *Counts*
|
328
341
|
#
|
329
|
-
#
|
330
|
-
#
|
331
|
-
#
|
342
|
+
# * #length, #size: Returns the count of characters (not bytes).
|
343
|
+
# * #empty?: Returns `true` if `self.length` is zero; `false` otherwise.
|
344
|
+
# * #bytesize: Returns the count of bytes.
|
345
|
+
# * #count: Returns the count of substrings matching given strings.
|
332
346
|
#
|
333
|
-
# #tr!
|
334
|
-
# : Replaces specified characters in `self` with specified replacement
|
335
|
-
# characters; returns `self` if any changes, `nil` otherwise.
|
336
347
|
#
|
337
|
-
#
|
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.
|
348
|
+
# *Substrings*
|
341
349
|
#
|
350
|
+
# * #=~: Returns the index of the first substring that matches a given Regexp
|
351
|
+
# or other object; returns `nil` if no match is found.
|
352
|
+
# * #index: Returns the index of the *first* occurrence of a given substring;
|
353
|
+
# returns `nil` if none found.
|
354
|
+
# * #rindex: Returns the index of the *last* occurrence of a given substring;
|
355
|
+
# returns `nil` if none found.
|
356
|
+
# * #include?: Returns `true` if the string contains a given substring;
|
357
|
+
# `false` otherwise.
|
358
|
+
# * #match: Returns a MatchData object if the string matches a given Regexp;
|
359
|
+
# `nil` otherwise.
|
360
|
+
# * #match?: Returns `true` if the string matches a given Regexp; `false`
|
361
|
+
# otherwise.
|
362
|
+
# * #start_with?: Returns `true` if the string begins with any of the given
|
363
|
+
# substrings.
|
364
|
+
# * #end_with?: Returns `true` if the string ends with any of the given
|
365
|
+
# substrings.
|
342
366
|
#
|
343
367
|
#
|
344
|
-
# *
|
368
|
+
# *Encodings*
|
345
369
|
#
|
346
|
-
#
|
347
|
-
#
|
348
|
-
#
|
370
|
+
# * #encoding: Returns the Encoding object that represents the encoding of the
|
371
|
+
# string.
|
372
|
+
# * #unicode_normalized?: Returns `true` if the string is in Unicode
|
373
|
+
# normalized form; `false` otherwise.
|
374
|
+
# * #valid_encoding?: Returns `true` if the string contains only characters
|
375
|
+
# that are valid for its encoding.
|
376
|
+
# * #ascii_only?: Returns `true` if the string has only ASCII characters;
|
377
|
+
# `false` otherwise.
|
349
378
|
#
|
350
|
-
# #downcase!
|
351
|
-
# : Downcases all characters; returns `self` if any changes, `nil`
|
352
|
-
# otherwise.
|
353
379
|
#
|
354
|
-
#
|
355
|
-
# : Upcases all characters; returns `self` if any changes, `nil`
|
356
|
-
# otherwise.
|
380
|
+
# *Other*
|
357
381
|
#
|
358
|
-
#
|
359
|
-
# :
|
360
|
-
# returns `self` if any changes, `nil` otherwise.
|
382
|
+
# * #sum: Returns a basic checksum for the string: the sum of each byte.
|
383
|
+
# * #hash: Returns the integer hash code.
|
361
384
|
#
|
362
385
|
#
|
386
|
+
# ### Methods for Comparing
|
363
387
|
#
|
364
|
-
# *
|
388
|
+
# * #==, #===: Returns `true` if a given other string has the same content as
|
389
|
+
# `self`.
|
390
|
+
# * #eql?: Returns `true` if the content is the same as the given other
|
391
|
+
# string.
|
392
|
+
# * #<=>: Returns -1, 0, or 1 as a given other string is smaller than, equal
|
393
|
+
# to, or larger than `self`.
|
394
|
+
# * #casecmp: Ignoring case, returns -1, 0, or 1 as a given other string is
|
395
|
+
# smaller than, equal to, or larger than `self`.
|
396
|
+
# * #casecmp?: Returns `true` if the string is equal to a given string after
|
397
|
+
# Unicode case folding; `false` otherwise.
|
365
398
|
#
|
366
|
-
# #encode!
|
367
|
-
# : Returns `self` with all characters transcoded from one given encoding
|
368
|
-
# into another.
|
369
399
|
#
|
370
|
-
#
|
371
|
-
# : Unicode-normalizes `self`; returns `self`.
|
400
|
+
# ### Methods for Modifying a String
|
372
401
|
#
|
373
|
-
#
|
374
|
-
# : Replaces each invalid byte with a given character; returns `self`.
|
402
|
+
# Each of these methods modifies `self`.
|
375
403
|
#
|
376
|
-
#
|
377
|
-
# : Changes the encoding to a given encoding; returns `self`.
|
404
|
+
# *Insertion*
|
378
405
|
#
|
406
|
+
# * #insert: Returns `self` with a given string inserted at a given offset.
|
407
|
+
# * #<<: Returns `self` concatenated with a given string or integer.
|
379
408
|
#
|
380
409
|
#
|
381
|
-
# *
|
410
|
+
# *Substitution*
|
382
411
|
#
|
383
|
-
#
|
384
|
-
#
|
412
|
+
# * #sub!: Replaces the first substring that matches a given pattern with a
|
413
|
+
# given replacement string; returns `self` if any changes, `nil` otherwise.
|
414
|
+
# * #gsub!: Replaces each substring that matches a given pattern with a given
|
415
|
+
# replacement string; returns `self` if any changes, `nil` otherwise.
|
416
|
+
# * #succ!, #next!: Returns `self` modified to become its own successor.
|
417
|
+
# * #replace: Returns `self` with its entire content replaced by a given
|
418
|
+
# string.
|
419
|
+
# * #reverse!: Returns `self` with its characters in reverse order.
|
420
|
+
# * #setbyte: Sets the byte at a given integer offset to a given value;
|
421
|
+
# returns the argument.
|
422
|
+
# * #tr!: Replaces specified characters in `self` with specified replacement
|
423
|
+
# characters; returns `self` if any changes, `nil` otherwise.
|
424
|
+
# * #tr_s!: Replaces specified characters in `self` with specified replacement
|
425
|
+
# characters, removing duplicates from the substrings that were modified;
|
426
|
+
# returns `self` if any changes, `nil` otherwise.
|
385
427
|
#
|
386
|
-
# #slice!, #[]=
|
387
|
-
# : Removes a substring determined by a given index, start/length, range,
|
388
|
-
# regexp, or substring.
|
389
428
|
#
|
390
|
-
#
|
391
|
-
# : Removes contiguous duplicate characters; returns `self`.
|
429
|
+
# *Casing*
|
392
430
|
#
|
393
|
-
#
|
394
|
-
#
|
395
|
-
#
|
431
|
+
# * #capitalize!: Upcases the initial character and downcases all others;
|
432
|
+
# returns `self` if any changes, `nil` otherwise.
|
433
|
+
# * #downcase!: Downcases all characters; returns `self` if any changes, `nil`
|
434
|
+
# otherwise.
|
435
|
+
# * #upcase!: Upcases all characters; returns `self` if any changes, `nil`
|
436
|
+
# otherwise.
|
437
|
+
# * #swapcase!: Upcases each downcase character and downcases each upcase
|
438
|
+
# character; returns `self` if any changes, `nil` otherwise.
|
396
439
|
#
|
397
|
-
# #lstrip!
|
398
|
-
# : Removes leading whitespace; returns `self` if any changes, `nil`
|
399
|
-
# otherwise.
|
400
440
|
#
|
401
|
-
#
|
402
|
-
# : Removes trailing whitespace; returns `self` if any changes, `nil`
|
403
|
-
# otherwise.
|
441
|
+
# *Encoding*
|
404
442
|
#
|
405
|
-
#
|
406
|
-
#
|
407
|
-
#
|
443
|
+
# * #encode!: Returns `self` with all characters transcoded from one given
|
444
|
+
# encoding into another.
|
445
|
+
# * #unicode_normalize!: Unicode-normalizes `self`; returns `self`.
|
446
|
+
# * #scrub!: Replaces each invalid byte with a given character; returns
|
447
|
+
# `self`.
|
448
|
+
# * #force_encoding: Changes the encoding to a given encoding; returns `self`.
|
408
449
|
#
|
409
|
-
# #chomp!
|
410
|
-
# : Removes trailing record separator, if found; returns `self` if any
|
411
|
-
# changes, `nil` otherwise.
|
412
450
|
#
|
413
|
-
#
|
414
|
-
# : Removes trailing whitespace if found, otherwise removes the last
|
415
|
-
# character; returns `self` if any changes, `nil` otherwise.
|
451
|
+
# *Deletion*
|
416
452
|
#
|
453
|
+
# * #clear: Removes all content, so that `self` is empty; returns `self`.
|
454
|
+
# * #slice!, #[]=: Removes a substring determined by a given index,
|
455
|
+
# start/length, range, regexp, or substring.
|
456
|
+
# * #squeeze!: Removes contiguous duplicate characters; returns `self`.
|
457
|
+
# * #delete!: Removes characters as determined by the intersection of
|
458
|
+
# substring arguments.
|
459
|
+
# * #lstrip!: Removes leading whitespace; returns `self` if any changes, `nil`
|
460
|
+
# otherwise.
|
461
|
+
# * #rstrip!: Removes trailing whitespace; returns `self` if any changes,
|
462
|
+
# `nil` otherwise.
|
463
|
+
# * #strip!: Removes leading and trailing whitespace; returns `self` if any
|
464
|
+
# changes, `nil` otherwise.
|
465
|
+
# * #chomp!: Removes trailing record separator, if found; returns `self` if
|
466
|
+
# any changes, `nil` otherwise.
|
467
|
+
# * #chop!: Removes trailing newline characters if found; otherwise removes
|
468
|
+
# the last character; returns `self` if any changes, `nil` otherwise.
|
417
469
|
#
|
418
470
|
#
|
419
471
|
# ### Methods for Converting to New String
|
@@ -423,156 +475,85 @@
|
|
423
475
|
#
|
424
476
|
# *Extension*
|
425
477
|
#
|
426
|
-
#
|
427
|
-
#
|
428
|
-
#
|
429
|
-
#
|
430
|
-
# :
|
431
|
-
#
|
432
|
-
#
|
433
|
-
# :
|
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
|
-
#
|
478
|
+
# * #*: Returns the concatenation of multiple copies of `self`,
|
479
|
+
# * #+: Returns the concatenation of `self` and a given other string.
|
480
|
+
# * #center: Returns a copy of `self` centered between pad substring.
|
481
|
+
# * #concat: Returns the concatenation of `self` with given other strings.
|
482
|
+
# * #prepend: Returns the concatenation of a given other string with `self`.
|
483
|
+
# * #ljust: Returns a copy of `self` of a given length, right-padded with a
|
484
|
+
# given other string.
|
485
|
+
# * #rjust: Returns a copy of `self` of a given length, left-padded with a
|
486
|
+
# given other string.
|
449
487
|
#
|
450
488
|
#
|
451
489
|
# *Encoding*
|
452
490
|
#
|
453
|
-
#
|
454
|
-
# :
|
455
|
-
#
|
456
|
-
#
|
457
|
-
#
|
458
|
-
#
|
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
|
-
#
|
491
|
+
# * #b: Returns a copy of `self` with ASCII-8BIT encoding.
|
492
|
+
# * #scrub: Returns a copy of `self` with each invalid byte replaced with a
|
493
|
+
# given character.
|
494
|
+
# * #unicode_normalize: Returns a copy of `self` with each character
|
495
|
+
# Unicode-normalized.
|
496
|
+
# * #encode: Returns a copy of `self` with all characters transcoded from one
|
497
|
+
# given encoding into another.
|
467
498
|
#
|
468
499
|
#
|
469
500
|
# *Substitution*
|
470
501
|
#
|
471
|
-
#
|
472
|
-
#
|
473
|
-
#
|
474
|
-
#
|
475
|
-
#
|
476
|
-
#
|
477
|
-
#
|
478
|
-
#
|
479
|
-
#
|
480
|
-
# :
|
481
|
-
#
|
482
|
-
#
|
483
|
-
#
|
484
|
-
#
|
485
|
-
#
|
486
|
-
#
|
487
|
-
#
|
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
|
-
#
|
502
|
+
# * #dump: Returns a copy of `self` with all non-printing characters replaced
|
503
|
+
# by xHH notation and all special characters escaped.
|
504
|
+
# * #undump: Returns a copy of `self` with all `\xNN` notation replace by
|
505
|
+
# `\uNNNN` notation and all escaped characters unescaped.
|
506
|
+
# * #sub: Returns a copy of `self` with the first substring matching a given
|
507
|
+
# pattern replaced with a given replacement string;.
|
508
|
+
# * #gsub: Returns a copy of `self` with each substring that matches a given
|
509
|
+
# pattern replaced with a given replacement string.
|
510
|
+
# * #succ, #next: Returns the string that is the successor to `self`.
|
511
|
+
# * #reverse: Returns a copy of `self` with its characters in reverse order.
|
512
|
+
# * #tr: Returns a copy of `self` with specified characters replaced with
|
513
|
+
# specified replacement characters.
|
514
|
+
# * #tr_s: Returns a copy of `self` with specified characters replaced with
|
515
|
+
# specified replacement characters, removing duplicates from the substrings
|
516
|
+
# that were modified.
|
517
|
+
# * #%: Returns the string resulting from formatting a given object into
|
518
|
+
# `self`
|
506
519
|
#
|
507
520
|
#
|
508
521
|
# *Casing*
|
509
522
|
#
|
510
|
-
#
|
511
|
-
#
|
512
|
-
#
|
513
|
-
#
|
514
|
-
#
|
515
|
-
#
|
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
|
-
#
|
523
|
+
# * #capitalize: Returns a copy of `self` with the first character upcased and
|
524
|
+
# all other characters downcased.
|
525
|
+
# * #downcase: Returns a copy of `self` with all characters downcased.
|
526
|
+
# * #upcase: Returns a copy of `self` with all characters upcased.
|
527
|
+
# * #swapcase: Returns a copy of `self` with all upcase characters downcased
|
528
|
+
# and all downcase characters upcased.
|
524
529
|
#
|
525
530
|
#
|
526
531
|
# *Deletion*
|
527
532
|
#
|
528
|
-
#
|
529
|
-
# :
|
530
|
-
#
|
531
|
-
#
|
532
|
-
# :
|
533
|
-
#
|
534
|
-
#
|
535
|
-
# :
|
536
|
-
#
|
537
|
-
#
|
538
|
-
#
|
539
|
-
#
|
540
|
-
#
|
541
|
-
# :
|
542
|
-
#
|
543
|
-
#
|
544
|
-
#
|
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
|
-
#
|
533
|
+
# * #delete: Returns a copy of `self` with characters removed
|
534
|
+
# * #delete_prefix: Returns a copy of `self` with a given prefix removed.
|
535
|
+
# * #delete_suffix: Returns a copy of `self` with a given suffix removed.
|
536
|
+
# * #lstrip: Returns a copy of `self` with leading whitespace removed.
|
537
|
+
# * #rstrip: Returns a copy of `self` with trailing whitespace removed.
|
538
|
+
# * #strip: Returns a copy of `self` with leading and trailing whitespace
|
539
|
+
# removed.
|
540
|
+
# * #chomp: Returns a copy of `self` with a trailing record separator removed,
|
541
|
+
# if found.
|
542
|
+
# * #chop: Returns a copy of `self` with trailing newline characters or the
|
543
|
+
# last character removed.
|
544
|
+
# * #squeeze: Returns a copy of `self` with contiguous duplicate characters
|
545
|
+
# removed.
|
546
|
+
# * #[], #slice: Returns a substring determined by a given index,
|
547
|
+
# start/length, or range, or string.
|
548
|
+
# * #byteslice: Returns a substring determined by a given index, start/length,
|
549
|
+
# or range.
|
550
|
+
# * #chr: Returns the first character.
|
568
551
|
#
|
569
552
|
#
|
570
553
|
# *Duplication*
|
571
554
|
#
|
572
|
-
#
|
573
|
-
#
|
574
|
-
# String; otherwise, returns `self`.
|
575
|
-
#
|
555
|
+
# * #to_s, $to_str: If `self` is a subclass of String, returns `self` copied
|
556
|
+
# into a String; otherwise, returns `self`.
|
576
557
|
#
|
577
558
|
#
|
578
559
|
# ### Methods for Converting to Non-String
|
@@ -581,116 +562,69 @@
|
|
581
562
|
#
|
582
563
|
# *Characters, Bytes, and Clusters*
|
583
564
|
#
|
584
|
-
#
|
585
|
-
# :
|
586
|
-
#
|
587
|
-
#
|
588
|
-
# :
|
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
|
-
#
|
565
|
+
# * #bytes: Returns an array of the bytes in `self`.
|
566
|
+
# * #chars: Returns an array of the characters in `self`.
|
567
|
+
# * #codepoints: Returns an array of the integer ordinals in `self`.
|
568
|
+
# * #getbyte: Returns an integer byte as determined by a given index.
|
569
|
+
# * #grapheme_clusters: Returns an array of the grapheme clusters in `self`.
|
599
570
|
#
|
600
571
|
#
|
601
572
|
# *Splitting*
|
602
573
|
#
|
603
|
-
#
|
604
|
-
#
|
605
|
-
#
|
606
|
-
#
|
607
|
-
#
|
608
|
-
#
|
609
|
-
#
|
610
|
-
#
|
611
|
-
#
|
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
|
-
#
|
574
|
+
# * #lines: Returns an array of the lines in `self`, as determined by a given
|
575
|
+
# record separator.
|
576
|
+
# * #partition: Returns a 3-element array determined by the first substring
|
577
|
+
# that matches a given substring or regexp,
|
578
|
+
# * #rpartition: Returns a 3-element array determined by the last substring
|
579
|
+
# that matches a given substring or regexp,
|
580
|
+
# * #split: Returns an array of substrings determined by a given delimiter --
|
581
|
+
# regexp or string -- or, if a block given, passes those substrings to the
|
582
|
+
# block.
|
620
583
|
#
|
621
584
|
#
|
622
585
|
# *Matching*
|
623
586
|
#
|
624
|
-
#
|
625
|
-
#
|
626
|
-
#
|
627
|
-
#
|
628
|
-
#
|
629
|
-
#
|
630
|
-
# given format.
|
631
|
-
#
|
632
|
-
# #unpack1
|
633
|
-
# : Returns the first substring extracted from `self` according to a given
|
634
|
-
# format.
|
635
|
-
#
|
587
|
+
# * #scan: Returns an array of substrings matching a given regexp or string,
|
588
|
+
# or, if a block given, passes each matching substring to the block.
|
589
|
+
# * #unpack: Returns an array of substrings extracted from `self` according to
|
590
|
+
# a given format.
|
591
|
+
# * #unpack1: Returns the first substring extracted from `self` according to a
|
592
|
+
# given format.
|
636
593
|
#
|
637
594
|
#
|
638
595
|
# *Numerics*
|
639
596
|
#
|
640
|
-
#
|
641
|
-
#
|
642
|
-
#
|
643
|
-
#
|
644
|
-
#
|
645
|
-
# :
|
646
|
-
#
|
647
|
-
#
|
648
|
-
#
|
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
|
-
#
|
597
|
+
# * #hex: Returns the integer value of the leading characters, interpreted as
|
598
|
+
# hexadecimal digits.
|
599
|
+
# * #oct: Returns the integer value of the leading characters, interpreted as
|
600
|
+
# octal digits.
|
601
|
+
# * #ord: Returns the integer ordinal of the first character in `self`.
|
602
|
+
# * #to_i: Returns the integer value of leading characters, interpreted as an
|
603
|
+
# integer.
|
604
|
+
# * #to_f: Returns the floating-point value of leading characters, interpreted
|
605
|
+
# as a floating-point number.
|
659
606
|
#
|
660
607
|
#
|
661
608
|
# *Strings and Symbols*
|
662
609
|
#
|
663
|
-
#
|
664
|
-
#
|
665
|
-
#
|
666
|
-
#
|
667
|
-
# #to_sym, #intern
|
668
|
-
# : Returns the symbol corresponding to `self`.
|
669
|
-
#
|
610
|
+
# * #inspect: Returns copy of `self`, enclosed in double-quotes, with special
|
611
|
+
# characters escaped.
|
612
|
+
# * #to_sym, #intern: Returns the symbol corresponding to `self`.
|
670
613
|
#
|
671
614
|
#
|
672
615
|
# ### Methods for Iterating
|
673
616
|
#
|
674
|
-
#
|
675
|
-
# :
|
676
|
-
#
|
677
|
-
#
|
678
|
-
#
|
679
|
-
#
|
680
|
-
#
|
681
|
-
# :
|
682
|
-
#
|
683
|
-
#
|
684
|
-
# #
|
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.
|
617
|
+
# * #each_byte: Calls the given block with each successive byte in `self`.
|
618
|
+
# * #each_char: Calls the given block with each successive character in
|
619
|
+
# `self`.
|
620
|
+
# * #each_codepoint: Calls the given block with each successive integer
|
621
|
+
# codepoint in `self`.
|
622
|
+
# * #each_grapheme_cluster: Calls the given block with each successive
|
623
|
+
# grapheme cluster in `self`.
|
624
|
+
# * #each_line: Calls the given block with each successive line in `self`, as
|
625
|
+
# determined by a given record separator.
|
626
|
+
# * #upto: Calls the given block with each string value returned by successive
|
627
|
+
# calls to #succ.
|
694
628
|
#
|
695
629
|
class String
|
696
630
|
include Comparable
|
@@ -759,7 +693,7 @@ class String
|
|
759
693
|
# -->
|
760
694
|
# Returns `self` if `self` is not frozen.
|
761
695
|
#
|
762
|
-
# Otherwise
|
696
|
+
# Otherwise returns `self.dup`, which is not frozen.
|
763
697
|
#
|
764
698
|
def +@: () -> String
|
765
699
|
|
@@ -770,7 +704,9 @@ class String
|
|
770
704
|
# Returns a frozen, possibly pre-existing copy of the string.
|
771
705
|
#
|
772
706
|
# The returned String will be deduplicated as long as it does not have any
|
773
|
-
# instance variables set on it.
|
707
|
+
# instance variables set on it and is not a String subclass.
|
708
|
+
#
|
709
|
+
# String#dedup is an alias for String#-@.
|
774
710
|
#
|
775
711
|
def -@: () -> String
|
776
712
|
|
@@ -868,14 +804,13 @@ class String
|
|
868
804
|
# 'foo' =~ /o/ # => 1
|
869
805
|
# 'foo' =~ /x/ # => nil
|
870
806
|
#
|
871
|
-
# Note: also updates
|
872
|
-
# variables](Regexp.html#class-Regexp-label-Special+global+variables).
|
807
|
+
# Note: also updates Regexp@Special+global+variables.
|
873
808
|
#
|
874
809
|
# If the given `object` is not a Regexp, returns the value returned by `object
|
875
810
|
# =~ self`.
|
876
811
|
#
|
877
812
|
# Note that `string =~ regexp` is different from `regexp =~ string` (see
|
878
|
-
#
|
813
|
+
# Regexp#=~):
|
879
814
|
#
|
880
815
|
# number= nil
|
881
816
|
# "no. 9" =~ /(?<number>\d+)/
|
@@ -893,152 +828,196 @@ class String
|
|
893
828
|
# - string[regexp, capture = 0] -> new_string or nil
|
894
829
|
# - string[substring] -> new_string or nil
|
895
830
|
# -->
|
896
|
-
# Returns the substring of `self` specified by the arguments.
|
831
|
+
# Returns the substring of `self` specified by the arguments. See examples at
|
832
|
+
# [String Slices](rdoc-ref:String@String+Slices).
|
897
833
|
#
|
898
|
-
|
899
|
-
|
834
|
+
def []: (int index) -> String?
|
835
|
+
| (int start, int length) -> String?
|
836
|
+
| (Range[Integer] | Range[Integer?] range) -> String?
|
837
|
+
| (Regexp regexp) -> String?
|
838
|
+
| (Regexp regexp, int | String capture) -> String?
|
839
|
+
| (String match_str) -> String?
|
840
|
+
|
841
|
+
# <!--
|
842
|
+
# rdoc-file=string.c
|
843
|
+
# - string[index] = new_string
|
844
|
+
# - string[start, length] = new_string
|
845
|
+
# - string[range] = new_string
|
846
|
+
# - string[regexp, capture = 0] = new_string
|
847
|
+
# - string[substring] = new_string
|
848
|
+
# -->
|
849
|
+
# Replaces all, some, or none of the contents of `self`; returns `new_string`.
|
850
|
+
# See [String Slices](rdoc-ref:String@String+Slices).
|
900
851
|
#
|
901
|
-
#
|
852
|
+
# A few examples:
|
902
853
|
#
|
903
|
-
#
|
854
|
+
# s = 'foo'
|
855
|
+
# s[2] = 'rtune' # => "rtune"
|
856
|
+
# s # => "fortune"
|
857
|
+
# s[1, 5] = 'init' # => "init"
|
858
|
+
# s # => "finite"
|
859
|
+
# s[3..4] = 'al' # => "al"
|
860
|
+
# s # => "finale"
|
861
|
+
# s[/e$/] = 'ly' # => "ly"
|
862
|
+
# s # => "finally"
|
863
|
+
# s['lly'] = 'ncial' # => "ncial"
|
864
|
+
# s # => "financial"
|
904
865
|
#
|
905
|
-
#
|
866
|
+
# String#slice is an alias for String#[].
|
906
867
|
#
|
907
|
-
|
868
|
+
def []=: (int pos, String new_str) -> String
|
869
|
+
| (int begin_pos, int end_pos, String new_str) -> String
|
870
|
+
| (Range[Integer] | Range[Integer?] range, String new_str) -> String
|
871
|
+
| (Regexp regexp, String new_str) -> String
|
872
|
+
| (Regexp regexp, int capture, String new_str) -> String
|
873
|
+
| (Regexp regexp, String name, String new_str) -> String
|
874
|
+
| (String other_str, String new_str) -> String
|
875
|
+
|
876
|
+
# <!--
|
877
|
+
# rdoc-file=string.c
|
878
|
+
# - ascii_only? -> true or false
|
879
|
+
# -->
|
880
|
+
# Returns `true` if `self` contains only ASCII characters, `false` otherwise:
|
908
881
|
#
|
909
|
-
# '
|
910
|
-
#
|
882
|
+
# 'abc'.ascii_only? # => true
|
883
|
+
# "abc\u{6666}".ascii_only? # => false
|
911
884
|
#
|
912
|
-
|
913
|
-
|
885
|
+
def ascii_only?: () -> bool
|
886
|
+
|
887
|
+
# <!--
|
888
|
+
# rdoc-file=string.c
|
889
|
+
# - b -> string
|
890
|
+
# -->
|
891
|
+
# Returns a copy of `self` that has ASCII-8BIT encoding; the underlying bytes
|
892
|
+
# are not modified:
|
914
893
|
#
|
915
|
-
#
|
916
|
-
#
|
894
|
+
# s = "\x99"
|
895
|
+
# s.encoding # => #<Encoding:UTF-8>
|
896
|
+
# t = s.b # => "\x99"
|
897
|
+
# t.encoding # => #<Encoding:ASCII-8BIT>
|
917
898
|
#
|
918
|
-
#
|
899
|
+
# s = "\u4095" # => "䂕"
|
900
|
+
# s.encoding # => #<Encoding:UTF-8>
|
901
|
+
# s.bytes # => [228, 130, 149]
|
902
|
+
# t = s.b # => "\xE4\x82\x95"
|
903
|
+
# t.encoding # => #<Encoding:ASCII-8BIT>
|
904
|
+
# t.bytes # => [228, 130, 149]
|
919
905
|
#
|
920
|
-
|
906
|
+
def b: () -> String
|
907
|
+
|
908
|
+
# <!--
|
909
|
+
# rdoc-file=string.c
|
910
|
+
# - byteindex(substring, offset = 0) -> integer or nil
|
911
|
+
# - byteindex(regexp, offset = 0) -> integer or nil
|
912
|
+
# -->
|
913
|
+
# Returns the Integer byte-based index of the first occurrence of the given
|
914
|
+
# `substring`, or `nil` if none found:
|
921
915
|
#
|
922
|
-
#
|
923
|
-
#
|
916
|
+
# 'foo'.byteindex('f') # => 0
|
917
|
+
# 'foo'.byteindex('o') # => 1
|
918
|
+
# 'foo'.byteindex('oo') # => 1
|
919
|
+
# 'foo'.byteindex('ooo') # => nil
|
924
920
|
#
|
925
|
-
#
|
921
|
+
# Returns the Integer byte-based index of the first match for the given Regexp
|
922
|
+
# `regexp`, or `nil` if none found:
|
926
923
|
#
|
927
|
-
#
|
924
|
+
# 'foo'.byteindex(/f/) # => 0
|
925
|
+
# 'foo'.byteindex(/o/) # => 1
|
926
|
+
# 'foo'.byteindex(/oo/) # => 1
|
927
|
+
# 'foo'.byteindex(/ooo/) # => nil
|
928
928
|
#
|
929
|
-
#
|
930
|
-
#
|
929
|
+
# Integer argument `offset`, if given, specifies the byte-based position in the
|
930
|
+
# string to begin the search:
|
931
931
|
#
|
932
|
-
#
|
932
|
+
# 'foo'.byteindex('o', 1) # => 1
|
933
|
+
# 'foo'.byteindex('o', 2) # => 2
|
934
|
+
# 'foo'.byteindex('o', 3) # => nil
|
933
935
|
#
|
934
|
-
#
|
936
|
+
# If `offset` is negative, counts backward from the end of `self`:
|
935
937
|
#
|
936
|
-
#
|
938
|
+
# 'foo'.byteindex('o', -1) # => 2
|
939
|
+
# 'foo'.byteindex('o', -2) # => 1
|
940
|
+
# 'foo'.byteindex('o', -3) # => 1
|
941
|
+
# 'foo'.byteindex('o', -4) # => nil
|
937
942
|
#
|
938
|
-
#
|
943
|
+
# If `offset` does not land on character (codepoint) boundary, `IndexError` is
|
944
|
+
# raised.
|
939
945
|
#
|
940
|
-
#
|
941
|
-
# values from the given `range`, and returns values as above:
|
946
|
+
# Related: String#index, String#byterindex.
|
942
947
|
#
|
943
|
-
|
944
|
-
|
948
|
+
def byteindex: (Regexp | string substr_or_regexp, ?int offset) -> Integer?
|
949
|
+
|
950
|
+
# <!--
|
951
|
+
# rdoc-file=string.c
|
952
|
+
# - byterindex(substring, offset = self.bytesize) -> integer or nil
|
953
|
+
# - byterindex(regexp, offset = self.bytesize) -> integer or nil
|
954
|
+
# -->
|
955
|
+
# Returns the Integer byte-based index of the *last* occurrence of the given
|
956
|
+
# `substring`, or `nil` if none found:
|
945
957
|
#
|
958
|
+
# 'foo'.byterindex('f') # => 0
|
959
|
+
# 'foo'.byterindex('o') # => 2
|
960
|
+
# 'foo'.byterindex('oo') # => 1
|
961
|
+
# 'foo'.byterindex('ooo') # => nil
|
946
962
|
#
|
947
|
-
#
|
948
|
-
#
|
963
|
+
# Returns the Integer byte-based index of the *last* match for the given Regexp
|
964
|
+
# `regexp`, or `nil` if none found:
|
949
965
|
#
|
950
|
-
# 'foo'
|
951
|
-
# 'foo'
|
952
|
-
#
|
953
|
-
#
|
954
|
-
# s[/[aeiou](.)\1/, 0] # => "ell"
|
966
|
+
# 'foo'.byterindex(/f/) # => 0
|
967
|
+
# 'foo'.byterindex(/o/) # => 2
|
968
|
+
# 'foo'.byterindex(/oo/) # => 1
|
969
|
+
# 'foo'.byterindex(/ooo/) # => nil
|
955
970
|
#
|
956
|
-
#
|
957
|
-
#
|
958
|
-
# returns only the specified capture (see [Regexp
|
959
|
-
# Capturing](Regexp.html#class-Regexp-label-Capturing)):
|
971
|
+
# The *last* match means starting at the possible last position, not the last of
|
972
|
+
# longest matches.
|
960
973
|
#
|
961
|
-
#
|
962
|
-
#
|
963
|
-
# s[/(?<vowel>[aeiou])(?<non_vowel>[^aeiou])/, "non_vowel"] # => "l"
|
964
|
-
# s[/(?<vowel>[aeiou])(?<non_vowel>[^aeiou])/, :vowel] # => "e"
|
974
|
+
# 'foo'.byterindex(/o+/) # => 2
|
975
|
+
# $~ #=> #<MatchData "o">
|
965
976
|
#
|
966
|
-
#
|
967
|
-
# capture group name is given, `IndexError` is raised.
|
977
|
+
# To get the last longest match, needs to combine with negative lookbehind.
|
968
978
|
#
|
969
|
-
#
|
970
|
-
#
|
979
|
+
# 'foo'.byterindex(/(?<!o)o+/) # => 1
|
980
|
+
# $~ #=> #<MatchData "oo">
|
971
981
|
#
|
972
|
-
#
|
973
|
-
# 'foo'['xx'] # => nil
|
982
|
+
# Or String#byteindex with negative lookforward.
|
974
983
|
#
|
975
|
-
#
|
984
|
+
# 'foo'.byteindex(/o+(?!.*o)/) # => 1
|
985
|
+
# $~ #=> #<MatchData "oo">
|
976
986
|
#
|
977
|
-
|
978
|
-
|
979
|
-
|
980
|
-
| (Regexp regexp) -> String?
|
981
|
-
| (Regexp regexp, int | String capture) -> String?
|
982
|
-
| (String match_str) -> String?
|
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
|
-
# -->
|
994
|
-
# Element Assignment---Replaces some or all of the content of *str*. The portion
|
995
|
-
# of the string affected is determined using the same criteria as String#[]. If
|
996
|
-
# the replacement string is not the same length as the text it is replacing, the
|
997
|
-
# string will be adjusted accordingly. If the regular expression or string is
|
998
|
-
# used as the index doesn't match a position in the string, IndexError is
|
999
|
-
# raised. If the regular expression form is used, the optional second Integer
|
1000
|
-
# allows you to specify which portion of the match to replace (effectively using
|
1001
|
-
# the MatchData indexing rules. The forms that take an Integer will raise an
|
1002
|
-
# IndexError if the value is out of range; the Range form will raise a
|
1003
|
-
# RangeError, and the Regexp and String will raise an IndexError on negative
|
1004
|
-
# match.
|
987
|
+
# Integer argument `offset`, if given and non-negative, specifies the maximum
|
988
|
+
# starting byte-based position in the
|
989
|
+
# string to _end_ the search:
|
1005
990
|
#
|
1006
|
-
|
1007
|
-
|
1008
|
-
|
1009
|
-
|
1010
|
-
|
1011
|
-
|
1012
|
-
|
1013
|
-
|
1014
|
-
# <!--
|
1015
|
-
# rdoc-file=string.c
|
1016
|
-
# - str.ascii_only? -> true or false
|
1017
|
-
# -->
|
1018
|
-
# Returns true for a string which has only ASCII characters.
|
991
|
+
# 'foo'.byterindex('o', 0) # => nil
|
992
|
+
# 'foo'.byterindex('o', 1) # => 1
|
993
|
+
# 'foo'.byterindex('o', 2) # => 2
|
994
|
+
# 'foo'.byterindex('o', 3) # => 2
|
995
|
+
#
|
996
|
+
# If `offset` is a negative Integer, the maximum starting position in the string
|
997
|
+
# to *end* the search is the sum of the string's length and `offset`:
|
1019
998
|
#
|
1020
|
-
#
|
1021
|
-
#
|
999
|
+
# 'foo'.byterindex('o', -1) # => 2
|
1000
|
+
# 'foo'.byterindex('o', -2) # => 1
|
1001
|
+
# 'foo'.byterindex('o', -3) # => nil
|
1002
|
+
# 'foo'.byterindex('o', -4) # => nil
|
1022
1003
|
#
|
1023
|
-
|
1024
|
-
|
1025
|
-
# <!--
|
1026
|
-
# rdoc-file=string.c
|
1027
|
-
# - str.b -> str
|
1028
|
-
# -->
|
1029
|
-
# Returns a copied string whose encoding is ASCII-8BIT.
|
1004
|
+
# If `offset` does not land on character (codepoint) boundary, `IndexError` is
|
1005
|
+
# raised.
|
1030
1006
|
#
|
1031
|
-
|
1007
|
+
# Related: String#byteindex.
|
1008
|
+
#
|
1009
|
+
def byterindex: (Regexp | string substr_or_regexp, ?int offset) -> Integer?
|
1032
1010
|
|
1033
1011
|
# <!--
|
1034
1012
|
# rdoc-file=string.c
|
1035
|
-
# -
|
1013
|
+
# - bytes -> array_of_bytes
|
1036
1014
|
# -->
|
1037
|
-
# Returns an array of bytes in
|
1038
|
-
# `str.each_byte.to_a`.
|
1015
|
+
# Returns an array of the bytes in `self`:
|
1039
1016
|
#
|
1040
|
-
#
|
1041
|
-
#
|
1017
|
+
# 'hello'.bytes # => [104, 101, 108, 108, 111]
|
1018
|
+
# 'тест'.bytes # => [209, 130, 208, 181, 209, 129, 209, 130]
|
1019
|
+
# 'こんにちは'.bytes
|
1020
|
+
# # => [227, 129, 147, 227, 130, 147, 227, 129, 171, 227, 129, 161, 227, 129, 175]
|
1042
1021
|
#
|
1043
1022
|
def bytes: () -> Array[Integer]
|
1044
1023
|
| () { (Integer byte) -> void } -> String
|
@@ -1047,12 +1026,17 @@ class String
|
|
1047
1026
|
# rdoc-file=string.c
|
1048
1027
|
# - bytesize -> integer
|
1049
1028
|
# -->
|
1050
|
-
# Returns the count
|
1029
|
+
# Returns the count of bytes (not characters) in `self`:
|
1030
|
+
#
|
1031
|
+
# 'foo'.bytesize # => 3
|
1032
|
+
# 'тест'.bytesize # => 8
|
1033
|
+
# 'こんにちは'.bytesize # => 15
|
1051
1034
|
#
|
1052
|
-
#
|
1053
|
-
# "hello".bytesize # => 5
|
1035
|
+
# Contrast with String#length:
|
1054
1036
|
#
|
1055
|
-
#
|
1037
|
+
# 'foo'.length # => 3
|
1038
|
+
# 'тест'.length # => 4
|
1039
|
+
# 'こんにちは'.length # => 5
|
1056
1040
|
#
|
1057
1041
|
def bytesize: () -> Integer
|
1058
1042
|
|
@@ -1100,6 +1084,23 @@ class String
|
|
1100
1084
|
def byteslice: (int start, ?int length) -> String?
|
1101
1085
|
| (Range[Integer] | Range[Integer?] range) -> String?
|
1102
1086
|
|
1087
|
+
# <!--
|
1088
|
+
# rdoc-file=string.c
|
1089
|
+
# - bytesplice(index, length, str) -> string
|
1090
|
+
# - bytesplice(range, str) -> string
|
1091
|
+
# -->
|
1092
|
+
# Replaces some or all of the content of `self` with `str`, and returns `self`.
|
1093
|
+
# The portion of the string affected is determined using the same criteria as
|
1094
|
+
# String#byteslice, except that `length` cannot be omitted. If the replacement
|
1095
|
+
# string is not the same length as the text it is replacing, the string will be
|
1096
|
+
# adjusted accordingly. The form that take an Integer will raise an IndexError
|
1097
|
+
# if the value is out of range; the Range form will raise a RangeError. If the
|
1098
|
+
# beginning or ending offset does not land on character (codepoint) boundary, an
|
1099
|
+
# IndexError will be raised.
|
1100
|
+
#
|
1101
|
+
def bytesplice: (int index, int length, string str) -> String
|
1102
|
+
| (Range[int?], string str) -> String
|
1103
|
+
|
1103
1104
|
# <!--
|
1104
1105
|
# rdoc-file=string.c
|
1105
1106
|
# - capitalize(*options) -> string
|
@@ -1111,7 +1112,7 @@ class String
|
|
1111
1112
|
# s.capitalize # => "Hello world!"
|
1112
1113
|
#
|
1113
1114
|
# The casing may be affected by the given `options`; see [Case
|
1114
|
-
# Mapping](
|
1115
|
+
# Mapping](rdoc-ref:case_mapping.rdoc).
|
1115
1116
|
#
|
1116
1117
|
# Related: String#capitalize!.
|
1117
1118
|
#
|
@@ -1133,7 +1134,7 @@ class String
|
|
1133
1134
|
# s.capitalize! # => nil
|
1134
1135
|
#
|
1135
1136
|
# The casing may be affected by the given `options`; see [Case
|
1136
|
-
# Mapping](
|
1137
|
+
# Mapping](rdoc-ref:case_mapping.rdoc).
|
1137
1138
|
#
|
1138
1139
|
# Related: String#capitalize.
|
1139
1140
|
#
|
@@ -1163,7 +1164,7 @@ class String
|
|
1163
1164
|
# 'foo'.casecmp('FOO') # => 0
|
1164
1165
|
# 'foo'.casecmp(1) # => nil
|
1165
1166
|
#
|
1166
|
-
# See [Case Mapping](
|
1167
|
+
# See [Case Mapping](rdoc-ref:case_mapping.rdoc).
|
1167
1168
|
#
|
1168
1169
|
# Related: String#casecmp?.
|
1169
1170
|
#
|
@@ -1186,7 +1187,7 @@ class String
|
|
1186
1187
|
#
|
1187
1188
|
# 'foo'.casecmp?(1) # => nil
|
1188
1189
|
#
|
1189
|
-
# See [Case Mapping](
|
1190
|
+
# See [Case Mapping](rdoc-ref:case_mapping.rdoc).
|
1190
1191
|
#
|
1191
1192
|
# Related: String#casecmp.
|
1192
1193
|
#
|
@@ -1194,85 +1195,119 @@ class String
|
|
1194
1195
|
|
1195
1196
|
# <!--
|
1196
1197
|
# rdoc-file=string.c
|
1197
|
-
# -
|
1198
|
+
# - center(size, pad_string = ' ') -> new_string
|
1198
1199
|
# -->
|
1199
|
-
#
|
1200
|
-
#
|
1201
|
-
# `
|
1200
|
+
# Returns a centered copy of `self`.
|
1201
|
+
#
|
1202
|
+
# If integer argument `size` is greater than the size (in characters) of `self`,
|
1203
|
+
# returns a new string of length `size` that is a copy of `self`, centered and
|
1204
|
+
# padded on both ends with `pad_string`:
|
1205
|
+
#
|
1206
|
+
# 'hello'.center(10) # => " hello "
|
1207
|
+
# ' hello'.center(10) # => " hello "
|
1208
|
+
# 'hello'.center(10, 'ab') # => "abhelloaba"
|
1209
|
+
# 'тест'.center(10) # => " тест "
|
1210
|
+
# 'こんにちは'.center(10) # => " こんにちは "
|
1211
|
+
#
|
1212
|
+
# If `size` is not greater than the size of `self`, returns a copy of `self`:
|
1202
1213
|
#
|
1203
|
-
#
|
1204
|
-
#
|
1205
|
-
#
|
1214
|
+
# 'hello'.center(5) # => "hello"
|
1215
|
+
# 'hello'.center(1) # => "hello"
|
1216
|
+
#
|
1217
|
+
# Related: String#ljust, String#rjust.
|
1206
1218
|
#
|
1207
1219
|
def center: (int width, ?string padstr) -> String
|
1208
1220
|
|
1209
1221
|
# <!--
|
1210
1222
|
# rdoc-file=string.c
|
1211
|
-
# -
|
1223
|
+
# - chars -> array_of_characters
|
1212
1224
|
# -->
|
1213
|
-
# Returns an array of characters in
|
1214
|
-
# `str.each_char.to_a`.
|
1225
|
+
# Returns an array of the characters in `self`:
|
1215
1226
|
#
|
1216
|
-
#
|
1217
|
-
#
|
1227
|
+
# 'hello'.chars # => ["h", "e", "l", "l", "o"]
|
1228
|
+
# 'тест'.chars # => ["т", "е", "с", "т"]
|
1229
|
+
# 'こんにちは'.chars # => ["こ", "ん", "に", "ち", "は"]
|
1218
1230
|
#
|
1219
1231
|
def chars: () -> Array[String]
|
1220
1232
|
| () { (String char) -> void } -> String
|
1221
1233
|
|
1222
1234
|
# <!--
|
1223
1235
|
# rdoc-file=string.c
|
1224
|
-
# -
|
1236
|
+
# - chomp(line_sep = $/) -> new_string
|
1225
1237
|
# -->
|
1226
|
-
# Returns a new
|
1227
|
-
#
|
1228
|
-
#
|
1229
|
-
#
|
1230
|
-
#
|
1238
|
+
# Returns a new string copied from `self`, with trailing characters possibly
|
1239
|
+
# removed:
|
1240
|
+
#
|
1241
|
+
# When `line_sep` is `"\n"`, removes the last one or two characters if they are
|
1242
|
+
# `"\r"`, `"\n"`, or `"\r\n"` (but not `"\n\r"`):
|
1243
|
+
#
|
1244
|
+
# $/ # => "\n"
|
1245
|
+
# "abc\r".chomp # => "abc"
|
1246
|
+
# "abc\n".chomp # => "abc"
|
1247
|
+
# "abc\r\n".chomp # => "abc"
|
1248
|
+
# "abc\n\r".chomp # => "abc\n"
|
1249
|
+
# "тест\r\n".chomp # => "тест"
|
1250
|
+
# "こんにちは\r\n".chomp # => "こんにちは"
|
1231
1251
|
#
|
1232
|
-
#
|
1233
|
-
#
|
1234
|
-
#
|
1235
|
-
# "
|
1236
|
-
# "
|
1237
|
-
# "
|
1238
|
-
# "
|
1239
|
-
# "
|
1240
|
-
#
|
1252
|
+
# When `line_sep` is `''` (an empty string), removes multiple trailing
|
1253
|
+
# occurrences of `"\n"` or `"\r\n"` (but not `"\r"` or `"\n\r"`):
|
1254
|
+
#
|
1255
|
+
# "abc\n\n\n".chomp('') # => "abc"
|
1256
|
+
# "abc\r\n\r\n\r\n".chomp('') # => "abc"
|
1257
|
+
# "abc\n\n\r\n\r\n\n\n".chomp('') # => "abc"
|
1258
|
+
# "abc\n\r\n\r\n\r".chomp('') # => "abc\n\r\n\r\n\r"
|
1259
|
+
# "abc\r\r\r".chomp('') # => "abc\r\r\r"
|
1260
|
+
#
|
1261
|
+
# When `line_sep` is neither `"\n"` nor `''`, removes a single trailing line
|
1262
|
+
# separator if there is one:
|
1263
|
+
#
|
1264
|
+
# 'abcd'.chomp('d') # => "abc"
|
1265
|
+
# 'abcdd'.chomp('d') # => "abcd"
|
1241
1266
|
#
|
1242
1267
|
def chomp: (?string separator) -> String
|
1243
1268
|
|
1244
1269
|
# <!--
|
1245
1270
|
# rdoc-file=string.c
|
1246
|
-
# -
|
1271
|
+
# - chomp!(line_sep = $/) -> self or nil
|
1247
1272
|
# -->
|
1248
|
-
#
|
1249
|
-
# `
|
1273
|
+
# Like String#chomp, but modifies `self` in place; returns `nil` if no
|
1274
|
+
# modification made, `self` otherwise.
|
1250
1275
|
#
|
1251
1276
|
def chomp!: (?string separator) -> String?
|
1252
1277
|
|
1253
1278
|
# <!--
|
1254
1279
|
# rdoc-file=string.c
|
1255
|
-
# -
|
1280
|
+
# - chop -> new_string
|
1256
1281
|
# -->
|
1257
|
-
# Returns a new
|
1258
|
-
#
|
1259
|
-
#
|
1260
|
-
#
|
1282
|
+
# Returns a new string copied from `self`, with trailing characters possibly
|
1283
|
+
# removed.
|
1284
|
+
#
|
1285
|
+
# Removes `"\r\n"` if those are the last two characters.
|
1286
|
+
#
|
1287
|
+
# "abc\r\n".chop # => "abc"
|
1288
|
+
# "тест\r\n".chop # => "тест"
|
1289
|
+
# "こんにちは\r\n".chop # => "こんにちは"
|
1290
|
+
#
|
1291
|
+
# Otherwise removes the last character if it exists.
|
1261
1292
|
#
|
1262
|
-
#
|
1263
|
-
#
|
1264
|
-
#
|
1265
|
-
#
|
1266
|
-
#
|
1293
|
+
# 'abcd'.chop # => "abc"
|
1294
|
+
# 'тест'.chop # => "тес"
|
1295
|
+
# 'こんにちは'.chop # => "こんにち"
|
1296
|
+
# ''.chop # => ""
|
1297
|
+
#
|
1298
|
+
# If you only need to remove the newline separator at the end of the string,
|
1299
|
+
# String#chomp is a better alternative.
|
1267
1300
|
#
|
1268
1301
|
def chop: () -> String
|
1269
1302
|
|
1270
1303
|
# <!--
|
1271
1304
|
# rdoc-file=string.c
|
1272
|
-
# -
|
1305
|
+
# - chop! -> self or nil
|
1273
1306
|
# -->
|
1274
|
-
#
|
1275
|
-
# empty
|
1307
|
+
# Like String#chop, but modifies `self` in place; returns `nil` if `self` is
|
1308
|
+
# empty, `self` otherwise.
|
1309
|
+
#
|
1310
|
+
# Related: String#chomp!.
|
1276
1311
|
#
|
1277
1312
|
def chop!: () -> String?
|
1278
1313
|
|
@@ -1300,13 +1335,14 @@ class String
|
|
1300
1335
|
|
1301
1336
|
# <!--
|
1302
1337
|
# rdoc-file=string.c
|
1303
|
-
# -
|
1338
|
+
# - codepoints -> array_of_integers
|
1304
1339
|
# -->
|
1305
|
-
# Returns an array of the
|
1306
|
-
#
|
1340
|
+
# Returns an array of the codepoints in `self`; each codepoint is the integer
|
1341
|
+
# value for a character:
|
1307
1342
|
#
|
1308
|
-
#
|
1309
|
-
#
|
1343
|
+
# 'hello'.codepoints # => [104, 101, 108, 108, 111]
|
1344
|
+
# 'тест'.codepoints # => [1090, 1077, 1089, 1090]
|
1345
|
+
# 'こんにちは'.codepoints # => [12371, 12435, 12395, 12385, 12399]
|
1310
1346
|
#
|
1311
1347
|
def codepoints: () -> ::Array[Integer]
|
1312
1348
|
| () { (Integer codepoint) -> void } -> String
|
@@ -1333,14 +1369,11 @@ class String
|
|
1333
1369
|
|
1334
1370
|
# <!--
|
1335
1371
|
# rdoc-file=string.c
|
1336
|
-
# -
|
1372
|
+
# - count(*selectors) -> integer
|
1337
1373
|
# -->
|
1338
|
-
#
|
1339
|
-
#
|
1340
|
-
#
|
1341
|
-
# means all characters between c1 and c2. The backslash character `\` can be
|
1342
|
-
# used to escape `^` or `-` and is otherwise ignored unless it appears at the
|
1343
|
-
# end of a sequence or the end of a `other_str`.
|
1374
|
+
# Returns the total number of characters in `self` that are specified by the
|
1375
|
+
# given `selectors` (see [Multiple Character
|
1376
|
+
# Selectors](rdoc-ref:character_selectors.rdoc@Multiple+Character+Selectors)):
|
1344
1377
|
#
|
1345
1378
|
# a = "hello world"
|
1346
1379
|
# a.count "lo" #=> 5
|
@@ -1360,7 +1393,7 @@ class String
|
|
1360
1393
|
|
1361
1394
|
# <!--
|
1362
1395
|
# rdoc-file=string.c
|
1363
|
-
# -
|
1396
|
+
# - crypt(salt_str) -> new_string
|
1364
1397
|
# -->
|
1365
1398
|
# Returns the string generated by calling `crypt(3)` standard library function
|
1366
1399
|
# with `str` and `salt_str`, in this order, as its arguments. Please do not use
|
@@ -1414,13 +1447,23 @@ class String
|
|
1414
1447
|
#
|
1415
1448
|
def crypt: (string salt_str) -> String
|
1416
1449
|
|
1450
|
+
# <!-- rdoc-file=string.c -->
|
1451
|
+
# Returns a frozen, possibly pre-existing copy of the string.
|
1452
|
+
#
|
1453
|
+
# The returned String will be deduplicated as long as it does not have any
|
1454
|
+
# instance variables set on it and is not a String subclass.
|
1455
|
+
#
|
1456
|
+
# String#dedup is an alias for String#-@.
|
1457
|
+
#
|
1458
|
+
alias dedup -@
|
1459
|
+
|
1417
1460
|
# <!--
|
1418
1461
|
# rdoc-file=string.c
|
1419
|
-
# -
|
1462
|
+
# - delete(*selectors) -> new_string
|
1420
1463
|
# -->
|
1421
|
-
# Returns a copy of
|
1422
|
-
#
|
1423
|
-
#
|
1464
|
+
# Returns a copy of `self` with characters specified by `selectors` removed (see
|
1465
|
+
# [Multiple Character
|
1466
|
+
# Selectors](rdoc-ref:character_selectors.rdoc@Multiple+Character+Selectors)):
|
1424
1467
|
#
|
1425
1468
|
# "hello".delete "l","lo" #=> "heo"
|
1426
1469
|
# "hello".delete "lo" #=> "he"
|
@@ -1431,54 +1474,58 @@ class String
|
|
1431
1474
|
|
1432
1475
|
# <!--
|
1433
1476
|
# rdoc-file=string.c
|
1434
|
-
# -
|
1477
|
+
# - delete!(*selectors) -> self or nil
|
1435
1478
|
# -->
|
1436
|
-
#
|
1437
|
-
#
|
1479
|
+
# Like String#delete, but modifies `self` in place. Returns `self` if any
|
1480
|
+
# changes were made, `nil` otherwise.
|
1438
1481
|
#
|
1439
1482
|
def delete!: (string other_str, *string other_strs) -> String?
|
1440
1483
|
|
1441
1484
|
# <!--
|
1442
1485
|
# rdoc-file=string.c
|
1443
|
-
# -
|
1486
|
+
# - delete_prefix(prefix) -> new_string
|
1444
1487
|
# -->
|
1445
|
-
# Returns a copy of
|
1488
|
+
# Returns a copy of `self` with leading substring `prefix` removed:
|
1489
|
+
#
|
1490
|
+
# 'hello'.delete_prefix('hel') # => "lo"
|
1491
|
+
# 'hello'.delete_prefix('llo') # => "hello"
|
1492
|
+
# 'тест'.delete_prefix('те') # => "ст"
|
1493
|
+
# 'こんにちは'.delete_prefix('こん') # => "にちは"
|
1446
1494
|
#
|
1447
|
-
#
|
1448
|
-
# "hello".delete_prefix("llo") #=> "hello"
|
1495
|
+
# Related: String#delete_prefix!, String#delete_suffix.
|
1449
1496
|
#
|
1450
1497
|
def delete_prefix: (string prefix) -> String
|
1451
1498
|
|
1452
1499
|
# <!--
|
1453
1500
|
# rdoc-file=string.c
|
1454
|
-
# -
|
1501
|
+
# - delete_prefix!(prefix) -> self or nil
|
1455
1502
|
# -->
|
1456
|
-
#
|
1457
|
-
#
|
1458
|
-
# "hello".delete_prefix!("hel") #=> "lo"
|
1459
|
-
# "hello".delete_prefix!("llo") #=> nil
|
1503
|
+
# Like String#delete_prefix, except that `self` is modified in place. Returns
|
1504
|
+
# `self` if the prefix is removed, `nil` otherwise.
|
1460
1505
|
#
|
1461
1506
|
def delete_prefix!: (string prefix) -> String?
|
1462
1507
|
|
1463
1508
|
# <!--
|
1464
1509
|
# rdoc-file=string.c
|
1465
|
-
# -
|
1510
|
+
# - delete_suffix(suffix) -> new_string
|
1466
1511
|
# -->
|
1467
|
-
# Returns a copy of
|
1512
|
+
# Returns a copy of `self` with trailing substring `suffix` removed:
|
1513
|
+
#
|
1514
|
+
# 'hello'.delete_suffix('llo') # => "he"
|
1515
|
+
# 'hello'.delete_suffix('hel') # => "hello"
|
1516
|
+
# 'тест'.delete_suffix('ст') # => "те"
|
1517
|
+
# 'こんにちは'.delete_suffix('ちは') # => "こんに"
|
1468
1518
|
#
|
1469
|
-
#
|
1470
|
-
# "hello".delete_suffix("hel") #=> "hello"
|
1519
|
+
# Related: String#delete_suffix!, String#delete_prefix.
|
1471
1520
|
#
|
1472
1521
|
def delete_suffix: (string suffix) -> String
|
1473
1522
|
|
1474
1523
|
# <!--
|
1475
1524
|
# rdoc-file=string.c
|
1476
|
-
# -
|
1525
|
+
# - delete_suffix!(suffix) -> self or nil
|
1477
1526
|
# -->
|
1478
|
-
#
|
1479
|
-
#
|
1480
|
-
# "hello".delete_suffix!("llo") #=> "he"
|
1481
|
-
# "hello".delete_suffix!("hel") #=> nil
|
1527
|
+
# Like String#delete_suffix, except that `self` is modified in place. Returns
|
1528
|
+
# `self` if the suffix is removed, `nil` otherwise.
|
1482
1529
|
#
|
1483
1530
|
def delete_suffix!: (string suffix) -> String?
|
1484
1531
|
|
@@ -1492,7 +1539,7 @@ class String
|
|
1492
1539
|
# s.downcase # => "hello world!"
|
1493
1540
|
#
|
1494
1541
|
# The casing may be affected by the given `options`; see [Case
|
1495
|
-
# Mapping](
|
1542
|
+
# Mapping](rdoc-ref:case_mapping.rdoc).
|
1496
1543
|
#
|
1497
1544
|
# Related: String#downcase!, String#upcase, String#upcase!.
|
1498
1545
|
#
|
@@ -1514,7 +1561,7 @@ class String
|
|
1514
1561
|
# s.downcase! # => nil
|
1515
1562
|
#
|
1516
1563
|
# The casing may be affected by the given `options`; see [Case
|
1517
|
-
# Mapping](
|
1564
|
+
# Mapping](rdoc-ref:case_mapping.rdoc).
|
1518
1565
|
#
|
1519
1566
|
# Related: String#downcase, String#upcase, String#upcase!.
|
1520
1567
|
#
|
@@ -1540,117 +1587,165 @@ class String
|
|
1540
1587
|
|
1541
1588
|
# <!--
|
1542
1589
|
# rdoc-file=string.c
|
1543
|
-
# -
|
1544
|
-
# -
|
1590
|
+
# - each_byte {|byte| ... } -> self
|
1591
|
+
# - each_byte -> enumerator
|
1545
1592
|
# -->
|
1546
|
-
#
|
1547
|
-
# block is given.
|
1593
|
+
# Calls the given block with each successive byte from `self`; returns `self`:
|
1548
1594
|
#
|
1549
|
-
#
|
1595
|
+
# 'hello'.each_byte {|byte| print byte, ' ' }
|
1596
|
+
# print "\n"
|
1597
|
+
# 'тест'.each_byte {|byte| print byte, ' ' }
|
1598
|
+
# print "\n"
|
1599
|
+
# 'こんにちは'.each_byte {|byte| print byte, ' ' }
|
1600
|
+
# print "\n"
|
1550
1601
|
#
|
1551
|
-
#
|
1602
|
+
# Output:
|
1552
1603
|
#
|
1553
1604
|
# 104 101 108 108 111
|
1605
|
+
# 209 130 208 181 209 129 209 130
|
1606
|
+
# 227 129 147 227 130 147 227 129 171 227 129 161 227 129 175
|
1607
|
+
#
|
1608
|
+
# Returns an enumerator if no block is given.
|
1554
1609
|
#
|
1555
1610
|
def each_byte: () { (Integer byte) -> void } -> self
|
1556
1611
|
| () -> ::Enumerator[Integer, self]
|
1557
1612
|
|
1558
1613
|
# <!--
|
1559
1614
|
# rdoc-file=string.c
|
1560
|
-
# -
|
1561
|
-
# -
|
1615
|
+
# - each_char {|c| ... } -> self
|
1616
|
+
# - each_char -> enumerator
|
1562
1617
|
# -->
|
1563
|
-
#
|
1564
|
-
#
|
1618
|
+
# Calls the given block with each successive character from `self`; returns
|
1619
|
+
# `self`:
|
1565
1620
|
#
|
1566
|
-
#
|
1621
|
+
# 'hello'.each_char {|char| print char, ' ' }
|
1622
|
+
# print "\n"
|
1623
|
+
# 'тест'.each_char {|char| print char, ' ' }
|
1624
|
+
# print "\n"
|
1625
|
+
# 'こんにちは'.each_char {|char| print char, ' ' }
|
1626
|
+
# print "\n"
|
1567
1627
|
#
|
1568
|
-
#
|
1628
|
+
# Output:
|
1569
1629
|
#
|
1570
1630
|
# h e l l o
|
1631
|
+
# т е с т
|
1632
|
+
# こ ん に ち は
|
1633
|
+
#
|
1634
|
+
# Returns an enumerator if no block is given.
|
1571
1635
|
#
|
1572
1636
|
def each_char: () { (String char) -> void } -> self
|
1573
1637
|
| () -> ::Enumerator[String, self]
|
1574
1638
|
|
1575
1639
|
# <!--
|
1576
1640
|
# rdoc-file=string.c
|
1577
|
-
# -
|
1578
|
-
# -
|
1641
|
+
# - each_codepoint {|integer| ... } -> self
|
1642
|
+
# - each_codepoint -> enumerator
|
1579
1643
|
# -->
|
1580
|
-
#
|
1581
|
-
#
|
1582
|
-
# other than UTF-8/UTF-16(BE|LE)/UTF-32(BE|LE), values are directly derived from
|
1583
|
-
# the binary representation of each character.
|
1644
|
+
# Calls the given block with each successive codepoint from `self`; each
|
1645
|
+
# codepoint is the integer value for a character; returns `self`:
|
1584
1646
|
#
|
1585
|
-
#
|
1647
|
+
# 'hello'.each_codepoint {|codepoint| print codepoint, ' ' }
|
1648
|
+
# print "\n"
|
1649
|
+
# 'тест'.each_codepoint {|codepoint| print codepoint, ' ' }
|
1650
|
+
# print "\n"
|
1651
|
+
# 'こんにちは'.each_codepoint {|codepoint| print codepoint, ' ' }
|
1652
|
+
# print "\n"
|
1586
1653
|
#
|
1587
|
-
#
|
1654
|
+
# Output:
|
1588
1655
|
#
|
1589
|
-
#
|
1656
|
+
# 104 101 108 108 111
|
1657
|
+
# 1090 1077 1089 1090
|
1658
|
+
# 12371 12435 12395 12385 12399
|
1590
1659
|
#
|
1591
|
-
#
|
1660
|
+
# Returns an enumerator if no block is given.
|
1592
1661
|
#
|
1593
1662
|
def each_codepoint: () { (Integer codepoint) -> void } -> self
|
1594
1663
|
| () -> ::Enumerator[Integer, self]
|
1595
1664
|
|
1596
1665
|
# <!--
|
1597
1666
|
# rdoc-file=string.c
|
1598
|
-
# -
|
1599
|
-
# -
|
1667
|
+
# - each_grapheme_cluster {|gc| ... } -> self
|
1668
|
+
# - each_grapheme_cluster -> enumerator
|
1600
1669
|
# -->
|
1601
|
-
#
|
1602
|
-
#
|
1603
|
-
#
|
1604
|
-
#
|
1670
|
+
# Calls the given block with each successive grapheme cluster from `self` (see
|
1671
|
+
# [Unicode Grapheme Cluster
|
1672
|
+
# Boundaries](https://www.unicode.org/reports/tr29/#Grapheme_Cluster_Boundaries)
|
1673
|
+
# ); returns `self`:
|
1674
|
+
#
|
1675
|
+
# s = "\u0061\u0308-pqr-\u0062\u0308-xyz-\u0063\u0308" # => "ä-pqr-b̈-xyz-c̈"
|
1676
|
+
# s.each_grapheme_cluster {|gc| print gc, ' ' }
|
1677
|
+
#
|
1678
|
+
# Output:
|
1679
|
+
#
|
1680
|
+
# ä - p q r - b̈ - x y z - c̈
|
1605
1681
|
#
|
1606
|
-
#
|
1607
|
-
# "a\u0300".each_grapheme_cluster.to_a.size #=> 1
|
1682
|
+
# Returns an enumerator if no block is given.
|
1608
1683
|
#
|
1609
1684
|
def each_grapheme_cluster: () { (String grapheme) -> void } -> self
|
1610
1685
|
| () -> ::Enumerator[String, self]
|
1611
1686
|
|
1612
1687
|
# <!--
|
1613
1688
|
# rdoc-file=string.c
|
1614
|
-
# -
|
1615
|
-
# -
|
1689
|
+
# - each_line(line_sep = $/, chomp: false) {|substring| ... } -> self
|
1690
|
+
# - each_line(line_sep = $/, chomp: false) -> enumerator
|
1616
1691
|
# -->
|
1617
|
-
#
|
1618
|
-
#
|
1619
|
-
#
|
1620
|
-
#
|
1692
|
+
# With a block given, forms the substrings ("lines") that are the result of
|
1693
|
+
# splitting `self` at each occurrence of the given line separator `line_sep`;
|
1694
|
+
# passes each line to the block; returns `self`:
|
1695
|
+
#
|
1696
|
+
# s = <<~EOT
|
1697
|
+
# This is the first line.
|
1698
|
+
# This is line two.
|
1699
|
+
#
|
1700
|
+
# This is line four.
|
1701
|
+
# This is line five.
|
1702
|
+
# EOT
|
1703
|
+
#
|
1704
|
+
# s.each_line {|line| p line }
|
1705
|
+
#
|
1706
|
+
# Output:
|
1707
|
+
#
|
1708
|
+
# "This is the first line.\n"
|
1709
|
+
# "This is line two.\n"
|
1710
|
+
# "\n"
|
1711
|
+
# "This is line four.\n"
|
1712
|
+
# "This is line five.\n"
|
1621
1713
|
#
|
1622
|
-
#
|
1714
|
+
# With a different `line_sep`:
|
1623
1715
|
#
|
1624
|
-
#
|
1716
|
+
# s.each_line(' is ') {|line| p line }
|
1625
1717
|
#
|
1626
|
-
#
|
1627
|
-
#
|
1628
|
-
#
|
1629
|
-
#
|
1718
|
+
# Output:
|
1719
|
+
#
|
1720
|
+
# "This is "
|
1721
|
+
# "the first line.\nThis is "
|
1722
|
+
# "line two.\n\nThis is "
|
1723
|
+
# "line four.\nThis is "
|
1724
|
+
# "line five.\n"
|
1630
1725
|
#
|
1631
|
-
#
|
1632
|
-
# # prints:
|
1633
|
-
# # "hel"
|
1634
|
-
# # "l"
|
1635
|
-
# # "o\nworl"
|
1636
|
-
# # "d"
|
1726
|
+
# With `chomp` as `true`, removes the trailing `line_sep` from each line:
|
1637
1727
|
#
|
1638
|
-
#
|
1639
|
-
#
|
1640
|
-
#
|
1641
|
-
# # "world"
|
1728
|
+
# s.each_line(chomp: true) {|line| p line }
|
1729
|
+
#
|
1730
|
+
# Output:
|
1642
1731
|
#
|
1643
|
-
# "
|
1644
|
-
#
|
1645
|
-
#
|
1646
|
-
#
|
1732
|
+
# "This is the first line."
|
1733
|
+
# "This is line two."
|
1734
|
+
# ""
|
1735
|
+
# "This is line four."
|
1736
|
+
# "This is line five."
|
1647
1737
|
#
|
1648
|
-
#
|
1649
|
-
#
|
1650
|
-
#
|
1651
|
-
#
|
1652
|
-
#
|
1653
|
-
#
|
1738
|
+
# With an empty string as `line_sep`, forms and passes "paragraphs" by splitting
|
1739
|
+
# at each occurrence of two or more newlines:
|
1740
|
+
#
|
1741
|
+
# s.each_line('') {|line| p line }
|
1742
|
+
#
|
1743
|
+
# Output:
|
1744
|
+
#
|
1745
|
+
# "This is the first line.\nThis is line two.\n\n"
|
1746
|
+
# "This is line four.\nThis is line five.\n"
|
1747
|
+
#
|
1748
|
+
# With no block given, returns an enumerator.
|
1654
1749
|
#
|
1655
1750
|
def each_line: (?string separator, ?chomp: boolish) { (String line) -> void } -> self
|
1656
1751
|
| (?string separator, ?chomp: boolish) -> Enumerator[String, self]
|
@@ -1668,67 +1763,60 @@ class String
|
|
1668
1763
|
def empty?: () -> bool
|
1669
1764
|
|
1670
1765
|
# <!--
|
1671
|
-
# rdoc-file=transcode.
|
1672
|
-
# -
|
1673
|
-
# -
|
1674
|
-
#
|
1675
|
-
#
|
1676
|
-
#
|
1677
|
-
#
|
1678
|
-
#
|
1679
|
-
#
|
1680
|
-
#
|
1681
|
-
#
|
1682
|
-
#
|
1683
|
-
#
|
1684
|
-
#
|
1685
|
-
#
|
1686
|
-
#
|
1687
|
-
#
|
1688
|
-
#
|
1689
|
-
#
|
1690
|
-
# :
|
1691
|
-
#
|
1692
|
-
#
|
1693
|
-
#
|
1694
|
-
#
|
1695
|
-
#
|
1696
|
-
#
|
1697
|
-
#
|
1698
|
-
#
|
1699
|
-
#
|
1700
|
-
#
|
1701
|
-
#
|
1702
|
-
#
|
1703
|
-
#
|
1704
|
-
#
|
1705
|
-
#
|
1706
|
-
#
|
1707
|
-
#
|
1708
|
-
#
|
1709
|
-
#
|
1710
|
-
#
|
1711
|
-
#
|
1712
|
-
#
|
1713
|
-
#
|
1714
|
-
#
|
1715
|
-
# :
|
1716
|
-
# : Replaces LF ("n") with CRLF ("r\n") if value is true.
|
1717
|
-
# :universal_newline
|
1718
|
-
# : Replaces CRLF ("r\n") and CR ("r") with LF ("n") if value is true.
|
1766
|
+
# rdoc-file=transcode.rdoc
|
1767
|
+
# - encode(dst_encoding = Encoding.default_internal, **enc_opts) -> string
|
1768
|
+
# - encode(dst_encoding, src_encoding, **enc_opts) -> string
|
1769
|
+
# -->
|
1770
|
+
# Returns a copy of `self` transcoded as determined by `dst_encoding`. By
|
1771
|
+
# default, raises an exception if `self` contains an invalid byte or a character
|
1772
|
+
# not defined in `dst_encoding`; that behavior may be modified by encoding
|
1773
|
+
# options; see below.
|
1774
|
+
#
|
1775
|
+
# With no arguments:
|
1776
|
+
#
|
1777
|
+
# * Uses the same encoding if `Encoding.default_internal` is `nil` (the
|
1778
|
+
# default):
|
1779
|
+
#
|
1780
|
+
# Encoding.default_internal # => nil
|
1781
|
+
# s = "Ruby\x99".force_encoding('Windows-1252')
|
1782
|
+
# s.encoding # => #<Encoding:Windows-1252>
|
1783
|
+
# s.bytes # => [82, 117, 98, 121, 153]
|
1784
|
+
# t = s.encode # => "Ruby\x99"
|
1785
|
+
# t.encoding # => #<Encoding:Windows-1252>
|
1786
|
+
# t.bytes # => [82, 117, 98, 121, 226, 132, 162]
|
1787
|
+
#
|
1788
|
+
# * Otherwise, uses the encoding `Encoding.default_internal`:
|
1789
|
+
#
|
1790
|
+
# Encoding.default_internal = 'UTF-8'
|
1791
|
+
# t = s.encode # => "Ruby™"
|
1792
|
+
# t.encoding # => #<Encoding:UTF-8>
|
1793
|
+
#
|
1794
|
+
#
|
1795
|
+
# With only argument `dst_encoding` given, uses that encoding:
|
1796
|
+
#
|
1797
|
+
# s = "Ruby\x99".force_encoding('Windows-1252')
|
1798
|
+
# s.encoding # => #<Encoding:Windows-1252>
|
1799
|
+
# t = s.encode('UTF-8') # => "Ruby™"
|
1800
|
+
# t.encoding # => #<Encoding:UTF-8>
|
1801
|
+
#
|
1802
|
+
# With arguments `dst_encoding` and `src_encoding` given, interprets `self`
|
1803
|
+
# using `src_encoding`, encodes the new string using `dst_encoding`:
|
1804
|
+
#
|
1805
|
+
# s = "Ruby\x99"
|
1806
|
+
# t = s.encode('UTF-8', 'Windows-1252') # => "Ruby™"
|
1807
|
+
# t.encoding # => #<Encoding:UTF-8>
|
1808
|
+
#
|
1809
|
+
# Optional keyword arguments `enc_opts` specify encoding options; see [Encoding
|
1810
|
+
# Options](rdoc-ref:encodings.rdoc@Encoding+Options).
|
1719
1811
|
#
|
1720
1812
|
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
|
1721
1813
|
|
1722
1814
|
# <!--
|
1723
1815
|
# rdoc-file=transcode.c
|
1724
|
-
# -
|
1725
|
-
# -
|
1816
|
+
# - encode!(dst_encoding = Encoding.default_internal, **enc_opts) -> self
|
1817
|
+
# - encode!(dst_encoding, src_encoding, **enc_opts) -> self
|
1726
1818
|
# -->
|
1727
|
-
#
|
1728
|
-
# `encoding`. The second form transcodes the contents of *str* from src_encoding
|
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.
|
1819
|
+
# Like #encode, but applies encoding changes to `self`; returns `self`.
|
1732
1820
|
#
|
1733
1821
|
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
|
1734
1822
|
|
@@ -1742,15 +1830,19 @@ class String
|
|
1742
1830
|
|
1743
1831
|
# <!--
|
1744
1832
|
# rdoc-file=string.c
|
1745
|
-
# -
|
1833
|
+
# - end_with?(*strings) -> true or false
|
1746
1834
|
# -->
|
1747
|
-
# Returns
|
1835
|
+
# Returns whether `self` ends with any of the given `strings`.
|
1836
|
+
#
|
1837
|
+
# Returns `true` if any given string matches the end, `false` otherwise:
|
1748
1838
|
#
|
1749
|
-
#
|
1839
|
+
# 'hello'.end_with?('ello') #=> true
|
1840
|
+
# 'hello'.end_with?('heaven', 'ello') #=> true
|
1841
|
+
# 'hello'.end_with?('heaven', 'paradise') #=> false
|
1842
|
+
# 'тест'.end_with?('т') # => true
|
1843
|
+
# 'こんにちは'.end_with?('は') # => true
|
1750
1844
|
#
|
1751
|
-
#
|
1752
|
-
# "hello".end_with?("heaven", "ello") #=> true
|
1753
|
-
# "hello".end_with?("heaven", "paradise") #=> false
|
1845
|
+
# Related: String#start_with?.
|
1754
1846
|
#
|
1755
1847
|
def end_with?: (*string suffixes) -> bool
|
1756
1848
|
|
@@ -1774,9 +1866,27 @@ class String
|
|
1774
1866
|
|
1775
1867
|
# <!--
|
1776
1868
|
# rdoc-file=string.c
|
1777
|
-
# -
|
1869
|
+
# - force_encoding(encoding) -> self
|
1778
1870
|
# -->
|
1779
|
-
# Changes the encoding to `encoding
|
1871
|
+
# Changes the encoding of `self` to `encoding`, which may be a string encoding
|
1872
|
+
# name or an Encoding object; returns self:
|
1873
|
+
#
|
1874
|
+
# s = 'łał'
|
1875
|
+
# s.bytes # => [197, 130, 97, 197, 130]
|
1876
|
+
# s.encoding # => #<Encoding:UTF-8>
|
1877
|
+
# s.force_encoding('ascii') # => "\xC5\x82a\xC5\x82"
|
1878
|
+
# s.encoding # => #<Encoding:US-ASCII>
|
1879
|
+
#
|
1880
|
+
# Does not change the underlying bytes:
|
1881
|
+
#
|
1882
|
+
# s.bytes # => [197, 130, 97, 197, 130]
|
1883
|
+
#
|
1884
|
+
# Makes the change even if the given `encoding` is invalid for `self` (as is the
|
1885
|
+
# change above):
|
1886
|
+
#
|
1887
|
+
# s.valid_encoding? # => false
|
1888
|
+
# s.force_encoding(Encoding::UTF_8) # => "łał"
|
1889
|
+
# s.valid_encoding? # => true
|
1780
1890
|
#
|
1781
1891
|
def force_encoding: (string | Encoding encoding) -> self
|
1782
1892
|
|
@@ -1789,13 +1899,15 @@ class String
|
|
1789
1899
|
|
1790
1900
|
# <!--
|
1791
1901
|
# rdoc-file=string.c
|
1792
|
-
# - getbyte(index) -> integer
|
1902
|
+
# - getbyte(index) -> integer or nil
|
1793
1903
|
# -->
|
1794
|
-
# Returns the byte at zero-based `index` as an integer
|
1904
|
+
# Returns the byte at zero-based `index` as an integer, or `nil` if `index` is
|
1905
|
+
# out of range:
|
1795
1906
|
#
|
1796
|
-
# s = 'abcde'
|
1797
|
-
# s.getbyte(0)
|
1798
|
-
# s.getbyte(1) # =>
|
1907
|
+
# s = 'abcde' # => "abcde"
|
1908
|
+
# s.getbyte(0) # => 97
|
1909
|
+
# s.getbyte(-1) # => 101
|
1910
|
+
# s.getbyte(5) # => nil
|
1799
1911
|
#
|
1800
1912
|
# Related: String#setbyte.
|
1801
1913
|
#
|
@@ -1803,13 +1915,16 @@ class String
|
|
1803
1915
|
|
1804
1916
|
# <!--
|
1805
1917
|
# rdoc-file=string.c
|
1806
|
-
# -
|
1918
|
+
# - grapheme_clusters -> array_of_grapheme_clusters
|
1807
1919
|
# -->
|
1808
|
-
# Returns an array of grapheme clusters in
|
1809
|
-
#
|
1920
|
+
# Returns an array of the grapheme clusters in `self` (see [Unicode Grapheme
|
1921
|
+
# Cluster
|
1922
|
+
# Boundaries](https://www.unicode.org/reports/tr29/#Grapheme_Cluster_Boundaries)
|
1923
|
+
# ):
|
1810
1924
|
#
|
1811
|
-
#
|
1812
|
-
#
|
1925
|
+
# s = "\u0061\u0308-pqr-\u0062\u0308-xyz-\u0063\u0308" # => "ä-pqr-b̈-xyz-c̈"
|
1926
|
+
# s.grapheme_clusters
|
1927
|
+
# # => ["ä", "-", "p", "q", "r", "-", "b̈", "-", "x", "y", "z", "-", "c̈"]
|
1813
1928
|
#
|
1814
1929
|
def grapheme_clusters: () -> ::Array[::String]
|
1815
1930
|
|
@@ -1821,7 +1936,7 @@ class String
|
|
1821
1936
|
# -->
|
1822
1937
|
# Returns a copy of `self` with all occurrences of the given `pattern` replaced.
|
1823
1938
|
#
|
1824
|
-
# See [Substitution Methods](
|
1939
|
+
# See [Substitution Methods](rdoc-ref:String@Substitution+Methods).
|
1825
1940
|
#
|
1826
1941
|
# Returns an Enumerator if no `replacement` and no block given.
|
1827
1942
|
#
|
@@ -1841,7 +1956,7 @@ class String
|
|
1841
1956
|
# Performs the specified substring replacement(s) on `self`; returns `self` if
|
1842
1957
|
# any replacement occurred, `nil` otherwise.
|
1843
1958
|
#
|
1844
|
-
# See [Substitution Methods](
|
1959
|
+
# See [Substitution Methods](rdoc-ref:String@Substitution+Methods).
|
1845
1960
|
#
|
1846
1961
|
# Returns an Enumerator if no `replacement` and no block given.
|
1847
1962
|
#
|
@@ -1865,16 +1980,18 @@ class String
|
|
1865
1980
|
|
1866
1981
|
# <!--
|
1867
1982
|
# rdoc-file=string.c
|
1868
|
-
# -
|
1983
|
+
# - hex -> integer
|
1869
1984
|
# -->
|
1870
|
-
#
|
1871
|
-
# an optional sign and an optional `0x`) and returns the corresponding
|
1872
|
-
#
|
1985
|
+
# Interprets the leading substring of `self` as a string of hexadecimal digits
|
1986
|
+
# (with an optional sign and an optional `0x`) and returns the corresponding
|
1987
|
+
# number; returns zero if there is no such leading substring:
|
1873
1988
|
#
|
1874
|
-
#
|
1875
|
-
#
|
1876
|
-
#
|
1877
|
-
#
|
1989
|
+
# '0x0a'.hex # => 10
|
1990
|
+
# '-1234'.hex # => -4660
|
1991
|
+
# '0'.hex # => 0
|
1992
|
+
# 'non-numeric'.hex # => 0
|
1993
|
+
#
|
1994
|
+
# Related: String#oct.
|
1878
1995
|
#
|
1879
1996
|
def hex: () -> Integer
|
1880
1997
|
|
@@ -1896,35 +2013,42 @@ class String
|
|
1896
2013
|
# - index(substring, offset = 0) -> integer or nil
|
1897
2014
|
# - index(regexp, offset = 0) -> integer or nil
|
1898
2015
|
# -->
|
1899
|
-
# Returns the
|
1900
|
-
# `
|
2016
|
+
# Returns the integer index of the first match for the given argument, or `nil`
|
2017
|
+
# if none found; the search of `self` is forward, and begins at position
|
2018
|
+
# `offset` (in characters).
|
1901
2019
|
#
|
1902
|
-
#
|
1903
|
-
#
|
1904
|
-
# 'foo'.index('oo') # => 1
|
1905
|
-
# 'foo'.index('ooo') # => nil
|
2020
|
+
# With string argument `substring`, returns the index of the first matching
|
2021
|
+
# substring in `self`:
|
1906
2022
|
#
|
1907
|
-
#
|
1908
|
-
#
|
2023
|
+
# 'foo'.index('f') # => 0
|
2024
|
+
# 'foo'.index('o') # => 1
|
2025
|
+
# 'foo'.index('oo') # => 1
|
2026
|
+
# 'foo'.index('ooo') # => nil
|
2027
|
+
# 'тест'.index('с') # => 2
|
2028
|
+
# 'こんにちは'.index('ち') # => 3
|
1909
2029
|
#
|
1910
|
-
#
|
1911
|
-
# 'foo'.index(/o/) # => 1
|
1912
|
-
# 'foo'.index(/oo/) # => 1
|
1913
|
-
# 'foo'.index(/ooo/) # => nil
|
2030
|
+
# With Regexp argument `regexp`, returns the index of the first match in `self`:
|
1914
2031
|
#
|
1915
|
-
#
|
1916
|
-
#
|
2032
|
+
# 'foo'.index(/o./) # => 1
|
2033
|
+
# 'foo'.index(/.o/) # => 0
|
1917
2034
|
#
|
1918
|
-
#
|
1919
|
-
# 'foo'.index('o', 2) # => 2
|
1920
|
-
# 'foo'.index('o', 3) # => nil
|
2035
|
+
# With positive integer `offset`, begins the search at position `offset`:
|
1921
2036
|
#
|
1922
|
-
#
|
2037
|
+
# 'foo'.index('o', 1) # => 1
|
2038
|
+
# 'foo'.index('o', 2) # => 2
|
2039
|
+
# 'foo'.index('o', 3) # => nil
|
2040
|
+
# 'тест'.index('с', 1) # => 2
|
2041
|
+
# 'こんにちは'.index('ち', 2) # => 3
|
1923
2042
|
#
|
1924
|
-
#
|
1925
|
-
#
|
1926
|
-
#
|
1927
|
-
# 'foo'.index('o', -
|
2043
|
+
# With negative integer `offset`, selects the search position by counting
|
2044
|
+
# backward from the end of `self`:
|
2045
|
+
#
|
2046
|
+
# 'foo'.index('o', -1) # => 2
|
2047
|
+
# 'foo'.index('o', -2) # => 1
|
2048
|
+
# 'foo'.index('o', -3) # => 1
|
2049
|
+
# 'foo'.index('o', -4) # => nil
|
2050
|
+
# 'foo'.index(/o./, -2) # => 1
|
2051
|
+
# 'foo'.index(/.o/, -2) # => 1
|
1928
2052
|
#
|
1929
2053
|
# Related: String#rindex.
|
1930
2054
|
#
|
@@ -1955,7 +2079,6 @@ class String
|
|
1955
2079
|
# special characters escaped:
|
1956
2080
|
#
|
1957
2081
|
# s = "foo\tbar\tbaz\n"
|
1958
|
-
# # => "foo\tbar\tbaz\n"
|
1959
2082
|
# s.inspect
|
1960
2083
|
# # => "\"foo\\tbar\\tbaz\\n\""
|
1961
2084
|
#
|
@@ -1988,74 +2111,78 @@ class String
|
|
1988
2111
|
# -->
|
1989
2112
|
# Returns the count of characters (not bytes) in `self`:
|
1990
2113
|
#
|
1991
|
-
#
|
1992
|
-
#
|
2114
|
+
# 'foo'.length # => 3
|
2115
|
+
# 'тест'.length # => 4
|
2116
|
+
# 'こんにちは'.length # => 5
|
1993
2117
|
#
|
1994
|
-
#
|
2118
|
+
# Contrast with String#bytesize:
|
1995
2119
|
#
|
1996
|
-
#
|
2120
|
+
# 'foo'.bytesize # => 3
|
2121
|
+
# 'тест'.bytesize # => 8
|
2122
|
+
# 'こんにちは'.bytesize # => 15
|
2123
|
+
#
|
2124
|
+
# String#size is an alias for String#length.
|
1997
2125
|
#
|
1998
2126
|
def length: () -> Integer
|
1999
2127
|
|
2000
2128
|
# <!--
|
2001
2129
|
# rdoc-file=string.c
|
2002
|
-
# -
|
2130
|
+
# - lines(Line_sep = $/, chomp: false) -> array_of_strings
|
2003
2131
|
# -->
|
2004
|
-
#
|
2005
|
-
#
|
2006
|
-
# getline_args).to_a`.
|
2007
|
-
#
|
2008
|
-
# If `chomp` is `true`, `separator` will be removed from the end of each line.
|
2009
|
-
#
|
2010
|
-
# "hello\nworld\n".lines #=> ["hello\n", "world\n"]
|
2011
|
-
# "hello world".lines(' ') #=> ["hello ", " ", "world"]
|
2012
|
-
# "hello\nworld\n".lines(chomp: true) #=> ["hello", "world"]
|
2013
|
-
#
|
2014
|
-
# If a block is given, which is a deprecated form, works the same as
|
2015
|
-
# `each_line`.
|
2132
|
+
# Forms substrings ("lines") of `self` according to the given arguments (see
|
2133
|
+
# String#each_line for details); returns the lines in an array.
|
2016
2134
|
#
|
2017
2135
|
def lines: (?string separator, ?chomp: boolish) -> Array[String]
|
2018
2136
|
|
2019
2137
|
# <!--
|
2020
2138
|
# rdoc-file=string.c
|
2021
|
-
# -
|
2139
|
+
# - ljust(size, pad_string = ' ') -> new_string
|
2022
2140
|
# -->
|
2023
|
-
#
|
2024
|
-
#
|
2025
|
-
#
|
2141
|
+
# Returns a left-justified copy of `self`.
|
2142
|
+
#
|
2143
|
+
# If integer argument `size` is greater than the size (in characters) of `self`,
|
2144
|
+
# returns a new string of length `size` that is a copy of `self`, left justified
|
2145
|
+
# and padded on the right with `pad_string`:
|
2146
|
+
#
|
2147
|
+
# 'hello'.ljust(10) # => "hello "
|
2148
|
+
# ' hello'.ljust(10) # => " hello "
|
2149
|
+
# 'hello'.ljust(10, 'ab') # => "helloababa"
|
2150
|
+
# 'тест'.ljust(10) # => "тест "
|
2151
|
+
# 'こんにちは'.ljust(10) # => "こんにちは "
|
2026
2152
|
#
|
2027
|
-
#
|
2028
|
-
#
|
2029
|
-
#
|
2153
|
+
# If `size` is not greater than the size of `self`, returns a copy of `self`:
|
2154
|
+
#
|
2155
|
+
# 'hello'.ljust(5) # => "hello"
|
2156
|
+
# 'hello'.ljust(1) # => "hello"
|
2157
|
+
#
|
2158
|
+
# Related: String#rjust, String#center.
|
2030
2159
|
#
|
2031
2160
|
def ljust: (int integer, ?string padstr) -> String
|
2032
2161
|
|
2033
2162
|
# <!--
|
2034
2163
|
# rdoc-file=string.c
|
2035
|
-
# -
|
2164
|
+
# - lstrip -> new_string
|
2036
2165
|
# -->
|
2037
|
-
# Returns a copy of
|
2038
|
-
# String
|
2166
|
+
# Returns a copy of `self` with leading whitespace removed; see [Whitespace in
|
2167
|
+
# Strings](rdoc-ref:String@Whitespace+in+Strings):
|
2039
2168
|
#
|
2040
|
-
#
|
2169
|
+
# whitespace = "\x00\t\n\v\f\r "
|
2170
|
+
# s = whitespace + 'abc' + whitespace
|
2171
|
+
# s # => "\u0000\t\n\v\f\r abc\u0000\t\n\v\f\r "
|
2172
|
+
# s.lstrip # => "abc\u0000\t\n\v\f\r "
|
2041
2173
|
#
|
2042
|
-
#
|
2043
|
-
# "hello".lstrip #=> "hello"
|
2174
|
+
# Related: String#rstrip, String#strip.
|
2044
2175
|
#
|
2045
2176
|
def lstrip: () -> String
|
2046
2177
|
|
2047
2178
|
# <!--
|
2048
2179
|
# rdoc-file=string.c
|
2049
|
-
# -
|
2180
|
+
# - lstrip! -> self or nil
|
2050
2181
|
# -->
|
2051
|
-
#
|
2052
|
-
# `
|
2182
|
+
# Like String#lstrip, except that any modifications are made in `self`; returns
|
2183
|
+
# `self` if any modification are made, `nil` otherwise.
|
2053
2184
|
#
|
2054
|
-
#
|
2055
|
-
#
|
2056
|
-
# " hello ".lstrip! #=> "hello "
|
2057
|
-
# "hello ".lstrip! #=> nil
|
2058
|
-
# "hello".lstrip! #=> nil
|
2185
|
+
# Related: String#rstrip!, String#strip!.
|
2059
2186
|
#
|
2060
2187
|
def lstrip!: () -> self?
|
2061
2188
|
|
@@ -2064,10 +2191,9 @@ class String
|
|
2064
2191
|
# - match(pattern, offset = 0) -> matchdata or nil
|
2065
2192
|
# - match(pattern, offset = 0) {|matchdata| ... } -> object
|
2066
2193
|
# -->
|
2067
|
-
# Returns a
|
2194
|
+
# Returns a MatchData object (or `nil`) based on `self` and the given `pattern`.
|
2068
2195
|
#
|
2069
|
-
# Note: also updates
|
2070
|
-
# variables](Regexp.html#class-Regexp-label-Special+global+variables).
|
2196
|
+
# Note: also updates Regexp@Special+global+variables.
|
2071
2197
|
#
|
2072
2198
|
# * Computes `regexp` by converting `pattern` (if not already a Regexp).
|
2073
2199
|
# regexp = Regexp.new(pattern)
|
@@ -2105,13 +2231,12 @@ class String
|
|
2105
2231
|
# Returns `true` or `false` based on whether a match is found for `self` and
|
2106
2232
|
# `pattern`.
|
2107
2233
|
#
|
2108
|
-
# Note: does not update
|
2109
|
-
# variables](Regexp.html#class-Regexp-label-Special+global+variables).
|
2234
|
+
# Note: does not update Regexp@Special+global+variables.
|
2110
2235
|
#
|
2111
2236
|
# Computes `regexp` by converting `pattern` (if not already a Regexp).
|
2112
2237
|
# regexp = Regexp.new(pattern)
|
2113
2238
|
#
|
2114
|
-
# Returns `true` if `self+.match(regexp)` returns a
|
2239
|
+
# Returns `true` if `self+.match(regexp)` returns a MatchData object, `false`
|
2115
2240
|
# otherwise:
|
2116
2241
|
#
|
2117
2242
|
# 'foo'.match?(/o/) # => true
|
@@ -2189,43 +2314,64 @@ class String
|
|
2189
2314
|
|
2190
2315
|
# <!--
|
2191
2316
|
# rdoc-file=string.c
|
2192
|
-
# -
|
2317
|
+
# - oct -> integer
|
2193
2318
|
# -->
|
2194
|
-
#
|
2195
|
-
# optional sign) and returns the corresponding number
|
2196
|
-
#
|
2319
|
+
# Interprets the leading substring of `self` as a string of octal digits (with
|
2320
|
+
# an optional sign) and returns the corresponding number; returns zero if there
|
2321
|
+
# is no such leading substring:
|
2322
|
+
#
|
2323
|
+
# '123'.oct # => 83
|
2324
|
+
# '-377'.oct # => -255
|
2325
|
+
# '0377non-numeric'.oct # => 255
|
2326
|
+
# 'non-numeric'.oct # => 0
|
2197
2327
|
#
|
2198
|
-
#
|
2199
|
-
# "-377".oct #=> -255
|
2200
|
-
# "bad".oct #=> 0
|
2201
|
-
# "0377bad".oct #=> 255
|
2328
|
+
# If `self` starts with `0`, radix indicators are honored; see Kernel#Integer.
|
2202
2329
|
#
|
2203
|
-
#
|
2330
|
+
# Related: String#hex.
|
2204
2331
|
#
|
2205
2332
|
def oct: () -> Integer
|
2206
2333
|
|
2207
2334
|
# <!--
|
2208
2335
|
# rdoc-file=string.c
|
2209
|
-
# -
|
2336
|
+
# - ord -> integer
|
2210
2337
|
# -->
|
2211
|
-
# Returns the
|
2338
|
+
# Returns the integer ordinal of the first character of `self`:
|
2212
2339
|
#
|
2213
|
-
#
|
2340
|
+
# 'h'.ord # => 104
|
2341
|
+
# 'hello'.ord # => 104
|
2342
|
+
# 'тест'.ord # => 1090
|
2343
|
+
# 'こんにちは'.ord # => 12371
|
2214
2344
|
#
|
2215
2345
|
def ord: () -> Integer
|
2216
2346
|
|
2217
2347
|
# <!--
|
2218
2348
|
# rdoc-file=string.c
|
2219
|
-
# -
|
2220
|
-
# - str.partition(regexp) -> [head, match, tail]
|
2349
|
+
# - partition(string_or_regexp) -> [head, match, tail]
|
2221
2350
|
# -->
|
2222
|
-
#
|
2223
|
-
#
|
2224
|
-
#
|
2351
|
+
# Returns a 3-element array of substrings of `self`.
|
2352
|
+
#
|
2353
|
+
# Matches a pattern against `self`, scanning from the beginning. The pattern is:
|
2354
|
+
#
|
2355
|
+
# * `string_or_regexp` itself, if it is a Regexp.
|
2356
|
+
# * `Regexp.quote(string_or_regexp)`, if `string_or_regexp` is a string.
|
2357
|
+
#
|
2225
2358
|
#
|
2226
|
-
#
|
2227
|
-
#
|
2228
|
-
#
|
2359
|
+
# If the pattern is matched, returns pre-match, first-match, post-match:
|
2360
|
+
#
|
2361
|
+
# 'hello'.partition('l') # => ["he", "l", "lo"]
|
2362
|
+
# 'hello'.partition('ll') # => ["he", "ll", "o"]
|
2363
|
+
# 'hello'.partition('h') # => ["", "h", "ello"]
|
2364
|
+
# 'hello'.partition('o') # => ["hell", "o", ""]
|
2365
|
+
# 'hello'.partition(/l+/) #=> ["he", "ll", "o"]
|
2366
|
+
# 'hello'.partition('') # => ["", "", "hello"]
|
2367
|
+
# 'тест'.partition('т') # => ["", "т", "ест"]
|
2368
|
+
# 'こんにちは'.partition('に') # => ["こん", "に", "ちは"]
|
2369
|
+
#
|
2370
|
+
# If the pattern is not matched, returns a copy of `self` and two empty strings:
|
2371
|
+
#
|
2372
|
+
# 'hello'.partition('x') # => ["hello", "", ""]
|
2373
|
+
#
|
2374
|
+
# Related: String#rpartition, String#split.
|
2229
2375
|
#
|
2230
2376
|
def partition: (Regexp | string sep_or_regexp) -> [ String, String, String ]
|
2231
2377
|
|
@@ -2333,102 +2479,123 @@ class String
|
|
2333
2479
|
|
2334
2480
|
# <!--
|
2335
2481
|
# rdoc-file=string.c
|
2336
|
-
# -
|
2482
|
+
# - rjust(size, pad_string = ' ') -> new_string
|
2337
2483
|
# -->
|
2338
|
-
#
|
2339
|
-
#
|
2340
|
-
#
|
2484
|
+
# Returns a right-justified copy of `self`.
|
2485
|
+
#
|
2486
|
+
# If integer argument `size` is greater than the size (in characters) of `self`,
|
2487
|
+
# returns a new string of length `size` that is a copy of `self`, right
|
2488
|
+
# justified and padded on the left with `pad_string`:
|
2489
|
+
#
|
2490
|
+
# 'hello'.rjust(10) # => " hello"
|
2491
|
+
# 'hello '.rjust(10) # => " hello "
|
2492
|
+
# 'hello'.rjust(10, 'ab') # => "ababahello"
|
2493
|
+
# 'тест'.rjust(10) # => " тест"
|
2494
|
+
# 'こんにちは'.rjust(10) # => " こんにちは"
|
2341
2495
|
#
|
2342
|
-
#
|
2343
|
-
#
|
2344
|
-
#
|
2496
|
+
# If `size` is not greater than the size of `self`, returns a copy of `self`:
|
2497
|
+
#
|
2498
|
+
# 'hello'.rjust(5, 'ab') # => "hello"
|
2499
|
+
# 'hello'.rjust(1, 'ab') # => "hello"
|
2500
|
+
#
|
2501
|
+
# Related: String#ljust, String#center.
|
2345
2502
|
#
|
2346
2503
|
def rjust: (int integer, ?string padstr) -> String
|
2347
2504
|
|
2348
2505
|
# <!--
|
2349
2506
|
# rdoc-file=string.c
|
2350
|
-
# -
|
2351
|
-
# - str.rpartition(regexp) -> [head, match, tail]
|
2507
|
+
# - rpartition(sep) -> [head, match, tail]
|
2352
2508
|
# -->
|
2353
|
-
#
|
2354
|
-
# and returns the part before it, the match, and the part after it. If it is not
|
2355
|
-
# found, returns two empty strings and *str*.
|
2509
|
+
# Returns a 3-element array of substrings of `self`.
|
2356
2510
|
#
|
2357
|
-
#
|
2358
|
-
#
|
2359
|
-
# "hello".rpartition(/.l/) #=> ["he", "ll", "o"]
|
2511
|
+
# Matches a pattern against `self`, scanning backwards from the end. The pattern
|
2512
|
+
# is:
|
2360
2513
|
#
|
2361
|
-
#
|
2362
|
-
#
|
2514
|
+
# * `string_or_regexp` itself, if it is a Regexp.
|
2515
|
+
# * `Regexp.quote(string_or_regexp)`, if `string_or_regexp` is a string.
|
2363
2516
|
#
|
2364
|
-
# "hello".rpartition(/l+/) #=> ["hel", "l", "o"]
|
2365
2517
|
#
|
2366
|
-
#
|
2367
|
-
# lookbehind.
|
2518
|
+
# If the pattern is matched, returns pre-match, last-match, post-match:
|
2368
2519
|
#
|
2369
|
-
#
|
2520
|
+
# 'hello'.rpartition('l') # => ["hel", "l", "o"]
|
2521
|
+
# 'hello'.rpartition('ll') # => ["he", "ll", "o"]
|
2522
|
+
# 'hello'.rpartition('h') # => ["", "h", "ello"]
|
2523
|
+
# 'hello'.rpartition('o') # => ["hell", "o", ""]
|
2524
|
+
# 'hello'.rpartition(/l+/) # => ["hel", "l", "o"]
|
2525
|
+
# 'hello'.rpartition('') # => ["hello", "", ""]
|
2526
|
+
# 'тест'.rpartition('т') # => ["тес", "т", ""]
|
2527
|
+
# 'こんにちは'.rpartition('に') # => ["こん", "に", "ちは"]
|
2370
2528
|
#
|
2371
|
-
#
|
2529
|
+
# If the pattern is not matched, returns two empty strings and a copy of `self`:
|
2372
2530
|
#
|
2373
|
-
#
|
2531
|
+
# 'hello'.rpartition('x') # => ["", "", "hello"]
|
2532
|
+
#
|
2533
|
+
# Related: String#partition, String#split.
|
2374
2534
|
#
|
2375
2535
|
def rpartition: (string | Regexp sep_or_regexp) -> [ String, String, String ]
|
2376
2536
|
|
2377
2537
|
# <!--
|
2378
2538
|
# rdoc-file=string.c
|
2379
|
-
# -
|
2539
|
+
# - rstrip -> new_string
|
2380
2540
|
# -->
|
2381
|
-
# Returns a copy of the receiver with trailing whitespace removed
|
2382
|
-
#
|
2541
|
+
# Returns a copy of the receiver with trailing whitespace removed; see
|
2542
|
+
# [Whitespace in Strings](rdoc-ref:String@Whitespace+in+Strings):
|
2383
2543
|
#
|
2384
|
-
#
|
2544
|
+
# whitespace = "\x00\t\n\v\f\r "
|
2545
|
+
# s = whitespace + 'abc' + whitespace
|
2546
|
+
# s # => "\u0000\t\n\v\f\r abc\u0000\t\n\v\f\r "
|
2547
|
+
# s.rstrip # => "\u0000\t\n\v\f\r abc"
|
2385
2548
|
#
|
2386
|
-
#
|
2387
|
-
# "hello".rstrip #=> "hello"
|
2549
|
+
# Related: String#lstrip, String#strip.
|
2388
2550
|
#
|
2389
2551
|
def rstrip: () -> String
|
2390
2552
|
|
2391
2553
|
# <!--
|
2392
2554
|
# rdoc-file=string.c
|
2393
|
-
# -
|
2555
|
+
# - rstrip! -> self or nil
|
2394
2556
|
# -->
|
2395
|
-
#
|
2396
|
-
#
|
2397
|
-
#
|
2398
|
-
# Refer to String#strip for the definition of whitespace.
|
2557
|
+
# Like String#rstrip, except that any modifications are made in `self`; returns
|
2558
|
+
# `self` if any modification are made, `nil` otherwise.
|
2399
2559
|
#
|
2400
|
-
#
|
2401
|
-
# " hello".rstrip! #=> nil
|
2402
|
-
# "hello".rstrip! #=> nil
|
2560
|
+
# Related: String#lstrip!, String#strip!.
|
2403
2561
|
#
|
2404
2562
|
def rstrip!: () -> self?
|
2405
2563
|
|
2406
2564
|
# <!--
|
2407
2565
|
# rdoc-file=string.c
|
2408
|
-
# -
|
2409
|
-
# -
|
2566
|
+
# - scan(string_or_regexp) -> array
|
2567
|
+
# - scan(string_or_regexp) {|matches| ... } -> self
|
2410
2568
|
# -->
|
2411
|
-
#
|
2412
|
-
#
|
2413
|
-
#
|
2414
|
-
#
|
2415
|
-
#
|
2416
|
-
#
|
2569
|
+
# Matches a pattern against `self`; the pattern is:
|
2570
|
+
#
|
2571
|
+
# * `string_or_regexp` itself, if it is a Regexp.
|
2572
|
+
# * `Regexp.quote(string_or_regexp)`, if `string_or_regexp` is a string.
|
2573
|
+
#
|
2574
|
+
#
|
2575
|
+
# Iterates through `self`, generating a collection of matching results:
|
2417
2576
|
#
|
2418
|
-
#
|
2419
|
-
#
|
2420
|
-
#
|
2421
|
-
#
|
2422
|
-
# a.scan(/(..)(..)/) #=> [["cr", "ue"], ["l ", "wo"]]
|
2577
|
+
# * If the pattern contains no groups, each result is the matched string,
|
2578
|
+
# `$&`.
|
2579
|
+
# * If the pattern contains groups, each result is an array containing one
|
2580
|
+
# entry per group.
|
2423
2581
|
#
|
2424
|
-
# And the block form:
|
2425
2582
|
#
|
2426
|
-
#
|
2583
|
+
# With no block given, returns an array of the results:
|
2584
|
+
#
|
2585
|
+
# s = 'cruel world'
|
2586
|
+
# s.scan(/\w+/) # => ["cruel", "world"]
|
2587
|
+
# s.scan(/.../) # => ["cru", "el ", "wor"]
|
2588
|
+
# s.scan(/(...)/) # => [["cru"], ["el "], ["wor"]]
|
2589
|
+
# s.scan(/(..)(..)/) # => [["cr", "ue"], ["l ", "wo"]]
|
2590
|
+
#
|
2591
|
+
# With a block given, calls the block with each result; returns `self`:
|
2592
|
+
#
|
2593
|
+
# s.scan(/\w+/) {|w| print "<<#{w}>> " }
|
2427
2594
|
# print "\n"
|
2428
|
-
#
|
2595
|
+
# s.scan(/(.)(.)/) {|x,y| print y, x }
|
2429
2596
|
# print "\n"
|
2430
2597
|
#
|
2431
|
-
#
|
2598
|
+
# Output:
|
2432
2599
|
#
|
2433
2600
|
# <<cruel>> <<world>>
|
2434
2601
|
# rceu lowlr
|
@@ -2438,34 +2605,44 @@ class String
|
|
2438
2605
|
|
2439
2606
|
# <!--
|
2440
2607
|
# rdoc-file=string.c
|
2441
|
-
# -
|
2442
|
-
# -
|
2443
|
-
# - str.scrub{|bytes|} -> new_str
|
2608
|
+
# - scrub(replacement_string = default_replacement) -> new_string
|
2609
|
+
# - scrub{|bytes| ... } -> new_string
|
2444
2610
|
# -->
|
2445
|
-
#
|
2446
|
-
#
|
2447
|
-
#
|
2611
|
+
# Returns a copy of `self` with each invalid byte sequence replaced by the given
|
2612
|
+
# `replacement_string`.
|
2613
|
+
#
|
2614
|
+
# With no block given and no argument, replaces each invalid sequence with the
|
2615
|
+
# default replacement string (`"�"` for a Unicode encoding, `'?'` otherwise):
|
2616
|
+
#
|
2617
|
+
# s = "foo\x81\x81bar"
|
2618
|
+
# s.scrub # => "foo��bar"
|
2448
2619
|
#
|
2449
|
-
#
|
2450
|
-
#
|
2451
|
-
#
|
2620
|
+
# With no block given and argument `replacement_string` given, replaces each
|
2621
|
+
# invalid sequence with that string:
|
2622
|
+
#
|
2623
|
+
# "foo\x81\x81bar".scrub('xyzzy') # => "fooxyzzyxyzzybar"
|
2624
|
+
#
|
2625
|
+
# With a block given, replaces each invalid sequence with the value of the
|
2626
|
+
# block:
|
2627
|
+
#
|
2628
|
+
# "foo\x81\x81bar".scrub {|bytes| p bytes; 'XYZZY' }
|
2629
|
+
# # => "fooXYZZYXYZZYbar"
|
2630
|
+
#
|
2631
|
+
# Output:
|
2632
|
+
#
|
2633
|
+
# "\x81"
|
2634
|
+
# "\x81"
|
2452
2635
|
#
|
2453
2636
|
def scrub: (?string repl) -> String
|
2454
2637
|
| () { (String bytes) -> string } -> String
|
2455
2638
|
|
2456
2639
|
# <!--
|
2457
2640
|
# rdoc-file=string.c
|
2458
|
-
# -
|
2459
|
-
# -
|
2460
|
-
# -
|
2641
|
+
# - scrub! -> self
|
2642
|
+
# - scrub!(replacement_string = default_replacement) -> self
|
2643
|
+
# - scrub!{|bytes| ... } -> self
|
2461
2644
|
# -->
|
2462
|
-
#
|
2463
|
-
# replacement character, else returns self. If block is given, replace invalid
|
2464
|
-
# bytes with returned value of the block.
|
2465
|
-
#
|
2466
|
-
# "abc\u3042\x81".scrub! #=> "abc\u3042\uFFFD"
|
2467
|
-
# "abc\u3042\x81".scrub!("*") #=> "abc\u3042*"
|
2468
|
-
# "abc\u3042\xE3\x80".scrub!{|bytes| '<'+bytes.unpack1('H*')+'>' } #=> "abc\u3042<e380>"
|
2645
|
+
# Like String#scrub, except that any replacements are made in `self`.
|
2469
2646
|
#
|
2470
2647
|
def scrub!: (?string repl) -> self
|
2471
2648
|
| () { (String bytes) -> string } -> self
|
@@ -2487,96 +2664,23 @@ class String
|
|
2487
2664
|
# <!-- rdoc-file=string.c -->
|
2488
2665
|
# Returns the count of characters (not bytes) in `self`:
|
2489
2666
|
#
|
2490
|
-
#
|
2491
|
-
#
|
2667
|
+
# 'foo'.length # => 3
|
2668
|
+
# 'тест'.length # => 4
|
2669
|
+
# 'こんにちは'.length # => 5
|
2492
2670
|
#
|
2493
|
-
#
|
2671
|
+
# Contrast with String#bytesize:
|
2672
|
+
#
|
2673
|
+
# 'foo'.bytesize # => 3
|
2674
|
+
# 'тест'.bytesize # => 8
|
2675
|
+
# 'こんにちは'.bytesize # => 15
|
2494
2676
|
#
|
2495
|
-
#
|
2677
|
+
# String#size is an alias for String#length.
|
2496
2678
|
#
|
2497
2679
|
alias size length
|
2498
2680
|
|
2499
2681
|
# <!-- 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:
|
2532
|
-
#
|
2533
|
-
# 'foo'[4, 2] # => nil
|
2534
|
-
# 'foo'[-4, 2] # => nil
|
2535
|
-
#
|
2536
|
-
# Returns the trailing substring of `self` if `length` is large:
|
2537
|
-
#
|
2538
|
-
# 'foo'[1, 50] # => "oo"
|
2539
|
-
#
|
2540
|
-
# Returns `nil` if `length` is negative:
|
2541
|
-
#
|
2542
|
-
# 'foo'[0, -1] # => nil
|
2543
|
-
#
|
2544
|
-
# When the single Range argument `range` is given, derives `start` and `length`
|
2545
|
-
# values from the given `range`, and returns values as above:
|
2546
|
-
#
|
2547
|
-
# * `'foo'[0..1]` is equivalent to `'foo'[0, 2]`.
|
2548
|
-
# * `'foo'[0...1]` is equivalent to `'foo'[0, 1]`.
|
2549
|
-
#
|
2550
|
-
#
|
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:
|
2553
|
-
#
|
2554
|
-
# 'foo'[/o/] # => "o"
|
2555
|
-
# 'foo'[/x/] # => nil
|
2556
|
-
# s = 'hello there'
|
2557
|
-
# s[/[aeiou](.)\1/] # => "ell"
|
2558
|
-
# s[/[aeiou](.)\1/, 0] # => "ell"
|
2559
|
-
#
|
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)):
|
2564
|
-
#
|
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#[].
|
2682
|
+
# Returns the substring of `self` specified by the arguments. See examples at
|
2683
|
+
# [String Slices](rdoc-ref:String@String+Slices).
|
2580
2684
|
#
|
2581
2685
|
alias slice []
|
2582
2686
|
|
@@ -2588,10 +2692,8 @@ class String
|
|
2588
2692
|
# - slice!(regexp, capture = 0) -> new_string or nil
|
2589
2693
|
# - slice!(substring) -> new_string or nil
|
2590
2694
|
# -->
|
2591
|
-
# Removes the substring of `self` specified by the arguments
|
2592
|
-
#
|
2593
|
-
#
|
2594
|
-
# See String#[] for details about the arguments that specify the substring.
|
2695
|
+
# Removes and returns the substring of `self` specified by the arguments. See
|
2696
|
+
# [String Slices](rdoc-ref:String@String+Slices).
|
2595
2697
|
#
|
2596
2698
|
# A few examples:
|
2597
2699
|
#
|
@@ -2609,66 +2711,104 @@ class String
|
|
2609
2711
|
|
2610
2712
|
# <!--
|
2611
2713
|
# rdoc-file=string.c
|
2612
|
-
# -
|
2613
|
-
# -
|
2714
|
+
# - split(field_sep = $;, limit = nil) -> array
|
2715
|
+
# - split(field_sep = $;, limit = nil) {|substring| ... } -> self
|
2614
2716
|
# -->
|
2615
|
-
#
|
2616
|
-
#
|
2717
|
+
# Returns an array of substrings of `self` that are the result of splitting
|
2718
|
+
# `self` at each occurrence of the given field separator `field_sep`.
|
2719
|
+
#
|
2720
|
+
# When `field_sep` is `$;`:
|
2721
|
+
#
|
2722
|
+
# * If `$;` is `nil` (its default value), the split occurs just as if
|
2723
|
+
# `field_sep` were given as a space character (see below).
|
2724
|
+
#
|
2725
|
+
# * If `$;` is a string, the split ocurs just as if `field_sep` were given as
|
2726
|
+
# that string (see below).
|
2727
|
+
#
|
2728
|
+
#
|
2729
|
+
# When `field_sep` is `' '` and `limit` is `nil`, the split occurs at each
|
2730
|
+
# sequence of whitespace:
|
2731
|
+
#
|
2732
|
+
# 'abc def ghi'.split(' ') => ["abc", "def", "ghi"]
|
2733
|
+
# "abc \n\tdef\t\n ghi".split(' ') # => ["abc", "def", "ghi"]
|
2734
|
+
# 'abc def ghi'.split(' ') => ["abc", "def", "ghi"]
|
2735
|
+
# ''.split(' ') => []
|
2736
|
+
#
|
2737
|
+
# When `field_sep` is a string different from `' '` and `limit` is `nil`, the
|
2738
|
+
# split occurs at each occurrence of `field_sep`; trailing empty substrings are
|
2739
|
+
# not returned:
|
2740
|
+
#
|
2741
|
+
# 'abracadabra'.split('ab') => ["", "racad", "ra"]
|
2742
|
+
# 'aaabcdaaa'.split('a') => ["", "", "", "bcd"]
|
2743
|
+
# ''.split('a') => []
|
2744
|
+
# '3.14159'.split('1') => ["3.", "4", "59"]
|
2745
|
+
# '!@#$%^$&*($)_+'.split('$') # => ["!@#", "%^", "&*(", ")_+"]
|
2746
|
+
# 'тест'.split('т') => ["", "ес"]
|
2747
|
+
# 'こんにちは'.split('に') => ["こん", "ちは"]
|
2748
|
+
#
|
2749
|
+
# When `field_sep` is a Regexp and `limit` is `nil`, the split occurs at each
|
2750
|
+
# occurrence of a match; trailing empty substrings are not returned:
|
2751
|
+
#
|
2752
|
+
# 'abracadabra'.split(/ab/) # => ["", "racad", "ra"]
|
2753
|
+
# 'aaabcdaaa'.split(/a/) => ["", "", "", "bcd"]
|
2754
|
+
# 'aaabcdaaa'.split(//) => ["a", "a", "a", "b", "c", "d", "a", "a", "a"]
|
2755
|
+
# '1 + 1 == 2'.split(/\W+/) # => ["1", "1", "2"]
|
2617
2756
|
#
|
2618
|
-
# If
|
2619
|
-
#
|
2620
|
-
# with leading and trailing whitespace and runs of contiguous whitespace
|
2621
|
-
# characters ignored.
|
2757
|
+
# If the Regexp contains groups, their matches are also included in the returned
|
2758
|
+
# array:
|
2622
2759
|
#
|
2623
|
-
#
|
2624
|
-
# the pattern matches a zero-length string, *str* is split into individual
|
2625
|
-
# characters. If *pattern* contains groups, the respective matches will be
|
2626
|
-
# returned in the array as well.
|
2760
|
+
# '1:2:3'.split(/(:)()()/, 2) # => ["1", ":", "", "", "2:3"]
|
2627
2761
|
#
|
2628
|
-
#
|
2629
|
-
# the
|
2762
|
+
# As seen above, if `limit` is `nil`, trailing empty substrings are not
|
2763
|
+
# returned; the same is true if `limit` is zero:
|
2630
2764
|
#
|
2631
|
-
#
|
2632
|
-
#
|
2633
|
-
# returned (captured groups will be returned as well, but are not counted
|
2634
|
-
# towards the limit). If *limit* is `1`, the entire string is returned as the
|
2635
|
-
# only entry in an array. If negative, there is no limit to the number of fields
|
2636
|
-
# returned, and trailing null fields are not suppressed.
|
2765
|
+
# 'aaabcdaaa'.split('a') => ["", "", "", "bcd"]
|
2766
|
+
# 'aaabcdaaa'.split('a', 0) # => ["", "", "", "bcd"]
|
2637
2767
|
#
|
2638
|
-
#
|
2639
|
-
#
|
2768
|
+
# If `limit` is positive integer `n`, no more than `n - 1-` splits occur, so
|
2769
|
+
# that at most `n` substrings are returned, and trailing empty substrings are
|
2770
|
+
# included:
|
2640
2771
|
#
|
2641
|
-
#
|
2642
|
-
#
|
2643
|
-
#
|
2644
|
-
#
|
2645
|
-
#
|
2646
|
-
# "hello".split(//, 3) #=> ["h", "e", "llo"]
|
2647
|
-
# "hi mom".split(%r{\s*}) #=> ["h", "i", "m", "o", "m"]
|
2772
|
+
# 'aaabcdaaa'.split('a', 1) # => ["aaabcdaaa"]
|
2773
|
+
# 'aaabcdaaa'.split('a', 2) # => ["", "aabcdaaa"]
|
2774
|
+
# 'aaabcdaaa'.split('a', 5) # => ["", "", "", "bcd", "aa"]
|
2775
|
+
# 'aaabcdaaa'.split('a', 7) # => ["", "", "", "bcd", "", "", ""]
|
2776
|
+
# 'aaabcdaaa'.split('a', 8) # => ["", "", "", "bcd", "", "", ""]
|
2648
2777
|
#
|
2649
|
-
#
|
2650
|
-
#
|
2651
|
-
# "1,2,,3,4,,".split(',', 4) #=> ["1", "2", "", "3,4,,"]
|
2652
|
-
# "1,2,,3,4,,".split(',', -4) #=> ["1", "2", "", "3", "4", "", ""]
|
2778
|
+
# Note that if `field_sep` is a Regexp containing groups, their matches are in
|
2779
|
+
# the returned array, but do not count toward the limit.
|
2653
2780
|
#
|
2654
|
-
#
|
2781
|
+
# If `limit` is negative, it behaves the same as if `limit` was `nil`, meaning
|
2782
|
+
# that there is no limit, and trailing empty substrings are included:
|
2655
2783
|
#
|
2656
|
-
#
|
2784
|
+
# 'aaabcdaaa'.split('a', -1) # => ["", "", "", "bcd", "", "", ""]
|
2657
2785
|
#
|
2658
|
-
# If a block is given,
|
2786
|
+
# If a block is given, it is called with each substring:
|
2787
|
+
#
|
2788
|
+
# 'abc def ghi'.split(' ') {|substring| p substring }
|
2789
|
+
#
|
2790
|
+
# Output:
|
2791
|
+
#
|
2792
|
+
# "abc"
|
2793
|
+
# "def"
|
2794
|
+
# "ghi"
|
2795
|
+
#
|
2796
|
+
# Related: String#partition, String#rpartition.
|
2659
2797
|
#
|
2660
2798
|
def split: (?Regexp | string pattern, ?int limit) -> Array[String]
|
2661
2799
|
| (?Regexp | string pattern, ?int limit) { (String) -> void } -> self
|
2662
2800
|
|
2663
2801
|
# <!--
|
2664
2802
|
# rdoc-file=string.c
|
2665
|
-
# -
|
2803
|
+
# - squeeze(*selectors) -> new_string
|
2666
2804
|
# -->
|
2667
|
-
#
|
2668
|
-
#
|
2669
|
-
#
|
2670
|
-
#
|
2671
|
-
#
|
2805
|
+
# Returns a copy of `self` with characters specified by `selectors` "squeezed"
|
2806
|
+
# (see [Multiple Character
|
2807
|
+
# Selectors](rdoc-ref:character_selectors.rdoc@Multiple+Character+Selectors)):
|
2808
|
+
#
|
2809
|
+
# "Squeezed" means that each multiple-character run of a selected character is
|
2810
|
+
# squeezed down to a single character; with no arguments given, squeezes all
|
2811
|
+
# characters:
|
2672
2812
|
#
|
2673
2813
|
# "yellow moon".squeeze #=> "yelow mon"
|
2674
2814
|
# " now is the".squeeze(" ") #=> " now is the"
|
@@ -2678,56 +2818,63 @@ class String
|
|
2678
2818
|
|
2679
2819
|
# <!--
|
2680
2820
|
# rdoc-file=string.c
|
2681
|
-
# -
|
2821
|
+
# - squeeze!(*selectors) -> self or nil
|
2682
2822
|
# -->
|
2683
|
-
#
|
2684
|
-
# made.
|
2823
|
+
# Like String#squeeze, but modifies `self` in place. Returns `self` if any
|
2824
|
+
# changes were made, `nil` otherwise.
|
2685
2825
|
#
|
2686
2826
|
def squeeze!: (*string other_str) -> self?
|
2687
2827
|
|
2688
2828
|
# <!--
|
2689
2829
|
# rdoc-file=string.c
|
2690
|
-
# -
|
2830
|
+
# - start_with?(*string_or_regexp) -> true or false
|
2691
2831
|
# -->
|
2692
|
-
# Returns
|
2693
|
-
#
|
2832
|
+
# Returns whether `self` starts with any of the given `string_or_regexp`.
|
2833
|
+
#
|
2834
|
+
# Matches patterns against the beginning of `self`. For each given
|
2835
|
+
# `string_or_regexp`, the pattern is:
|
2694
2836
|
#
|
2695
|
-
#
|
2696
|
-
#
|
2837
|
+
# * `string_or_regexp` itself, if it is a Regexp.
|
2838
|
+
# * `Regexp.quote(string_or_regexp)`, if `string_or_regexp` is a string.
|
2697
2839
|
#
|
2698
|
-
#
|
2699
|
-
#
|
2700
|
-
#
|
2840
|
+
#
|
2841
|
+
# Returns `true` if any pattern matches the beginning, `false` otherwise:
|
2842
|
+
#
|
2843
|
+
# 'hello'.start_with?('hell') # => true
|
2844
|
+
# 'hello'.start_with?(/H/i) # => true
|
2845
|
+
# 'hello'.start_with?('heaven', 'hell') # => true
|
2846
|
+
# 'hello'.start_with?('heaven', 'paradise') # => false
|
2847
|
+
# 'тест'.start_with?('т') # => true
|
2848
|
+
# 'こんにちは'.start_with?('こ') # => true
|
2849
|
+
#
|
2850
|
+
# Related: String#end_with?.
|
2701
2851
|
#
|
2702
2852
|
def start_with?: (*string prefixes) -> bool
|
2703
2853
|
|
2704
2854
|
# <!--
|
2705
2855
|
# rdoc-file=string.c
|
2706
|
-
# -
|
2856
|
+
# - strip -> new_string
|
2707
2857
|
# -->
|
2708
|
-
# Returns a copy of the receiver with leading and trailing whitespace removed
|
2858
|
+
# Returns a copy of the receiver with leading and trailing whitespace removed;
|
2859
|
+
# see [Whitespace in Strings](rdoc-ref:String@Whitespace+in+Strings):
|
2709
2860
|
#
|
2710
|
-
#
|
2711
|
-
#
|
2861
|
+
# whitespace = "\x00\t\n\v\f\r "
|
2862
|
+
# s = whitespace + 'abc' + whitespace
|
2863
|
+
# s # => "\u0000\t\n\v\f\r abc\u0000\t\n\v\f\r "
|
2864
|
+
# s.strip # => "abc"
|
2712
2865
|
#
|
2713
|
-
#
|
2714
|
-
# "\tgoodbye\r\n".strip #=> "goodbye"
|
2715
|
-
# "\x00\t\n\v\f\r ".strip #=> ""
|
2716
|
-
# "hello".strip #=> "hello"
|
2866
|
+
# Related: String#lstrip, String#rstrip.
|
2717
2867
|
#
|
2718
2868
|
def strip: () -> String
|
2719
2869
|
|
2720
2870
|
# <!--
|
2721
2871
|
# rdoc-file=string.c
|
2722
|
-
# -
|
2872
|
+
# - strip! -> self or nil
|
2723
2873
|
# -->
|
2724
|
-
#
|
2725
|
-
#
|
2874
|
+
# Like String#strip, except that any modifications are made in `self`; returns
|
2875
|
+
# `self` if any modification are made, `nil` otherwise.
|
2726
2876
|
#
|
2727
|
-
#
|
2728
|
-
#
|
2729
|
-
# " hello ".strip! #=> "hello"
|
2730
|
-
# "hello".strip! #=> nil
|
2877
|
+
# Related: String#lstrip!, String#strip!.
|
2731
2878
|
#
|
2732
2879
|
def strip!: () -> self?
|
2733
2880
|
|
@@ -2739,7 +2886,7 @@ class String
|
|
2739
2886
|
# Returns a copy of `self` with only the first occurrence (not all occurrences)
|
2740
2887
|
# of the given `pattern` replaced.
|
2741
2888
|
#
|
2742
|
-
# See [Substitution Methods](
|
2889
|
+
# See [Substitution Methods](rdoc-ref:String@Substitution+Methods).
|
2743
2890
|
#
|
2744
2891
|
# Related: String#sub!, String#gsub, String#gsub!.
|
2745
2892
|
#
|
@@ -2754,7 +2901,7 @@ class String
|
|
2754
2901
|
# Returns `self` with only the first occurrence (not all occurrences) of the
|
2755
2902
|
# given `pattern` replaced.
|
2756
2903
|
#
|
2757
|
-
# See [Substitution Methods](
|
2904
|
+
# See [Substitution Methods](rdoc-ref:String@Substitution+Methods).
|
2758
2905
|
#
|
2759
2906
|
# Related: String#sub, String#gsub, String#gsub!.
|
2760
2907
|
#
|
@@ -2832,12 +2979,18 @@ class String
|
|
2832
2979
|
|
2833
2980
|
# <!--
|
2834
2981
|
# rdoc-file=string.c
|
2835
|
-
# -
|
2982
|
+
# - sum(n = 16) -> integer
|
2836
2983
|
# -->
|
2837
|
-
# Returns a basic
|
2838
|
-
#
|
2839
|
-
#
|
2840
|
-
#
|
2984
|
+
# Returns a basic `n`-bit checksum of the characters in `self`; the checksum is
|
2985
|
+
# the sum of the binary value of each byte in `self`, modulo `2**n - 1`:
|
2986
|
+
#
|
2987
|
+
# 'hello'.sum # => 532
|
2988
|
+
# 'hello'.sum(4) # => 4
|
2989
|
+
# 'hello'.sum(64) # => 532
|
2990
|
+
# 'тест'.sum # => 1405
|
2991
|
+
# 'こんにちは'.sum # => 2582
|
2992
|
+
#
|
2993
|
+
# This is not a particularly strong checksum.
|
2841
2994
|
#
|
2842
2995
|
def sum: (?int n) -> Integer
|
2843
2996
|
|
@@ -2852,7 +3005,7 @@ class String
|
|
2852
3005
|
# s.swapcase # => "hELLO wORLD!"
|
2853
3006
|
#
|
2854
3007
|
# The casing may be affected by the given `options`; see [Case
|
2855
|
-
# Mapping](
|
3008
|
+
# Mapping](rdoc-ref:case_mapping.rdoc).
|
2856
3009
|
#
|
2857
3010
|
# Related: String#swapcase!.
|
2858
3011
|
#
|
@@ -2870,11 +3023,11 @@ class String
|
|
2870
3023
|
#
|
2871
3024
|
# s = 'Hello World!' # => "Hello World!"
|
2872
3025
|
# s.swapcase! # => "hELLO wORLD!"
|
2873
|
-
# s # => "
|
3026
|
+
# s # => "hELLO wORLD!"
|
2874
3027
|
# ''.swapcase! # => nil
|
2875
3028
|
#
|
2876
3029
|
# The casing may be affected by the given `options`; see [Case
|
2877
|
-
# Mapping](
|
3030
|
+
# Mapping](rdoc-ref:case_mapping.rdoc).
|
2878
3031
|
#
|
2879
3032
|
# Related: String#swapcase.
|
2880
3033
|
#
|
@@ -2903,6 +3056,12 @@ class String
|
|
2903
3056
|
# '1/2+3/4i'.to_c #=> ((1/2)+(3/4)*i)
|
2904
3057
|
# 'ruby'.to_c #=> (0+0i)
|
2905
3058
|
#
|
3059
|
+
# Polar form:
|
3060
|
+
# include Math
|
3061
|
+
# "1.0@0".to_c #=> (1+0.0i)
|
3062
|
+
# "1.0@#{PI/2}".to_c #=> (0.0+1i)
|
3063
|
+
# "1.0@#{PI}".to_c #=> (-1+0.0i)
|
3064
|
+
#
|
2906
3065
|
# See Kernel.Complex.
|
2907
3066
|
#
|
2908
3067
|
def to_c: () -> Complex
|
@@ -2931,11 +3090,21 @@ class String
|
|
2931
3090
|
# - to_i(base = 10) -> integer
|
2932
3091
|
# -->
|
2933
3092
|
# Returns the result of interpreting leading characters in `self` as an integer
|
2934
|
-
# in the given `base` (which must be in (2..36)):
|
3093
|
+
# in the given `base` (which must be in (0, 2..36)):
|
2935
3094
|
#
|
2936
3095
|
# '123456'.to_i # => 123456
|
2937
3096
|
# '123def'.to_i(16) # => 1195503
|
2938
3097
|
#
|
3098
|
+
# With `base` zero, string `object` may contain leading characters to specify
|
3099
|
+
# the actual base:
|
3100
|
+
#
|
3101
|
+
# '123def'.to_i(0) # => 123
|
3102
|
+
# '0123def'.to_i(0) # => 83
|
3103
|
+
# '0b123def'.to_i(0) # => 1
|
3104
|
+
# '0o123def'.to_i(0) # => 83
|
3105
|
+
# '0d123def'.to_i(0) # => 123
|
3106
|
+
# '0x123def'.to_i(0) # => 1195503
|
3107
|
+
#
|
2939
3108
|
# Characters past a leading valid number (in the given `base`) are ignored:
|
2940
3109
|
#
|
2941
3110
|
# '12.345'.to_i # => 12
|
@@ -3015,67 +3184,76 @@ class String
|
|
3015
3184
|
|
3016
3185
|
# <!--
|
3017
3186
|
# rdoc-file=string.c
|
3018
|
-
# -
|
3187
|
+
# - tr(selector, replacements) -> new_string
|
3019
3188
|
# -->
|
3020
|
-
# Returns a copy of `
|
3021
|
-
# corresponding
|
3022
|
-
#
|
3189
|
+
# Returns a copy of `self` with each character specified by string `selector`
|
3190
|
+
# translated to the corresponding character in string `replacements`. The
|
3191
|
+
# correspondence is *positional*:
|
3192
|
+
#
|
3193
|
+
# * Each occurrence of the first character specified by `selector` is
|
3194
|
+
# translated to the first character in `replacements`.
|
3195
|
+
# * Each occurrence of the second character specified by `selector` is
|
3196
|
+
# translated to the second character in `replacements`.
|
3197
|
+
# * And so on.
|
3023
3198
|
#
|
3024
|
-
# "hello".tr('el', 'ip') #=> "hippo"
|
3025
|
-
# "hello".tr('aeiou', '*') #=> "h*ll*"
|
3026
|
-
# "hello".tr('aeiou', 'AA*') #=> "hAll*"
|
3027
3199
|
#
|
3028
|
-
#
|
3029
|
-
# `from_str` may start with a `^`, which denotes all characters except those
|
3030
|
-
# listed.
|
3200
|
+
# Example:
|
3031
3201
|
#
|
3032
|
-
#
|
3033
|
-
# "hello".tr('^aeiou', '*') #=> "*e**o"
|
3202
|
+
# 'hello'.tr('el', 'ip') #=> "hippo"
|
3034
3203
|
#
|
3035
|
-
#
|
3036
|
-
#
|
3037
|
-
# or `to_str`:
|
3204
|
+
# If `replacements` is shorter than `selector`, it is implicitly padded with its
|
3205
|
+
# own last character:
|
3038
3206
|
#
|
3039
|
-
#
|
3040
|
-
#
|
3207
|
+
# 'hello'.tr('aeiou', '-') # => "h-ll-"
|
3208
|
+
# 'hello'.tr('aeiou', 'AA-') # => "hAll-"
|
3041
3209
|
#
|
3042
|
-
#
|
3043
|
-
#
|
3044
|
-
#
|
3210
|
+
# Arguments `selector` and `replacements` must be valid character selectors (see
|
3211
|
+
# [Character Selectors](rdoc-ref:character_selectors.rdoc)), and may use any of
|
3212
|
+
# its valid forms, including negation, ranges, and escaping:
|
3045
3213
|
#
|
3046
|
-
#
|
3047
|
-
#
|
3214
|
+
# # Negation.
|
3215
|
+
# 'hello'.tr('^aeiou', '-') # => "-e--o"
|
3216
|
+
# # Ranges.
|
3217
|
+
# 'ibm'.tr('b-z', 'a-z') # => "hal"
|
3218
|
+
# # Escapes.
|
3219
|
+
# 'hel^lo'.tr('\^aeiou', '-') # => "h-l-l-" # Escaped leading caret.
|
3220
|
+
# 'i-b-m'.tr('b\-z', 'a-z') # => "ibabm" # Escaped embedded hyphen.
|
3221
|
+
# 'foo\\bar'.tr('ab\\', 'XYZ') # => "fooZYXr" # Escaped backslash.
|
3048
3222
|
#
|
3049
3223
|
def tr: (string from_str, string to_str) -> String
|
3050
3224
|
|
3051
3225
|
# <!--
|
3052
3226
|
# rdoc-file=string.c
|
3053
|
-
# -
|
3227
|
+
# - tr!(selector, replacements) -> self or nil
|
3054
3228
|
# -->
|
3055
|
-
#
|
3056
|
-
#
|
3229
|
+
# Like String#tr, but modifies `self` in place. Returns `self` if any changes
|
3230
|
+
# were made, `nil` otherwise.
|
3057
3231
|
#
|
3058
3232
|
def tr!: (string from_str, string to_str) -> String?
|
3059
3233
|
|
3060
3234
|
# <!--
|
3061
3235
|
# rdoc-file=string.c
|
3062
|
-
# -
|
3236
|
+
# - tr_s(selector, replacements) -> string
|
3063
3237
|
# -->
|
3064
|
-
#
|
3065
|
-
#
|
3238
|
+
# Like String#tr, but also squeezes the modified portions of the translated
|
3239
|
+
# string; returns a new string (translated and squeezed).
|
3066
3240
|
#
|
3067
|
-
#
|
3068
|
-
#
|
3069
|
-
#
|
3241
|
+
# 'hello'.tr_s('l', 'r') #=> "hero"
|
3242
|
+
# 'hello'.tr_s('el', '-') #=> "h-o"
|
3243
|
+
# 'hello'.tr_s('el', 'hx') #=> "hhxo"
|
3244
|
+
#
|
3245
|
+
# Related: String#squeeze.
|
3070
3246
|
#
|
3071
3247
|
def tr_s: (string from_str, string to_str) -> String
|
3072
3248
|
|
3073
3249
|
# <!--
|
3074
3250
|
# rdoc-file=string.c
|
3075
|
-
# -
|
3251
|
+
# - tr_s!(selector, replacements) -> self or nil
|
3076
3252
|
# -->
|
3077
|
-
#
|
3078
|
-
#
|
3253
|
+
# Like String#tr_s, but modifies `self` in place. Returns `self` if any changes
|
3254
|
+
# were made, `nil` otherwise.
|
3255
|
+
#
|
3256
|
+
# Related: String#squeeze!.
|
3079
3257
|
#
|
3080
3258
|
def tr_s!: (string from_str, string to_str) -> String?
|
3081
3259
|
|
@@ -3096,209 +3274,91 @@ class String
|
|
3096
3274
|
|
3097
3275
|
# <!--
|
3098
3276
|
# rdoc-file=string.c
|
3099
|
-
# -
|
3277
|
+
# - unicode_normalize(form = :nfc) -> string
|
3100
3278
|
# -->
|
3101
|
-
#
|
3102
|
-
#
|
3103
|
-
#
|
3104
|
-
#
|
3279
|
+
# Returns a copy of `self` with [Unicode
|
3280
|
+
# normalization](https://unicode.org/reports/tr15) applied.
|
3281
|
+
#
|
3282
|
+
# Argument `form` must be one of the following symbols (see [Unicode
|
3283
|
+
# normalization forms](https://unicode.org/reports/tr15/#Norm_Forms)):
|
3105
3284
|
#
|
3106
|
-
#
|
3107
|
-
#
|
3108
|
-
#
|
3109
|
-
#
|
3285
|
+
# * `:nfc`: Canonical decomposition, followed by canonical composition.
|
3286
|
+
# * `:nfd`: Canonical decomposition.
|
3287
|
+
# * `:nfkc`: Compatibility decomposition, followed by canonical composition.
|
3288
|
+
# * `:nfkd`: Compatibility decomposition.
|
3289
|
+
#
|
3290
|
+
#
|
3291
|
+
# The encoding of `self` must be one of:
|
3292
|
+
#
|
3293
|
+
# * Encoding::UTF_8
|
3294
|
+
# * Encoding::UTF_16BE
|
3295
|
+
# * Encoding::UTF_16LE
|
3296
|
+
# * Encoding::UTF_32BE
|
3297
|
+
# * Encoding::UTF_32LE
|
3298
|
+
# * Encoding::GB18030
|
3299
|
+
# * Encoding::UCS_2BE
|
3300
|
+
# * Encoding::UCS_4BE
|
3301
|
+
#
|
3302
|
+
#
|
3303
|
+
# Examples:
|
3110
3304
|
#
|
3111
|
-
# "a\u0300".unicode_normalize
|
3112
|
-
# "
|
3113
|
-
#
|
3114
|
-
#
|
3115
|
-
# #=> Encoding::CompatibilityError raised
|
3305
|
+
# "a\u0300".unicode_normalize # => "a"
|
3306
|
+
# "\u00E0".unicode_normalize(:nfd) # => "a "
|
3307
|
+
#
|
3308
|
+
# Related: String#unicode_normalize!, String#unicode_normalized?.
|
3116
3309
|
#
|
3117
3310
|
def unicode_normalize: (?:nfc | :nfd | :nfkc | :nfkd) -> String
|
3118
3311
|
|
3119
3312
|
# <!--
|
3120
3313
|
# rdoc-file=string.c
|
3121
|
-
# -
|
3314
|
+
# - unicode_normalize!(form = :nfc) -> self
|
3122
3315
|
# -->
|
3123
|
-
#
|
3124
|
-
#
|
3316
|
+
# Like String#unicode_normalize, except that the normalization is performed on
|
3317
|
+
# `self`.
|
3318
|
+
#
|
3319
|
+
# Related String#unicode_normalized?.
|
3125
3320
|
#
|
3126
3321
|
def unicode_normalize!: (?:nfc | :nfd | :nfkc | :nfkd) -> String
|
3127
3322
|
|
3128
3323
|
# <!--
|
3129
3324
|
# rdoc-file=string.c
|
3130
|
-
# -
|
3325
|
+
# - unicode_normalized?(form = :nfc) -> true or false
|
3131
3326
|
# -->
|
3132
|
-
#
|
3133
|
-
#
|
3327
|
+
# Returns `true` if `self` is in the given `form` of Unicode normalization,
|
3328
|
+
# `false` otherwise. The `form` must be one of `:nfc`, `:nfd`, `:nfkc`, or
|
3329
|
+
# `:nfkd`.
|
3330
|
+
#
|
3331
|
+
# Examples:
|
3134
3332
|
#
|
3135
|
-
#
|
3136
|
-
#
|
3333
|
+
# "a\u0300".unicode_normalized? # => false
|
3334
|
+
# "a\u0300".unicode_normalized?(:nfd) # => true
|
3335
|
+
# "\u00E0".unicode_normalized? # => true
|
3336
|
+
# "\u00E0".unicode_normalized?(:nfd) # => false
|
3137
3337
|
#
|
3138
|
-
#
|
3139
|
-
#
|
3140
|
-
# "\
|
3141
|
-
#
|
3142
|
-
#
|
3143
|
-
#
|
3338
|
+
# Raises an exception if `self` is not in a Unicode encoding:
|
3339
|
+
#
|
3340
|
+
# s = "\xE0".force_encoding('ISO-8859-1')
|
3341
|
+
# s.unicode_normalized? # Raises Encoding::CompatibilityError.
|
3342
|
+
#
|
3343
|
+
# Related: String#unicode_normalize, String#unicode_normalize!.
|
3144
3344
|
#
|
3145
3345
|
def unicode_normalized?: (?:nfc | :nfd | :nfkc | :nfkd) -> bool
|
3146
3346
|
|
3147
3347
|
# <!--
|
3148
3348
|
# rdoc-file=pack.rb
|
3149
|
-
# -
|
3150
|
-
#
|
3151
|
-
#
|
3152
|
-
#
|
3153
|
-
# returning an array of each value extracted. The format string consists of a
|
3154
|
-
# sequence of single-character directives, summarized in the table at the end of
|
3155
|
-
# this entry. Each directive may be followed by a number, indicating the number
|
3156
|
-
# of times to repeat with this directive. An asterisk (```*`'') will use up all
|
3157
|
-
# remaining elements. The directives `sSiIlL` may each be followed by an
|
3158
|
-
# underscore (```_`'') or exclamation mark (```!`'') to use the underlying
|
3159
|
-
# platform's native size for the specified type; otherwise, it uses a
|
3160
|
-
# platform-independent consistent size. Spaces are ignored in the format string.
|
3161
|
-
#
|
3162
|
-
# See also String#unpack1, Array#pack.
|
3163
|
-
#
|
3164
|
-
# "abc \0\0abc \0\0".unpack('A6Z6') #=> ["abc", "abc "]
|
3165
|
-
# "abc \0\0".unpack('a3a3') #=> ["abc", " \000\000"]
|
3166
|
-
# "abc \0abc \0".unpack('Z*Z*') #=> ["abc ", "abc "]
|
3167
|
-
# "aa".unpack('b8B8') #=> ["10000110", "01100001"]
|
3168
|
-
# "aaa".unpack('h2H2c') #=> ["16", "61", 97]
|
3169
|
-
# "\xfe\xff\xfe\xff".unpack('sS') #=> [-2, 65534]
|
3170
|
-
# "now=20is".unpack('M*') #=> ["now is"]
|
3171
|
-
# "whole".unpack('xax2aX2aX1aX2a') #=> ["h", "e", "l", "l", "o"]
|
3172
|
-
#
|
3173
|
-
# This table summarizes the various formats and the Ruby classes returned by
|
3174
|
-
# each.
|
3175
|
-
#
|
3176
|
-
# Integer | |
|
3177
|
-
# Directive | Returns | Meaning
|
3178
|
-
# ------------------------------------------------------------------
|
3179
|
-
# C | Integer | 8-bit unsigned (unsigned char)
|
3180
|
-
# S | Integer | 16-bit unsigned, native endian (uint16_t)
|
3181
|
-
# L | Integer | 32-bit unsigned, native endian (uint32_t)
|
3182
|
-
# Q | Integer | 64-bit unsigned, native endian (uint64_t)
|
3183
|
-
# J | Integer | pointer width unsigned, native endian (uintptr_t)
|
3184
|
-
# | |
|
3185
|
-
# c | Integer | 8-bit signed (signed char)
|
3186
|
-
# s | Integer | 16-bit signed, native endian (int16_t)
|
3187
|
-
# l | Integer | 32-bit signed, native endian (int32_t)
|
3188
|
-
# q | Integer | 64-bit signed, native endian (int64_t)
|
3189
|
-
# j | Integer | pointer width signed, native endian (intptr_t)
|
3190
|
-
# | |
|
3191
|
-
# S_ S! | Integer | unsigned short, native endian
|
3192
|
-
# I I_ I! | Integer | unsigned int, native endian
|
3193
|
-
# L_ L! | Integer | unsigned long, native endian
|
3194
|
-
# Q_ Q! | Integer | unsigned long long, native endian (ArgumentError
|
3195
|
-
# | | if the platform has no long long type.)
|
3196
|
-
# J! | Integer | uintptr_t, native endian (same with J)
|
3197
|
-
# | |
|
3198
|
-
# s_ s! | Integer | signed short, native endian
|
3199
|
-
# i i_ i! | Integer | signed int, native endian
|
3200
|
-
# l_ l! | Integer | signed long, native endian
|
3201
|
-
# q_ q! | Integer | signed long long, native endian (ArgumentError
|
3202
|
-
# | | if the platform has no long long type.)
|
3203
|
-
# j! | Integer | intptr_t, native endian (same with j)
|
3204
|
-
# | |
|
3205
|
-
# S> s> S!> s!> | Integer | same as the directives without ">" except
|
3206
|
-
# L> l> L!> l!> | | big endian
|
3207
|
-
# I!> i!> | |
|
3208
|
-
# Q> q> Q!> q!> | | "S>" is the same as "n"
|
3209
|
-
# J> j> J!> j!> | | "L>" is the same as "N"
|
3210
|
-
# | |
|
3211
|
-
# S< s< S!< s!< | Integer | same as the directives without "<" except
|
3212
|
-
# L< l< L!< l!< | | little endian
|
3213
|
-
# I!< i!< | |
|
3214
|
-
# Q< q< Q!< q!< | | "S<" is the same as "v"
|
3215
|
-
# J< j< J!< j!< | | "L<" is the same as "V"
|
3216
|
-
# | |
|
3217
|
-
# n | Integer | 16-bit unsigned, network (big-endian) byte order
|
3218
|
-
# N | Integer | 32-bit unsigned, network (big-endian) byte order
|
3219
|
-
# v | Integer | 16-bit unsigned, VAX (little-endian) byte order
|
3220
|
-
# V | Integer | 32-bit unsigned, VAX (little-endian) byte order
|
3221
|
-
# | |
|
3222
|
-
# U | Integer | UTF-8 character
|
3223
|
-
# w | Integer | BER-compressed integer (see Array#pack)
|
3224
|
-
#
|
3225
|
-
# Float | |
|
3226
|
-
# Directive | Returns | Meaning
|
3227
|
-
# -----------------------------------------------------------------
|
3228
|
-
# D d | Float | double-precision, native format
|
3229
|
-
# F f | Float | single-precision, native format
|
3230
|
-
# E | Float | double-precision, little-endian byte order
|
3231
|
-
# e | Float | single-precision, little-endian byte order
|
3232
|
-
# G | Float | double-precision, network (big-endian) byte order
|
3233
|
-
# g | Float | single-precision, network (big-endian) byte order
|
3234
|
-
#
|
3235
|
-
# String | |
|
3236
|
-
# Directive | Returns | Meaning
|
3237
|
-
# -----------------------------------------------------------------
|
3238
|
-
# A | String | arbitrary binary string (remove trailing nulls and ASCII spaces)
|
3239
|
-
# a | String | arbitrary binary string
|
3240
|
-
# Z | String | null-terminated string
|
3241
|
-
# B | String | bit string (MSB first)
|
3242
|
-
# b | String | bit string (LSB first)
|
3243
|
-
# H | String | hex string (high nibble first)
|
3244
|
-
# h | String | hex string (low nibble first)
|
3245
|
-
# u | String | UU-encoded string
|
3246
|
-
# M | String | quoted-printable, MIME encoding (see RFC2045)
|
3247
|
-
# m | String | base64 encoded string (RFC 2045) (default)
|
3248
|
-
# | | base64 encoded string (RFC 4648) if followed by 0
|
3249
|
-
# P | String | pointer to a structure (fixed-length string)
|
3250
|
-
# p | String | pointer to a null-terminated string
|
3251
|
-
#
|
3252
|
-
# Misc. | |
|
3253
|
-
# Directive | Returns | Meaning
|
3254
|
-
# -----------------------------------------------------------------
|
3255
|
-
# @ | --- | skip to the offset given by the length argument
|
3256
|
-
# X | --- | skip backward one byte
|
3257
|
-
# x | --- | skip forward one byte
|
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
|
-
#
|
3265
|
-
# HISTORY
|
3266
|
-
#
|
3267
|
-
# * J, J! j, and j! are available since Ruby 2.3.
|
3268
|
-
# * Q_, Q!, q_, and q! are available since Ruby 2.1.
|
3269
|
-
# * I!<, i!<, I!>, and i!> are available since Ruby 1.9.3.
|
3349
|
+
# - unpack(template, offset: 0) -> array
|
3350
|
+
# -->
|
3351
|
+
# Extracts data from `self`, forming objects that become the elements of a new
|
3352
|
+
# array; returns that array. See [Packed Data](rdoc-ref:packed_data.rdoc).
|
3270
3353
|
#
|
3271
3354
|
def unpack: (String format, ?offset: Integer) -> Array[Integer | Float | String | nil]
|
3272
3355
|
|
3273
3356
|
# <!--
|
3274
3357
|
# rdoc-file=pack.rb
|
3275
|
-
# -
|
3276
|
-
# - str.unpack1(format, offset: anInteger) -> obj
|
3358
|
+
# - unpack1(template, offset: 0) -> object
|
3277
3359
|
# -->
|
3278
|
-
#
|
3279
|
-
#
|
3280
|
-
#
|
3281
|
-
# See also String#unpack, Array#pack.
|
3282
|
-
#
|
3283
|
-
# Contrast with String#unpack:
|
3284
|
-
#
|
3285
|
-
# "abc \0\0abc \0\0".unpack('A6Z6') #=> ["abc", "abc "]
|
3286
|
-
# "abc \0\0abc \0\0".unpack1('A6Z6') #=> "abc"
|
3287
|
-
#
|
3288
|
-
# In that case data would be lost but often it's the case that the array only
|
3289
|
-
# holds one value, especially when unpacking binary data. For instance:
|
3290
|
-
#
|
3291
|
-
# "\xff\x00\x00\x00".unpack("l") #=> [255]
|
3292
|
-
# "\xff\x00\x00\x00".unpack1("l") #=> 255
|
3293
|
-
#
|
3294
|
-
# Thus unpack1 is convenient, makes clear the intention and signals the expected
|
3295
|
-
# return value to those reading the code.
|
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)
|
3360
|
+
# Like String#unpack, but unpacks and returns only the first extracted object.
|
3361
|
+
# See [Packed Data](rdoc-ref:packed_data.rdoc).
|
3302
3362
|
#
|
3303
3363
|
def unpack1: (String format) -> (Integer | Float | String | nil)
|
3304
3364
|
|
@@ -3312,7 +3372,7 @@ class String
|
|
3312
3372
|
# s.upcase # => "HELLO WORLD!"
|
3313
3373
|
#
|
3314
3374
|
# The casing may be affected by the given `options`; see [Case
|
3315
|
-
# Mapping](
|
3375
|
+
# Mapping](rdoc-ref:case_mapping.rdoc).
|
3316
3376
|
#
|
3317
3377
|
# Related: String#upcase!, String#downcase, String#downcase!.
|
3318
3378
|
#
|
@@ -3334,7 +3394,7 @@ class String
|
|
3334
3394
|
# s.upcase! # => nil
|
3335
3395
|
#
|
3336
3396
|
# The casing may be affected by the given `options`; see [Case
|
3337
|
-
# Mapping](
|
3397
|
+
# Mapping](rdoc-ref:case_mapping.rdoc).
|
3338
3398
|
#
|
3339
3399
|
# Related: String#upcase, String#downcase, String#downcase!.
|
3340
3400
|
#
|
@@ -3382,13 +3442,13 @@ class String
|
|
3382
3442
|
|
3383
3443
|
# <!--
|
3384
3444
|
# rdoc-file=string.c
|
3385
|
-
# -
|
3445
|
+
# - valid_encoding? -> true or false
|
3386
3446
|
# -->
|
3387
|
-
# Returns true
|
3447
|
+
# Returns `true` if `self` is encoded correctly, `false` otherwise:
|
3388
3448
|
#
|
3389
|
-
# "\xc2\xa1".force_encoding("UTF-8").valid_encoding?
|
3390
|
-
# "\xc2".force_encoding("UTF-8").valid_encoding?
|
3391
|
-
# "\x80".force_encoding("UTF-8").valid_encoding?
|
3449
|
+
# "\xc2\xa1".force_encoding("UTF-8").valid_encoding? # => true
|
3450
|
+
# "\xc2".force_encoding("UTF-8").valid_encoding? # => false
|
3451
|
+
# "\x80".force_encoding("UTF-8").valid_encoding? # => false
|
3392
3452
|
#
|
3393
3453
|
def valid_encoding?: () -> bool
|
3394
3454
|
|
@@ -3396,42 +3456,53 @@ class String
|
|
3396
3456
|
|
3397
3457
|
# <!--
|
3398
3458
|
# 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
|
3459
|
+
# - String.new(string = '', **opts) -> new_string
|
3402
3460
|
# -->
|
3403
3461
|
# Returns a new String that is a copy of `string`.
|
3404
3462
|
#
|
3405
3463
|
# With no arguments, returns the empty string with the Encoding `ASCII-8BIT`:
|
3464
|
+
#
|
3406
3465
|
# s = String.new
|
3407
3466
|
# s # => ""
|
3408
3467
|
# s.encoding # => #<Encoding:ASCII-8BIT>
|
3409
3468
|
#
|
3410
|
-
# With
|
3411
|
-
# same encoding
|
3412
|
-
#
|
3413
|
-
#
|
3414
|
-
#
|
3415
|
-
#
|
3416
|
-
#
|
3417
|
-
#
|
3418
|
-
# String
|
3419
|
-
#
|
3420
|
-
#
|
3421
|
-
#
|
3422
|
-
#
|
3423
|
-
#
|
3424
|
-
#
|
3425
|
-
#
|
3426
|
-
#
|
3427
|
-
#
|
3428
|
-
#
|
3429
|
-
#
|
3430
|
-
#
|
3431
|
-
#
|
3432
|
-
#
|
3433
|
-
# String.new(
|
3434
|
-
#
|
3469
|
+
# With optional argument `string` and no keyword arguments, returns a copy of
|
3470
|
+
# `string` with the same encoding:
|
3471
|
+
#
|
3472
|
+
# String.new('foo') # => "foo"
|
3473
|
+
# String.new('тест') # => "тест"
|
3474
|
+
# String.new('こんにちは') # => "こんにちは"
|
3475
|
+
#
|
3476
|
+
# (Unlike String.new, a [string
|
3477
|
+
# literal](rdoc-ref:syntax/literals.rdoc@String+Literals) like `''` or a [here
|
3478
|
+
# document literal](rdoc-ref:syntax/literals.rdoc@Here+Document+Literals) always
|
3479
|
+
# has [script encoding](rdoc-ref:encodings.rdoc@Script+Encoding).)
|
3480
|
+
#
|
3481
|
+
# With optional keyword argument `encoding`, returns a copy of `string` with the
|
3482
|
+
# specified encoding; the `encoding` may be an Encoding object, an encoding
|
3483
|
+
# name, or an encoding name alias:
|
3484
|
+
#
|
3485
|
+
# String.new('foo', encoding: Encoding::US_ASCII).encoding # => #<Encoding:US-ASCII>
|
3486
|
+
# String.new('foo', encoding: 'US-ASCII').encoding # => #<Encoding:US-ASCII>
|
3487
|
+
# String.new('foo', encoding: 'ASCII').encoding # => #<Encoding:US-ASCII>
|
3488
|
+
#
|
3489
|
+
# The given encoding need not be valid for the string's content, and that
|
3490
|
+
# validity is not checked:
|
3491
|
+
#
|
3492
|
+
# s = String.new('こんにちは', encoding: 'ascii')
|
3493
|
+
# s.valid_encoding? # => false
|
3494
|
+
#
|
3495
|
+
# But the given `encoding` itself is checked:
|
3496
|
+
#
|
3497
|
+
# String.new('foo', encoding: 'bar') # Raises ArgumentError.
|
3498
|
+
#
|
3499
|
+
# With optional keyword argument `capacity`, returns a copy of `string` (or an
|
3500
|
+
# empty string, if `string` is not given); the given `capacity` is advisory
|
3501
|
+
# only, and may or may not set the size of the internal buffer, which may in
|
3502
|
+
# turn affect performance:
|
3503
|
+
#
|
3504
|
+
# String.new(capacity: 1)
|
3505
|
+
# String.new('foo', capacity: 4096)
|
3435
3506
|
#
|
3436
3507
|
# The `string`, `encoding`, and `capacity` arguments may all be used together:
|
3437
3508
|
#
|