-
Notifications
You must be signed in to change notification settings - Fork 251
Expand file tree
/
Copy pathstring.rbs
More file actions
5533 lines (5394 loc) · 195 KB
/
Copy pathstring.rbs
File metadata and controls
5533 lines (5394 loc) · 195 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# <!-- rdoc-file=string.rb -->
# A `String` object has an arbitrary sequence of bytes, typically representing
# text or binary data. A `String` object may be created using String::new or as
# literals.
#
# String objects differ from Symbol objects in that Symbol objects are designed
# to be used as identifiers, instead of text or data.
#
# You can create a `String` object explicitly with:
#
# * A [string literal](rdoc-ref:syntax/literals.rdoc@String+Literals).
# * A [heredoc literal](rdoc-ref:syntax/literals.rdoc@Here+Document+Literals).
#
# You can convert certain objects to Strings with:
#
# * Method #String.
#
# Some `String` methods modify `self`. Typically, a method whose name ends with
# <code>!</code> modifies `self` and returns `self`; often, a similarly named
# method (without the <code>!</code>) returns a new string.
#
# In general, if both bang and non-bang versions of a method exist, the bang
# method mutates and the non-bang method does not. However, a method without a
# bang can also mutate, such as String#replace.
#
# ## Substitution Methods
#
# These methods perform substitutions:
#
# * String#sub: One substitution (or none); returns a new string.
# * String#sub!: One substitution (or none); returns `self` if any changes,
# `nil` otherwise.
# * String#gsub: Zero or more substitutions; returns a new string.
# * String#gsub!: Zero or more substitutions; returns `self` if any changes,
# `nil` otherwise.
#
# Each of these methods takes:
#
# * A first argument, `pattern` (String or Regexp), that specifies the
# substring(s) to be replaced.
#
# * Either of the following:
#
# * A second argument, `replacement` (String or Hash), that determines the
# replacing string.
# * A block that will determine the replacing string.
#
# The examples in this section mostly use the String#sub and String#gsub
# methods; the principles illustrated apply to all four substitution methods.
#
# <strong>Argument `pattern`</strong>
#
# Argument `pattern` is commonly a regular expression:
#
# s = 'hello'
# s.sub(/[aeiou]/, '*') # => "h*llo"
# s.gsub(/[aeiou]/, '*') # => "h*ll*"
# s.gsub(/[aeiou]/, '') # => "hll"
# s.sub(/ell/, 'al') # => "halo"
# s.gsub(/xyzzy/, '*') # => "hello"
# 'THX1138'.gsub(/\d+/, '00') # => "THX00"
#
# When `pattern` is a string, all its characters are treated as ordinary
# characters (not as Regexp special characters):
#
# 'THX1138'.gsub('\d+', '00') # => "THX1138"
#
# <strong>`String` `replacement`</strong>
#
# If `replacement` is a string, that string determines the replacing string that
# is substituted for the matched text.
#
# Each of the examples above uses a simple string as the replacing string.
#
# `String` `replacement` may contain back-references to the pattern's captures:
#
# * <code>\n</code> (*n* is a non-negative integer) refers to <code>$n</code>.
# * <code>\k<name></code> refers to the named capture `name`.
#
# See Regexp for details.
#
# Note that within the string `replacement`, a character combination such as
# <code>$&</code> is treated as ordinary text, not as a special match variable.
# However, you may refer to some special match variables using these
# combinations:
#
# * <code>\&</code> and <code>\0</code> correspond to <code>$&</code>, which
# contains the complete matched text.
# * <code>\'</code> corresponds to <code>$'</code>, which contains the string
# after the match.
# * <code>`</code> corresponds to <code>$`</code>, which contains the string
# before the match.
# * <code>\+</code> corresponds to <code>$+</code>, which contains the last
# capture group.
#
# See Regexp for details.
#
# Note that <code>\\</code> is interpreted as an escape, i.e., a single
# backslash.
#
# Note also that a string literal consumes backslashes. See [String
# Literals](rdoc-ref:syntax/literals.rdoc@String+Literals) for details about
# string literals.
#
# A back-reference is typically preceded by an additional backslash. For
# example, if you want to write a back-reference <code>\&</code> in
# `replacement` with a double-quoted string literal, you need to write
# <code>"..\\&.."</code>.
#
# If you want to write a non-back-reference string <code>\&</code> in
# `replacement`, you need to first escape the backslash to prevent this method
# from interpreting it as a back-reference, and then you need to escape the
# backslashes again to prevent a string literal from consuming them:
# <code>"..\\\\&.."</code>.
#
# You may want to use the block form to avoid excessive backslashes.
#
# <strong>Hash `replacement`</strong>
#
# If the argument `replacement` is a hash, and `pattern` matches one of its
# keys, the replacing string is the value for that key:
#
# h = {'foo' => 'bar', 'baz' => 'bat'}
# 'food'.sub('foo', h) # => "bard"
#
# Note that a symbol key does not match:
#
# h = {foo: 'bar', baz: 'bat'}
# 'food'.sub('foo', h) # => "d"
#
# **Block**
#
# In the block form, the current match string is passed to the block; the
# block's return value becomes the replacing string:
#
# s = '@'
# '1234'.gsub(/\d/) { |match| s.succ! } # => "ABCD"
#
# Special match variables such as <code>$1</code>, <code>$2</code>,
# <code>$`</code>, <code>$&</code>, and <code>$'</code> are set appropriately.
#
# ## Whitespace in Strings
#
# In the class `String`, *whitespace* is defined as a contiguous sequence of
# characters consisting of any mixture of the following:
#
# * NL (null): <code>"\x00"</code>, <code>"\u0000"</code>.
# * HT (horizontal tab): <code>"\x09"</code>, <code>"\t"</code>.
# * LF (line feed): <code>"\x0a"</code>, <code>"\n"</code>.
# * VT (vertical tab): <code>"\x0b"</code>, <code>"\v"</code>.
# * FF (form feed): <code>"\x0c"</code>, <code>"\f"</code>.
# * CR (carriage return): <code>"\x0d"</code>, <code>"\r"</code>.
# * SP (space): <code>"\x20"</code>, <code>" "</code>.
#
# Whitespace is relevant for the following methods:
#
# * #lstrip, #lstrip!: Strip leading whitespace.
# * #rstrip, #rstrip!: Strip trailing whitespace.
# * #strip, #strip!: Strip leading and trailing whitespace.
#
# ## What's Here
#
# First, what's elsewhere. Class `String`:
#
# * Inherits from the [Object class](rdoc-ref:Object@What-27s+Here).
# * Includes the [Comparable module](rdoc-ref:Comparable@What-27s+Here).
#
# Here, class `String` provides methods that are useful for:
#
# * [Creating a String](rdoc-ref:String@Creating+a+String).
# * [Freezing/Unfreezing a String](rdoc-ref:String@Freezing-2FUnfreezing).
# * [Querying a String](rdoc-ref:String@Querying).
# * [Comparing Strings](rdoc-ref:String@Comparing).
# * [Modifying a String](rdoc-ref:String@Modifying).
# * [Converting to a new String](rdoc-ref:String@Converting+to+New+String).
# * [Converting to a non-String](rdoc-ref:String@Converting+to+Non--5CString).
# * [Iterating over a String](rdoc-ref:String@Iterating).
#
# ### Creating a String
#
# * ::new: Returns a new string.
# * ::try_convert: Returns a new string created from a given object.
#
# ### Freezing/Unfreezing
#
# * #+@: Returns a string that is not frozen: `self` if not frozen;
# <code>self.dup</code> otherwise.
# * #-@ (aliased as #dedup): Returns a string that is frozen: `self` if
# already frozen; <code>self.freeze</code> otherwise.
# * #freeze: Freezes `self` if not already frozen; returns `self`.
#
# ### Querying
#
# *Counts*
#
# * #bytesize: Returns the count of bytes.
# * #count: Returns the count of substrings matching given strings.
# * #empty?: Returns whether the length of `self` is zero.
# * #length (aliased as #size): Returns the count of characters (not bytes).
#
# *Substrings*
#
# * #=~: Returns the index of the first substring that matches a given Regexp
# or other object; returns `nil` if no match is found.
# * #byteindex: Returns the byte index of the first occurrence of a given
# substring.
# * #byterindex: Returns the byte index of the last occurrence of a given
# substring.
# * #index: Returns the index of the *first* occurrence of a given substring;
# returns `nil` if none found.
# * #rindex: Returns the index of the *last* occurrence of a given substring;
# returns `nil` if none found.
# * #include?: Returns `true` if the string contains a given substring;
# `false` otherwise.
# * #match: Returns a MatchData object if the string matches a given Regexp;
# `nil` otherwise.
# * #match?: Returns `true` if the string matches a given Regexp; `false`
# otherwise.
# * #start_with?: Returns `true` if the string begins with any of the given
# substrings.
# * #end_with?: Returns `true` if the string ends with any of the given
# substrings.
#
# *Encodings*
#
# * #encoding: Returns the Encoding object that represents the encoding of the
# string.
# * #unicode_normalized?: Returns `true` if the string is in Unicode
# normalized form; `false` otherwise.
# * #valid_encoding?: Returns `true` if the string contains only characters
# that are valid for its encoding.
# * #ascii_only?: Returns `true` if the string has only ASCII characters;
# `false` otherwise.
#
# *Other*
#
# * #sum: Returns a basic checksum for the string: the sum of each byte.
# * #hash: Returns the integer hash code.
#
# ### Comparing
#
# * #== (aliased as #===): Returns `true` if a given other string has the same
# content as `self`.
# * #eql?: Returns `true` if the content is the same as the given other
# string.
# * #<=>: Returns -1, 0, or 1 as a given other string is smaller than, equal
# to, or larger than `self`.
# * #casecmp: Ignoring case, returns -1, 0, or 1 as `self` is smaller than,
# equal to, or larger than a given other string.
# * #casecmp?: Ignoring case, returns whether a given other string is equal to
# `self`.
#
# ### Modifying
#
# Each of these methods modifies `self`.
#
# *Insertion*
#
# * #insert: Returns `self` with a given string inserted at a specified
# offset.
# * #<<: Returns `self` concatenated with a given string or integer.
# * #append_as_bytes: Returns `self` concatenated with strings without
# performing any encoding validation or conversion.
# * #prepend: Prefixes to `self` the concatenation of given other strings.
#
# *Substitution*
#
# * #bytesplice: Replaces bytes of `self` with bytes from a given string;
# returns `self`.
# * #sub!: Replaces the first substring that matches a given pattern with a
# given replacement string; returns `self` if any changes, `nil` otherwise.
# * #gsub!: Replaces each substring that matches a given pattern with a given
# replacement string; returns `self` if any changes, `nil` otherwise.
# * #succ! (aliased as #next!): Returns `self` modified to become its own
# successor.
# * #replace: Returns `self` with its entire content replaced by a given
# string.
# * #reverse!: Returns `self` with its characters in reverse order.
# * #setbyte: Sets the byte at a given integer offset to a given value;
# returns the argument.
# * #tr!: Replaces specified characters in `self` with specified replacement
# characters; returns `self` if any changes, `nil` otherwise.
# * #tr_s!: Replaces specified characters in `self` with specified replacement
# characters, removing duplicates from the substrings that were modified;
# returns `self` if any changes, `nil` otherwise.
#
# *Casing*
#
# * #capitalize!: Upcases the initial character and downcases all others;
# returns `self` if any changes, `nil` otherwise.
# * #downcase!: Downcases all characters; returns `self` if any changes, `nil`
# otherwise.
# * #upcase!: Upcases all characters; returns `self` if any changes, `nil`
# otherwise.
# * #swapcase!: Upcases each downcase character and downcases each upcase
# character; returns `self` if any changes, `nil` otherwise.
#
# *Encoding*
#
# * #encode!: Returns `self` with all characters transcoded from one encoding
# to another.
# * #unicode_normalize!: Unicode-normalizes `self`; returns `self`.
# * #scrub!: Replaces each invalid byte with a given character; returns
# `self`.
# * #force_encoding: Changes the encoding to a given encoding; returns `self`.
#
# *Deletion*
#
# * #clear: Removes all content, so that `self` is empty; returns `self`.
# * #slice!, #[]=: Removes a substring determined by a given index,
# start/length, range, regexp, or substring.
# * #squeeze!: Removes contiguous duplicate characters; returns `self`.
# * #delete!: Removes characters as determined by the intersection of
# substring arguments.
# * #delete_prefix!: Removes leading prefix; returns `self` if any changes,
# `nil` otherwise.
# * #delete_suffix!: Removes trailing suffix; returns `self` if any changes,
# `nil` otherwise.
# * #lstrip!: Removes leading whitespace; returns `self` if any changes, `nil`
# otherwise.
# * #rstrip!: Removes trailing whitespace; returns `self` if any changes,
# `nil` otherwise.
# * #strip!: Removes leading and trailing whitespace; returns `self` if any
# changes, `nil` otherwise.
# * #chomp!: Removes the trailing record separator, if found; returns `self`
# if any changes, `nil` otherwise.
# * #chop!: Removes trailing newline characters if found; otherwise removes
# the last character; returns `self` if any changes, `nil` otherwise.
#
# ### Converting to New String
#
# Each of these methods returns a new `String` based on `self`, often just a
# modified copy of `self`.
#
# *Extension*
#
# * #*: Returns the concatenation of multiple copies of `self`.
# * #+: Returns the concatenation of `self` and a given other string.
# * #center: Returns a copy of `self`, centered by specified padding.
# * #concat: Returns the concatenation of `self` with given other strings.
# * #ljust: Returns a copy of `self` of a given length, right-padded with a
# given other string.
# * #rjust: Returns a copy of `self` of a given length, left-padded with a
# given other string.
#
# *Encoding*
#
# * #b: Returns a copy of `self` with ASCII-8BIT encoding.
# * #scrub: Returns a copy of `self` with each invalid byte replaced with a
# given character.
# * #unicode_normalize: Returns a copy of `self` with each character
# Unicode-normalized.
# * #encode: Returns a copy of `self` with all characters transcoded from one
# encoding to another.
#
# *Substitution*
#
# * #dump: Returns a printable version of `self`, enclosed in double-quotes.
# * #undump: Inverse of #dump; returns a copy of `self` with changes of the
# kinds made by #dump "undone."
# * #sub: Returns a copy of `self` with the first substring matching a given
# pattern replaced with a given replacement string.
# * #gsub: Returns a copy of `self` with each substring that matches a given
# pattern replaced with a given replacement string.
# * #succ (aliased as #next): Returns the string that is the successor to
# `self`.
# * #reverse: Returns a copy of `self` with its characters in reverse order.
# * #tr: Returns a copy of `self` with specified characters replaced with
# specified replacement characters.
# * #tr_s: Returns a copy of `self` with specified characters replaced with
# specified replacement characters, removing duplicates from the substrings
# that were modified.
# * #%: Returns the string resulting from formatting a given object into
# `self`.
#
# *Casing*
#
# * #capitalize: Returns a copy of `self` with the first character upcased and
# all other characters downcased.
# * #downcase: Returns a copy of `self` with all characters downcased.
# * #upcase: Returns a copy of `self` with all characters upcased.
# * #swapcase: Returns a copy of `self` with all upcase characters downcased
# and all downcase characters upcased.
#
# *Deletion*
#
# * #delete: Returns a copy of `self` with characters removed.
# * #delete_prefix: Returns a copy of `self` with a given prefix removed.
# * #delete_suffix: Returns a copy of `self` with a given suffix removed.
# * #lstrip: Returns a copy of `self` with leading whitespace removed.
# * #rstrip: Returns a copy of `self` with trailing whitespace removed.
# * #strip: Returns a copy of `self` with leading and trailing whitespace
# removed.
# * #chomp: Returns a copy of `self` with a trailing record separator removed,
# if found.
# * #chop: Returns a copy of `self` with trailing newline characters or the
# last character removed.
# * #squeeze: Returns a copy of `self` with contiguous duplicate characters
# removed.
# * #[] (aliased as #slice): Returns a substring determined by a given index,
# start/length, range, regexp, or string.
# * #byteslice: Returns a substring determined by a given index, start/length,
# or range.
# * #chr: Returns the first character.
#
# *Duplication*
#
# * #to_s (aliased as #to_str): If `self` is a subclass of `String`, returns
# `self` copied into a `String`; otherwise, returns `self`.
#
# ### Converting to Non-String
#
# Each of these methods converts the contents of `self` to a non-`String`.
#
# <em>Characters, Bytes, and Clusters</em>
#
# * #bytes: Returns an array of the bytes in `self`.
# * #chars: Returns an array of the characters in `self`.
# * #codepoints: Returns an array of the integer ordinals in `self`.
# * #getbyte: Returns the integer byte at the given index in `self`.
# * #grapheme_clusters: Returns an array of the grapheme clusters in `self`.
#
# *Splitting*
#
# * #lines: Returns an array of the lines in `self`, as determined by a given
# record separator.
# * #partition: Returns a 3-element array determined by the first substring
# that matches a given substring or regexp.
# * #rpartition: Returns a 3-element array determined by the last substring
# that matches a given substring or regexp.
# * #split: Returns an array of substrings determined by a given delimiter --
# regexp or string -- or, if a block is given, passes those substrings to
# the block.
#
# *Matching*
#
# * #scan: Returns an array of substrings matching a given regexp or string,
# or, if a block is given, passes each matching substring to the block.
# * #unpack: Returns an array of substrings extracted from `self` according to
# a given format.
# * #unpack1: Returns the first substring extracted from `self` according to a
# given format.
#
# *Numerics*
#
# * #hex: Returns the integer value of the leading characters, interpreted as
# hexadecimal digits.
# * #oct: Returns the integer value of the leading characters, interpreted as
# octal digits.
# * #ord: Returns the integer ordinal of the first character in `self`.
# * #to_c: Returns the complex value of leading characters, interpreted as a
# complex number.
# * #to_i: Returns the integer value of leading characters, interpreted as an
# integer.
# * #to_f: Returns the floating-point value of leading characters, interpreted
# as a floating-point number.
# * #to_r: Returns the rational value of leading characters, interpreted as a
# rational.
#
# *Strings and Symbols*
#
# * #inspect: Returns a copy of `self`, enclosed in double quotes, with
# special characters escaped.
# * #intern (aliased as #to_sym): Returns the symbol corresponding to `self`.
#
# ### Iterating
#
# * #each_byte: Calls the given block with each successive byte in `self`.
# * #each_char: Calls the given block with each successive character in
# `self`.
# * #each_codepoint: Calls the given block with each successive integer
# codepoint in `self`.
# * #each_grapheme_cluster: Calls the given block with each successive
# grapheme cluster in `self`.
# * #each_line: Calls the given block with each successive line in `self`, as
# determined by a given record separator.
# * #upto: Calls the given block with each string value returned by successive
# calls to #succ.
#
# <!-- rdoc-file=string.rb -->
# A `String` object has an arbitrary sequence of bytes, typically representing
# text or binary data. A `String` object may be created using String::new or as
# literals.
#
# String objects differ from Symbol objects in that Symbol objects are designed
# to be used as identifiers, instead of text or data.
#
# You can create a `String` object explicitly with:
#
# * A [string literal](rdoc-ref:syntax/literals.rdoc@String+Literals).
# * A [heredoc literal](rdoc-ref:syntax/literals.rdoc@Here+Document+Literals).
#
# You can convert certain objects to Strings with:
#
# * Method #String.
#
# Some `String` methods modify `self`. Typically, a method whose name ends with
# <code>!</code> modifies `self` and returns `self`; often, a similarly named
# method (without the <code>!</code>) returns a new string.
#
# In general, if both bang and non-bang versions of a method exist, the bang
# method mutates and the non-bang method does not. However, a method without a
# bang can also mutate, such as String#replace.
#
# ## Substitution Methods
#
# These methods perform substitutions:
#
# * String#sub: One substitution (or none); returns a new string.
# * String#sub!: One substitution (or none); returns `self` if any changes,
# `nil` otherwise.
# * String#gsub: Zero or more substitutions; returns a new string.
# * String#gsub!: Zero or more substitutions; returns `self` if any changes,
# `nil` otherwise.
#
# Each of these methods takes:
#
# * A first argument, `pattern` (String or Regexp), that specifies the
# substring(s) to be replaced.
#
# * Either of the following:
#
# * A second argument, `replacement` (String or Hash), that determines the
# replacing string.
# * A block that will determine the replacing string.
#
# The examples in this section mostly use the String#sub and String#gsub
# methods; the principles illustrated apply to all four substitution methods.
#
# <strong>Argument `pattern`</strong>
#
# Argument `pattern` is commonly a regular expression:
#
# s = 'hello'
# s.sub(/[aeiou]/, '*') # => "h*llo"
# s.gsub(/[aeiou]/, '*') # => "h*ll*"
# s.gsub(/[aeiou]/, '') # => "hll"
# s.sub(/ell/, 'al') # => "halo"
# s.gsub(/xyzzy/, '*') # => "hello"
# 'THX1138'.gsub(/\d+/, '00') # => "THX00"
#
# When `pattern` is a string, all its characters are treated as ordinary
# characters (not as Regexp special characters):
#
# 'THX1138'.gsub('\d+', '00') # => "THX1138"
#
# <strong>`String` `replacement`</strong>
#
# If `replacement` is a string, that string determines the replacing string that
# is substituted for the matched text.
#
# Each of the examples above uses a simple string as the replacing string.
#
# `String` `replacement` may contain back-references to the pattern's captures:
#
# * <code>\n</code> (*n* is a non-negative integer) refers to <code>$n</code>.
# * <code>\k<name></code> refers to the named capture `name`.
#
# See Regexp for details.
#
# Note that within the string `replacement`, a character combination such as
# <code>$&</code> is treated as ordinary text, not as a special match variable.
# However, you may refer to some special match variables using these
# combinations:
#
# * <code>\&</code> and <code>\0</code> correspond to <code>$&</code>, which
# contains the complete matched text.
# * <code>\'</code> corresponds to <code>$'</code>, which contains the string
# after the match.
# * <code>`</code> corresponds to <code>$`</code>, which contains the string
# before the match.
# * <code>\+</code> corresponds to <code>$+</code>, which contains the last
# capture group.
#
# See Regexp for details.
#
# Note that <code>\\</code> is interpreted as an escape, i.e., a single
# backslash.
#
# Note also that a string literal consumes backslashes. See [String
# Literals](rdoc-ref:syntax/literals.rdoc@String+Literals) for details about
# string literals.
#
# A back-reference is typically preceded by an additional backslash. For
# example, if you want to write a back-reference <code>\&</code> in
# `replacement` with a double-quoted string literal, you need to write
# <code>"..\\&.."</code>.
#
# If you want to write a non-back-reference string <code>\&</code> in
# `replacement`, you need to first escape the backslash to prevent this method
# from interpreting it as a back-reference, and then you need to escape the
# backslashes again to prevent a string literal from consuming them:
# <code>"..\\\\&.."</code>.
#
# You may want to use the block form to avoid excessive backslashes.
#
# <strong>Hash `replacement`</strong>
#
# If the argument `replacement` is a hash, and `pattern` matches one of its
# keys, the replacing string is the value for that key:
#
# h = {'foo' => 'bar', 'baz' => 'bat'}
# 'food'.sub('foo', h) # => "bard"
#
# Note that a symbol key does not match:
#
# h = {foo: 'bar', baz: 'bat'}
# 'food'.sub('foo', h) # => "d"
#
# **Block**
#
# In the block form, the current match string is passed to the block; the
# block's return value becomes the replacing string:
#
# s = '@'
# '1234'.gsub(/\d/) { |match| s.succ! } # => "ABCD"
#
# Special match variables such as <code>$1</code>, <code>$2</code>,
# <code>$`</code>, <code>$&</code>, and <code>$'</code> are set appropriately.
#
# ## Whitespace in Strings
#
# In the class `String`, *whitespace* is defined as a contiguous sequence of
# characters consisting of any mixture of the following:
#
# * NL (null): <code>"\x00"</code>, <code>"\u0000"</code>.
# * HT (horizontal tab): <code>"\x09"</code>, <code>"\t"</code>.
# * LF (line feed): <code>"\x0a"</code>, <code>"\n"</code>.
# * VT (vertical tab): <code>"\x0b"</code>, <code>"\v"</code>.
# * FF (form feed): <code>"\x0c"</code>, <code>"\f"</code>.
# * CR (carriage return): <code>"\x0d"</code>, <code>"\r"</code>.
# * SP (space): <code>"\x20"</code>, <code>" "</code>.
#
# Whitespace is relevant for the following methods:
#
# * #lstrip, #lstrip!: Strip leading whitespace.
# * #rstrip, #rstrip!: Strip trailing whitespace.
# * #strip, #strip!: Strip leading and trailing whitespace.
#
# ## What's Here
#
# First, what's elsewhere. Class `String`:
#
# * Inherits from the [Object class](rdoc-ref:Object@What-27s+Here).
# * Includes the [Comparable module](rdoc-ref:Comparable@What-27s+Here).
#
# Here, class `String` provides methods that are useful for:
#
# * [Creating a String](rdoc-ref:String@Creating+a+String).
# * [Freezing/Unfreezing a String](rdoc-ref:String@Freezing-2FUnfreezing).
# * [Querying a String](rdoc-ref:String@Querying).
# * [Comparing Strings](rdoc-ref:String@Comparing).
# * [Modifying a String](rdoc-ref:String@Modifying).
# * [Converting to a new String](rdoc-ref:String@Converting+to+New+String).
# * [Converting to a non-String](rdoc-ref:String@Converting+to+Non--5CString).
# * [Iterating over a String](rdoc-ref:String@Iterating).
#
# ### Creating a String
#
# * ::new: Returns a new string.
# * ::try_convert: Returns a new string created from a given object.
#
# ### Freezing/Unfreezing
#
# * #+@: Returns a string that is not frozen: `self` if not frozen;
# <code>self.dup</code> otherwise.
# * #-@ (aliased as #dedup): Returns a string that is frozen: `self` if
# already frozen; <code>self.freeze</code> otherwise.
# * #freeze: Freezes `self` if not already frozen; returns `self`.
#
# ### Querying
#
# *Counts*
#
# * #bytesize: Returns the count of bytes.
# * #count: Returns the count of substrings matching given strings.
# * #empty?: Returns whether the length of `self` is zero.
# * #length (aliased as #size): Returns the count of characters (not bytes).
#
# *Substrings*
#
# * #=~: Returns the index of the first substring that matches a given Regexp
# or other object; returns `nil` if no match is found.
# * #byteindex: Returns the byte index of the first occurrence of a given
# substring.
# * #byterindex: Returns the byte index of the last occurrence of a given
# substring.
# * #index: Returns the index of the *first* occurrence of a given substring;
# returns `nil` if none found.
# * #rindex: Returns the index of the *last* occurrence of a given substring;
# returns `nil` if none found.
# * #include?: Returns `true` if the string contains a given substring;
# `false` otherwise.
# * #match: Returns a MatchData object if the string matches a given Regexp;
# `nil` otherwise.
# * #match?: Returns `true` if the string matches a given Regexp; `false`
# otherwise.
# * #start_with?: Returns `true` if the string begins with any of the given
# substrings.
# * #end_with?: Returns `true` if the string ends with any of the given
# substrings.
#
# *Encodings*
#
# * #encoding: Returns the Encoding object that represents the encoding of the
# string.
# * #unicode_normalized?: Returns `true` if the string is in Unicode
# normalized form; `false` otherwise.
# * #valid_encoding?: Returns `true` if the string contains only characters
# that are valid for its encoding.
# * #ascii_only?: Returns `true` if the string has only ASCII characters;
# `false` otherwise.
#
# *Other*
#
# * #sum: Returns a basic checksum for the string: the sum of each byte.
# * #hash: Returns the integer hash code.
#
# ### Comparing
#
# * #== (aliased as #===): Returns `true` if a given other string has the same
# content as `self`.
# * #eql?: Returns `true` if the content is the same as the given other
# string.
# * #<=>: Returns -1, 0, or 1 as a given other string is smaller than, equal
# to, or larger than `self`.
# * #casecmp: Ignoring case, returns -1, 0, or 1 as `self` is smaller than,
# equal to, or larger than a given other string.
# * #casecmp?: Ignoring case, returns whether a given other string is equal to
# `self`.
#
# ### Modifying
#
# Each of these methods modifies `self`.
#
# *Insertion*
#
# * #insert: Returns `self` with a given string inserted at a specified
# offset.
# * #<<: Returns `self` concatenated with a given string or integer.
# * #append_as_bytes: Returns `self` concatenated with strings without
# performing any encoding validation or conversion.
# * #prepend: Prefixes to `self` the concatenation of given other strings.
#
# *Substitution*
#
# * #bytesplice: Replaces bytes of `self` with bytes from a given string;
# returns `self`.
# * #sub!: Replaces the first substring that matches a given pattern with a
# given replacement string; returns `self` if any changes, `nil` otherwise.
# * #gsub!: Replaces each substring that matches a given pattern with a given
# replacement string; returns `self` if any changes, `nil` otherwise.
# * #succ! (aliased as #next!): Returns `self` modified to become its own
# successor.
# * #replace: Returns `self` with its entire content replaced by a given
# string.
# * #reverse!: Returns `self` with its characters in reverse order.
# * #setbyte: Sets the byte at a given integer offset to a given value;
# returns the argument.
# * #tr!: Replaces specified characters in `self` with specified replacement
# characters; returns `self` if any changes, `nil` otherwise.
# * #tr_s!: Replaces specified characters in `self` with specified replacement
# characters, removing duplicates from the substrings that were modified;
# returns `self` if any changes, `nil` otherwise.
#
# *Casing*
#
# * #capitalize!: Upcases the initial character and downcases all others;
# returns `self` if any changes, `nil` otherwise.
# * #downcase!: Downcases all characters; returns `self` if any changes, `nil`
# otherwise.
# * #upcase!: Upcases all characters; returns `self` if any changes, `nil`
# otherwise.
# * #swapcase!: Upcases each downcase character and downcases each upcase
# character; returns `self` if any changes, `nil` otherwise.
#
# *Encoding*
#
# * #encode!: Returns `self` with all characters transcoded from one encoding
# to another.
# * #unicode_normalize!: Unicode-normalizes `self`; returns `self`.
# * #scrub!: Replaces each invalid byte with a given character; returns
# `self`.
# * #force_encoding: Changes the encoding to a given encoding; returns `self`.
#
# *Deletion*
#
# * #clear: Removes all content, so that `self` is empty; returns `self`.
# * #slice!, #[]=: Removes a substring determined by a given index,
# start/length, range, regexp, or substring.
# * #squeeze!: Removes contiguous duplicate characters; returns `self`.
# * #delete!: Removes characters as determined by the intersection of
# substring arguments.
# * #delete_prefix!: Removes leading prefix; returns `self` if any changes,
# `nil` otherwise.
# * #delete_suffix!: Removes trailing suffix; returns `self` if any changes,
# `nil` otherwise.
# * #lstrip!: Removes leading whitespace; returns `self` if any changes, `nil`
# otherwise.
# * #rstrip!: Removes trailing whitespace; returns `self` if any changes,
# `nil` otherwise.
# * #strip!: Removes leading and trailing whitespace; returns `self` if any
# changes, `nil` otherwise.
# * #chomp!: Removes the trailing record separator, if found; returns `self`
# if any changes, `nil` otherwise.
# * #chop!: Removes trailing newline characters if found; otherwise removes
# the last character; returns `self` if any changes, `nil` otherwise.
#
# ### Converting to New String
#
# Each of these methods returns a new `String` based on `self`, often just a
# modified copy of `self`.
#
# *Extension*
#
# * #*: Returns the concatenation of multiple copies of `self`.
# * #+: Returns the concatenation of `self` and a given other string.
# * #center: Returns a copy of `self`, centered by specified padding.
# * #concat: Returns the concatenation of `self` with given other strings.
# * #ljust: Returns a copy of `self` of a given length, right-padded with a
# given other string.
# * #rjust: Returns a copy of `self` of a given length, left-padded with a
# given other string.
#
# *Encoding*
#
# * #b: Returns a copy of `self` with ASCII-8BIT encoding.
# * #scrub: Returns a copy of `self` with each invalid byte replaced with a
# given character.
# * #unicode_normalize: Returns a copy of `self` with each character
# Unicode-normalized.
# * #encode: Returns a copy of `self` with all characters transcoded from one
# encoding to another.
#
# *Substitution*
#
# * #dump: Returns a printable version of `self`, enclosed in double-quotes.
# * #undump: Inverse of #dump; returns a copy of `self` with changes of the
# kinds made by #dump "undone."
# * #sub: Returns a copy of `self` with the first substring matching a given
# pattern replaced with a given replacement string.
# * #gsub: Returns a copy of `self` with each substring that matches a given
# pattern replaced with a given replacement string.
# * #succ (aliased as #next): Returns the string that is the successor to
# `self`.
# * #reverse: Returns a copy of `self` with its characters in reverse order.
# * #tr: Returns a copy of `self` with specified characters replaced with
# specified replacement characters.
# * #tr_s: Returns a copy of `self` with specified characters replaced with
# specified replacement characters, removing duplicates from the substrings
# that were modified.
# * #%: Returns the string resulting from formatting a given object into
# `self`.
#
# *Casing*
#
# * #capitalize: Returns a copy of `self` with the first character upcased and
# all other characters downcased.
# * #downcase: Returns a copy of `self` with all characters downcased.
# * #upcase: Returns a copy of `self` with all characters upcased.
# * #swapcase: Returns a copy of `self` with all upcase characters downcased
# and all downcase characters upcased.
#
# *Deletion*
#
# * #delete: Returns a copy of `self` with characters removed.
# * #delete_prefix: Returns a copy of `self` with a given prefix removed.
# * #delete_suffix: Returns a copy of `self` with a given suffix removed.
# * #lstrip: Returns a copy of `self` with leading whitespace removed.
# * #rstrip: Returns a copy of `self` with trailing whitespace removed.
# * #strip: Returns a copy of `self` with leading and trailing whitespace
# removed.
# * #chomp: Returns a copy of `self` with a trailing record separator removed,
# if found.
# * #chop: Returns a copy of `self` with trailing newline characters or the
# last character removed.
# * #squeeze: Returns a copy of `self` with contiguous duplicate characters
# removed.
# * #[] (aliased as #slice): Returns a substring determined by a given index,
# start/length, range, regexp, or string.
# * #byteslice: Returns a substring determined by a given index, start/length,
# or range.
# * #chr: Returns the first character.
#
# *Duplication*
#
# * #to_s (aliased as #to_str): If `self` is a subclass of `String`, returns
# `self` copied into a `String`; otherwise, returns `self`.
#
# ### Converting to Non-String
#
# Each of these methods converts the contents of `self` to a non-`String`.
#
# <em>Characters, Bytes, and Clusters</em>
#
# * #bytes: Returns an array of the bytes in `self`.
# * #chars: Returns an array of the characters in `self`.
# * #codepoints: Returns an array of the integer ordinals in `self`.
# * #getbyte: Returns the integer byte at the given index in `self`.
# * #grapheme_clusters: Returns an array of the grapheme clusters in `self`.
#
# *Splitting*
#
# * #lines: Returns an array of the lines in `self`, as determined by a given
# record separator.
# * #partition: Returns a 3-element array determined by the first substring
# that matches a given substring or regexp.
# * #rpartition: Returns a 3-element array determined by the last substring
# that matches a given substring or regexp.
# * #split: Returns an array of substrings determined by a given delimiter --
# regexp or string -- or, if a block is given, passes those substrings to
# the block.
#
# *Matching*
#
# * #scan: Returns an array of substrings matching a given regexp or string,
# or, if a block is given, passes each matching substring to the block.
# * #unpack: Returns an array of substrings extracted from `self` according to
# a given format.
# * #unpack1: Returns the first substring extracted from `self` according to a
# given format.
#
# *Numerics*
#
# * #hex: Returns the integer value of the leading characters, interpreted as
# hexadecimal digits.
# * #oct: Returns the integer value of the leading characters, interpreted as
# octal digits.
# * #ord: Returns the integer ordinal of the first character in `self`.
# * #to_c: Returns the complex value of leading characters, interpreted as a
# complex number.
# * #to_i: Returns the integer value of leading characters, interpreted as an
# integer.
# * #to_f: Returns the floating-point value of leading characters, interpreted
# as a floating-point number.
# * #to_r: Returns the rational value of leading characters, interpreted as a
# rational.
#
# *Strings and Symbols*
#
# * #inspect: Returns a copy of `self`, enclosed in double quotes, with
# special characters escaped.
# * #intern (aliased as #to_sym): Returns the symbol corresponding to `self`.
#
# ### Iterating
#
# * #each_byte: Calls the given block with each successive byte in `self`.
# * #each_char: Calls the given block with each successive character in
# `self`.
# * #each_codepoint: Calls the given block with each successive integer
# codepoint in `self`.
# * #each_grapheme_cluster: Calls the given block with each successive
# grapheme cluster in `self`.
# * #each_line: Calls the given block with each successive line in `self`, as
# determined by a given record separator.
# * #upto: Calls the given block with each string value returned by successive
# calls to #succ.
#
class String
include Comparable
# A `selector` is a special type of string, used within methods like `String#tr`.
type selector = string
# <!--
# rdoc-file=string.c
# - String.try_convert(object) -> object, new_string, or nil
# -->
# Attempts to convert the given `object` to a string.
#
# If `object` is already a string, returns `object`, unmodified.
#
# Otherwise if `object` responds to <code>:to_str</code>, calls
# <code>object.to_str</code> and returns the result.
#
# Returns `nil` if `object` does not respond to <code>:to_str</code>.
#
# Raises an exception unless <code>object.to_str</code> returns a string.
#
def self.try_convert: (String object) -> String # technically will return `object` unchanged.
| (_ToStr object) -> String
| (untyped object) -> String?
# <!--
# rdoc-file=string.c
# - String.new(string = ''.encode(Encoding::ASCII_8BIT) , **options) -> new_string
# -->
# Returns a new String object containing the given `string`.
#
# The `options` are optional keyword options (see below).
#
# With no argument given and keyword `encoding` also not given, returns an empty
# string with the Encoding <code>ASCII-8BIT</code>:
#
# s = String.new # => ""
# s.encoding # => #<Encoding:ASCII-8BIT>
#
# With argument `string` given and keyword option `encoding` not given, returns
# a new string with the same encoding as `string`:
#