rbs 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.github/workflows/ruby.yml +28 -0
- data/.gitignore +12 -0
- data/.rubocop.yml +15 -0
- data/BSDL +22 -0
- data/CHANGELOG.md +9 -0
- data/COPYING +56 -0
- data/Gemfile +6 -0
- data/README.md +93 -0
- data/Rakefile +142 -0
- data/bin/annotate-with-rdoc +157 -0
- data/bin/console +14 -0
- data/bin/query-rdoc +103 -0
- data/bin/setup +10 -0
- data/bin/sort +89 -0
- data/bin/test_runner.rb +16 -0
- data/docs/CONTRIBUTING.md +97 -0
- data/docs/sigs.md +148 -0
- data/docs/stdlib.md +152 -0
- data/docs/syntax.md +528 -0
- data/exe/rbs +7 -0
- data/lib/rbs.rb +64 -0
- data/lib/rbs/ast/annotation.rb +27 -0
- data/lib/rbs/ast/comment.rb +27 -0
- data/lib/rbs/ast/declarations.rb +395 -0
- data/lib/rbs/ast/members.rb +362 -0
- data/lib/rbs/buffer.rb +50 -0
- data/lib/rbs/builtin_names.rb +55 -0
- data/lib/rbs/cli.rb +558 -0
- data/lib/rbs/constant.rb +26 -0
- data/lib/rbs/constant_table.rb +150 -0
- data/lib/rbs/definition.rb +170 -0
- data/lib/rbs/definition_builder.rb +919 -0
- data/lib/rbs/environment.rb +281 -0
- data/lib/rbs/environment_loader.rb +136 -0
- data/lib/rbs/environment_walker.rb +124 -0
- data/lib/rbs/errors.rb +187 -0
- data/lib/rbs/location.rb +102 -0
- data/lib/rbs/method_type.rb +123 -0
- data/lib/rbs/namespace.rb +91 -0
- data/lib/rbs/parser.y +1344 -0
- data/lib/rbs/prototype/rb.rb +553 -0
- data/lib/rbs/prototype/rbi.rb +587 -0
- data/lib/rbs/prototype/runtime.rb +381 -0
- data/lib/rbs/substitution.rb +46 -0
- data/lib/rbs/test.rb +26 -0
- data/lib/rbs/test/errors.rb +61 -0
- data/lib/rbs/test/hook.rb +294 -0
- data/lib/rbs/test/setup.rb +58 -0
- data/lib/rbs/test/spy.rb +325 -0
- data/lib/rbs/test/test_helper.rb +183 -0
- data/lib/rbs/test/type_check.rb +254 -0
- data/lib/rbs/type_name.rb +70 -0
- data/lib/rbs/types.rb +936 -0
- data/lib/rbs/variance_calculator.rb +138 -0
- data/lib/rbs/vendorer.rb +47 -0
- data/lib/rbs/version.rb +3 -0
- data/lib/rbs/writer.rb +269 -0
- data/lib/ruby/signature.rb +7 -0
- data/rbs.gemspec +46 -0
- data/stdlib/abbrev/abbrev.rbs +60 -0
- data/stdlib/base64/base64.rbs +71 -0
- data/stdlib/benchmark/benchmark.rbs +372 -0
- data/stdlib/builtin/array.rbs +1997 -0
- data/stdlib/builtin/basic_object.rbs +280 -0
- data/stdlib/builtin/binding.rbs +177 -0
- data/stdlib/builtin/builtin.rbs +45 -0
- data/stdlib/builtin/class.rbs +145 -0
- data/stdlib/builtin/comparable.rbs +116 -0
- data/stdlib/builtin/complex.rbs +400 -0
- data/stdlib/builtin/constants.rbs +37 -0
- data/stdlib/builtin/data.rbs +5 -0
- data/stdlib/builtin/deprecated.rbs +2 -0
- data/stdlib/builtin/dir.rbs +413 -0
- data/stdlib/builtin/encoding.rbs +607 -0
- data/stdlib/builtin/enumerable.rbs +404 -0
- data/stdlib/builtin/enumerator.rbs +260 -0
- data/stdlib/builtin/errno.rbs +781 -0
- data/stdlib/builtin/errors.rbs +582 -0
- data/stdlib/builtin/exception.rbs +194 -0
- data/stdlib/builtin/false_class.rbs +40 -0
- data/stdlib/builtin/fiber.rbs +68 -0
- data/stdlib/builtin/fiber_error.rbs +12 -0
- data/stdlib/builtin/file.rbs +1076 -0
- data/stdlib/builtin/file_test.rbs +59 -0
- data/stdlib/builtin/float.rbs +696 -0
- data/stdlib/builtin/gc.rbs +243 -0
- data/stdlib/builtin/hash.rbs +1029 -0
- data/stdlib/builtin/integer.rbs +707 -0
- data/stdlib/builtin/io.rbs +683 -0
- data/stdlib/builtin/kernel.rbs +576 -0
- data/stdlib/builtin/marshal.rbs +161 -0
- data/stdlib/builtin/match_data.rbs +271 -0
- data/stdlib/builtin/math.rbs +369 -0
- data/stdlib/builtin/method.rbs +185 -0
- data/stdlib/builtin/module.rbs +1104 -0
- data/stdlib/builtin/nil_class.rbs +82 -0
- data/stdlib/builtin/numeric.rbs +409 -0
- data/stdlib/builtin/object.rbs +824 -0
- data/stdlib/builtin/proc.rbs +429 -0
- data/stdlib/builtin/process.rbs +1227 -0
- data/stdlib/builtin/random.rbs +267 -0
- data/stdlib/builtin/range.rbs +226 -0
- data/stdlib/builtin/rational.rbs +424 -0
- data/stdlib/builtin/rb_config.rbs +57 -0
- data/stdlib/builtin/regexp.rbs +1083 -0
- data/stdlib/builtin/ruby_vm.rbs +14 -0
- data/stdlib/builtin/signal.rbs +55 -0
- data/stdlib/builtin/string.rbs +1901 -0
- data/stdlib/builtin/string_io.rbs +284 -0
- data/stdlib/builtin/struct.rbs +40 -0
- data/stdlib/builtin/symbol.rbs +228 -0
- data/stdlib/builtin/thread.rbs +1108 -0
- data/stdlib/builtin/thread_group.rbs +23 -0
- data/stdlib/builtin/time.rbs +1047 -0
- data/stdlib/builtin/trace_point.rbs +290 -0
- data/stdlib/builtin/true_class.rbs +46 -0
- data/stdlib/builtin/unbound_method.rbs +153 -0
- data/stdlib/builtin/warning.rbs +17 -0
- data/stdlib/coverage/coverage.rbs +62 -0
- data/stdlib/csv/csv.rbs +773 -0
- data/stdlib/erb/erb.rbs +392 -0
- data/stdlib/find/find.rbs +40 -0
- data/stdlib/ipaddr/ipaddr.rbs +247 -0
- data/stdlib/json/json.rbs +335 -0
- data/stdlib/pathname/pathname.rbs +1093 -0
- data/stdlib/prime/integer-extension.rbs +23 -0
- data/stdlib/prime/prime.rbs +188 -0
- data/stdlib/securerandom/securerandom.rbs +9 -0
- data/stdlib/set/set.rbs +301 -0
- data/stdlib/tmpdir/tmpdir.rbs +53 -0
- metadata +292 -0
@@ -0,0 +1,14 @@
|
|
1
|
+
# The [RubyVM](RubyVM) module provides some access to
|
2
|
+
# Ruby internals. This module is for very limited purposes, such as
|
3
|
+
# debugging, prototyping, and research. Normal users must not use it.
|
4
|
+
class RubyVM < Object
|
5
|
+
end
|
6
|
+
|
7
|
+
RubyVM::DEFAULT_PARAMS: Hash[Symbol, Integer]
|
8
|
+
|
9
|
+
RubyVM::INSTRUCTION_NAMES: Array[String]
|
10
|
+
|
11
|
+
RubyVM::OPTS: Array[String]
|
12
|
+
|
13
|
+
class RubyVM::InstructionSequence < Object
|
14
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# Many operating systems allow signals to be sent to running processes.
|
2
|
+
# Some signals have a defined effect on the process, while others may be
|
3
|
+
# trapped at the code level and acted upon. For example, your process may
|
4
|
+
# trap the USR1 signal and use it to toggle debugging, and may use TERM to
|
5
|
+
# initiate a controlled shutdown.
|
6
|
+
#
|
7
|
+
# ```ruby
|
8
|
+
# pid = fork do
|
9
|
+
# Signal.trap("USR1") do
|
10
|
+
# $debug = !$debug
|
11
|
+
# puts "Debug now: #$debug"
|
12
|
+
# end
|
13
|
+
# Signal.trap("TERM") do
|
14
|
+
# puts "Terminating..."
|
15
|
+
# shutdown()
|
16
|
+
# end
|
17
|
+
# # . . . do some work . . .
|
18
|
+
# end
|
19
|
+
#
|
20
|
+
# Process.detach(pid)
|
21
|
+
#
|
22
|
+
# # Controlling program:
|
23
|
+
# Process.kill("USR1", pid)
|
24
|
+
# # ...
|
25
|
+
# Process.kill("USR1", pid)
|
26
|
+
# # ...
|
27
|
+
# Process.kill("TERM", pid)
|
28
|
+
# ```
|
29
|
+
#
|
30
|
+
# produces:
|
31
|
+
#
|
32
|
+
# ```
|
33
|
+
# Debug now: true
|
34
|
+
# Debug now: false
|
35
|
+
# Terminating...
|
36
|
+
# ```
|
37
|
+
#
|
38
|
+
# The list of available signal names and their interpretation is system
|
39
|
+
# dependent. [Signal](Signal) delivery semantics may
|
40
|
+
# also vary between systems; in particular signal delivery may not always
|
41
|
+
# be reliable.
|
42
|
+
module Signal
|
43
|
+
# Returns a list of signal names mapped to the corresponding underlying
|
44
|
+
# signal numbers.
|
45
|
+
#
|
46
|
+
# ```ruby
|
47
|
+
# Signal.list #=> {"EXIT"=>0, "HUP"=>1, "INT"=>2, "QUIT"=>3, "ILL"=>4, "TRAP"=>5, "IOT"=>6, "ABRT"=>6, "FPE"=>8, "KILL"=>9, "BUS"=>7, "SEGV"=>11, "SYS"=>31, "PIPE"=>13, "ALRM"=>14, "TERM"=>15, "URG"=>23, "STOP"=>19, "TSTP"=>20, "CONT"=>18, "CHLD"=>17, "CLD"=>17, "TTIN"=>21, "TTOU"=>22, "IO"=>29, "XCPU"=>24, "XFSZ"=>25, "VTALRM"=>26, "PROF"=>27, "WINCH"=>28, "USR1"=>10, "USR2"=>12, "PWR"=>30, "POLL"=>29}
|
48
|
+
# ```
|
49
|
+
def self.list: () -> ::Hash[String, Integer]
|
50
|
+
|
51
|
+
def self.signame: (Integer arg0) -> String?
|
52
|
+
|
53
|
+
def self.trap: (Integer | String | Symbol signal, ?untyped command) -> (String | Proc)
|
54
|
+
| (Integer | String | Symbol signal) { (Integer arg0) -> untyped } -> (String | Proc)
|
55
|
+
end
|
@@ -0,0 +1,1901 @@
|
|
1
|
+
# A String object holds and manipulates an arbitrary sequence of bytes,
|
2
|
+
# typically representing characters. String objects may be created using
|
3
|
+
# String::new or as literals.
|
4
|
+
#
|
5
|
+
# Because of aliasing issues, users of strings should be aware of the methods
|
6
|
+
# that modify the contents of a String object. Typically, methods with names
|
7
|
+
# ending in ``!'' modify their receiver, while those without a ``!'' return a
|
8
|
+
# new String. However, there are exceptions, such as String#[]=.
|
9
|
+
#
|
10
|
+
class String
|
11
|
+
include Comparable
|
12
|
+
|
13
|
+
# Try to convert *obj* into a String, using to_str method. Returns converted
|
14
|
+
# string or nil if *obj* cannot be converted for any reason.
|
15
|
+
#
|
16
|
+
# String.try_convert("str") #=> "str"
|
17
|
+
# String.try_convert(/re/) #=> nil
|
18
|
+
#
|
19
|
+
def self.try_convert: (untyped obj) -> String?
|
20
|
+
|
21
|
+
public
|
22
|
+
|
23
|
+
# Format---Uses *str* as a format specification, and returns the result of
|
24
|
+
# applying it to *arg*. If the format specification contains more than one
|
25
|
+
# substitution, then *arg* must be an Array or Hash containing the values to be
|
26
|
+
# substituted. See Kernel#sprintf for details of the format string.
|
27
|
+
#
|
28
|
+
# "%05d" % 123 #=> "00123"
|
29
|
+
# "%-5s: %016x" % [ "ID", self.object_id ] #=> "ID : 00002b054ec93168"
|
30
|
+
# "foo = %{foo}" % { :foo => 'bar' } #=> "foo = bar"
|
31
|
+
#
|
32
|
+
def %: (Hash[Symbol, untyped]) -> String
|
33
|
+
| (Array[untyped]) -> String
|
34
|
+
| (untyped arg) -> String
|
35
|
+
|
36
|
+
# Copy --- Returns a new String containing `integer` copies of the receiver.
|
37
|
+
# `integer` must be greater than or equal to 0.
|
38
|
+
#
|
39
|
+
# "Ho! " * 3 #=> "Ho! Ho! Ho! "
|
40
|
+
# "Ho! " * 0 #=> ""
|
41
|
+
#
|
42
|
+
def *: (int n) -> String
|
43
|
+
|
44
|
+
# Concatenation---Returns a new String containing *other_str* concatenated to
|
45
|
+
# *str*.
|
46
|
+
#
|
47
|
+
# "Hello from " + self.to_s #=> "Hello from main"
|
48
|
+
#
|
49
|
+
def +: (string other_str) -> String
|
50
|
+
|
51
|
+
# If the string is frozen, then return duplicated mutable string.
|
52
|
+
#
|
53
|
+
# If the string is not frozen, then return the string itself.
|
54
|
+
#
|
55
|
+
def +@: () -> String
|
56
|
+
|
57
|
+
# Returns a frozen, possibly pre-existing copy of the string.
|
58
|
+
#
|
59
|
+
# The string will be deduplicated as long as it does not have any instance
|
60
|
+
# variables set on it.
|
61
|
+
#
|
62
|
+
def -@: () -> String
|
63
|
+
|
64
|
+
# Appends the given object to *str*. If the object is an Integer, it is
|
65
|
+
# considered a codepoint and converted to a character before being appended.
|
66
|
+
#
|
67
|
+
# a = "hello "
|
68
|
+
# a << "world" #=> "hello world"
|
69
|
+
# a << 33 #=> "hello world!"
|
70
|
+
#
|
71
|
+
# See also String#concat, which takes multiple arguments.
|
72
|
+
#
|
73
|
+
def <<: (string | Integer str_or_codepoint) -> String
|
74
|
+
|
75
|
+
# Comparison---Returns -1, 0, +1, or `nil` depending on whether `string` is less
|
76
|
+
# than, equal to, or greater than `other_string`.
|
77
|
+
#
|
78
|
+
# `nil` is returned if the two values are incomparable.
|
79
|
+
#
|
80
|
+
# If the strings are of different lengths, and the strings are equal when
|
81
|
+
# compared up to the shortest length, then the longer string is considered
|
82
|
+
# greater than the shorter one.
|
83
|
+
#
|
84
|
+
# `<=>` is the basis for the methods `<`, `<=`, `>`, `>=`, and `between?`,
|
85
|
+
# included from module Comparable. The method String#== does not use
|
86
|
+
# Comparable#==.
|
87
|
+
#
|
88
|
+
# "abcdef" <=> "abcde" #=> 1
|
89
|
+
# "abcdef" <=> "abcdef" #=> 0
|
90
|
+
# "abcdef" <=> "abcdefg" #=> -1
|
91
|
+
# "abcdef" <=> "ABCDEF" #=> 1
|
92
|
+
# "abcdef" <=> 1 #=> nil
|
93
|
+
#
|
94
|
+
def <=>: (untyped other) -> Integer?
|
95
|
+
|
96
|
+
# Equality---Returns whether `str` == `obj`, similar to Object#==.
|
97
|
+
#
|
98
|
+
# If `obj` is not an instance of String but responds to `to_str`, then the two
|
99
|
+
# strings are compared using `obj.==`.
|
100
|
+
#
|
101
|
+
# Otherwise, returns similarly to String#eql?, comparing length and content.
|
102
|
+
#
|
103
|
+
def ==: (untyped obj) -> bool
|
104
|
+
|
105
|
+
# Equality---Returns whether `str` == `obj`, similar to Object#==.
|
106
|
+
#
|
107
|
+
# If `obj` is not an instance of String but responds to `to_str`, then the two
|
108
|
+
# strings are compared using `obj.==`.
|
109
|
+
#
|
110
|
+
# Otherwise, returns similarly to String#eql?, comparing length and content.
|
111
|
+
#
|
112
|
+
def ===: (untyped obj) -> bool
|
113
|
+
|
114
|
+
# Match---If *obj* is a Regexp, uses it as a pattern to match against the
|
115
|
+
# receiver, and returns the position the match starts, or `nil` if there is no
|
116
|
+
# match. Otherwise, invokes *obj.=~*, passing the string as an argument. The
|
117
|
+
# default Object#=~ (deprecated) returns `nil`.
|
118
|
+
#
|
119
|
+
# "cat o' 9 tails" =~ /\d/ #=> 7
|
120
|
+
# "cat o' 9 tails" =~ 9 #=> nil
|
121
|
+
#
|
122
|
+
# Note that `string =~ regexp` is not the same as `regexp =~ string`. Strings
|
123
|
+
# captured from named capture groups are assigned to local variables only in the
|
124
|
+
# second case.
|
125
|
+
#
|
126
|
+
# "no. 9" =~ /(?<number>\d+)/
|
127
|
+
# number #=> nil (not assigned)
|
128
|
+
# /(?<number>\d+)/ =~ "no. 9"
|
129
|
+
# number #=> "9"
|
130
|
+
#
|
131
|
+
def =~: (untyped obj) -> Integer?
|
132
|
+
|
133
|
+
# Element Reference --- If passed a single `index`, returns a substring of one
|
134
|
+
# character at that index. If passed a `start` index and a `length`, returns a
|
135
|
+
# substring containing `length` characters starting at the `start` index. If
|
136
|
+
# passed a `range`, its beginning and end are interpreted as offsets delimiting
|
137
|
+
# the substring to be returned.
|
138
|
+
#
|
139
|
+
# In these three cases, if an index is negative, it is counted from the end of
|
140
|
+
# the string. For the `start` and `range` cases the starting index is just
|
141
|
+
# before a character and an index matching the string's size. Additionally, an
|
142
|
+
# empty string is returned when the starting index for a character range is at
|
143
|
+
# the end of the string.
|
144
|
+
#
|
145
|
+
# Returns `nil` if the initial index falls outside the string or the length is
|
146
|
+
# negative.
|
147
|
+
#
|
148
|
+
# If a `Regexp` is supplied, the matching portion of the string is returned. If
|
149
|
+
# a `capture` follows the regular expression, which may be a capture group index
|
150
|
+
# or name, follows the regular expression that component of the MatchData is
|
151
|
+
# returned instead.
|
152
|
+
#
|
153
|
+
# If a `match_str` is given, that string is returned if it occurs in the string.
|
154
|
+
#
|
155
|
+
# Returns `nil` if the regular expression does not match or the match string
|
156
|
+
# cannot be found.
|
157
|
+
#
|
158
|
+
# a = "hello there"
|
159
|
+
#
|
160
|
+
# a[1] #=> "e"
|
161
|
+
# a[2, 3] #=> "llo"
|
162
|
+
# a[2..3] #=> "ll"
|
163
|
+
#
|
164
|
+
# a[-3, 2] #=> "er"
|
165
|
+
# a[7..-2] #=> "her"
|
166
|
+
# a[-4..-2] #=> "her"
|
167
|
+
# a[-2..-4] #=> ""
|
168
|
+
#
|
169
|
+
# a[11, 0] #=> ""
|
170
|
+
# a[11] #=> nil
|
171
|
+
# a[12, 0] #=> nil
|
172
|
+
# a[12..-1] #=> nil
|
173
|
+
#
|
174
|
+
# a[/[aeiou](.)\1/] #=> "ell"
|
175
|
+
# a[/[aeiou](.)\1/, 0] #=> "ell"
|
176
|
+
# a[/[aeiou](.)\1/, 1] #=> "l"
|
177
|
+
# a[/[aeiou](.)\1/, 2] #=> nil
|
178
|
+
#
|
179
|
+
# a[/(?<vowel>[aeiou])(?<non_vowel>[^aeiou])/, "non_vowel"] #=> "l"
|
180
|
+
# a[/(?<vowel>[aeiou])(?<non_vowel>[^aeiou])/, "vowel"] #=> "e"
|
181
|
+
#
|
182
|
+
# a["lo"] #=> "lo"
|
183
|
+
# a["bye"] #=> nil
|
184
|
+
#
|
185
|
+
def []: (int index) -> String?
|
186
|
+
| (int start, int length) -> String?
|
187
|
+
| (Range[Integer] | Range[Integer?] range) -> String?
|
188
|
+
| (Regexp regexp) -> String?
|
189
|
+
| (Regexp regexp, int | String capture) -> String?
|
190
|
+
| (String match_str) -> String?
|
191
|
+
|
192
|
+
# Element Assignment---Replaces some or all of the content of *str*. The portion
|
193
|
+
# of the string affected is determined using the same criteria as String#[]. If
|
194
|
+
# the replacement string is not the same length as the text it is replacing, the
|
195
|
+
# string will be adjusted accordingly. If the regular expression or string is
|
196
|
+
# used as the index doesn't match a position in the string, IndexError is
|
197
|
+
# raised. If the regular expression form is used, the optional second Integer
|
198
|
+
# allows you to specify which portion of the match to replace (effectively using
|
199
|
+
# the MatchData indexing rules. The forms that take an Integer will raise an
|
200
|
+
# IndexError if the value is out of range; the Range form will raise a
|
201
|
+
# RangeError, and the Regexp and String will raise an IndexError on negative
|
202
|
+
# match.
|
203
|
+
#
|
204
|
+
def []=: (int pos, String new_str) -> String
|
205
|
+
| (int begin_pos, int end_pos, String new_str) -> String
|
206
|
+
| (Range[Integer] | Range[Integer?] range, String new_str) -> String
|
207
|
+
| (Regexp regexp, String new_str) -> String
|
208
|
+
| (Regexp regexp, int capture, String new_str) -> String
|
209
|
+
| (Regexp regexp, String name, String new_str) -> String
|
210
|
+
| (String other_str, String new_str) -> String
|
211
|
+
|
212
|
+
# Returns true for a string which has only ASCII characters.
|
213
|
+
#
|
214
|
+
# "abc".force_encoding("UTF-8").ascii_only? #=> true
|
215
|
+
# "abc\u{6666}".force_encoding("UTF-8").ascii_only? #=> false
|
216
|
+
#
|
217
|
+
def ascii_only?: () -> bool
|
218
|
+
|
219
|
+
# Returns a copied string whose encoding is ASCII-8BIT.
|
220
|
+
#
|
221
|
+
def b: () -> String
|
222
|
+
|
223
|
+
# Returns an array of bytes in *str*. This is a shorthand for
|
224
|
+
# `str.each_byte.to_a`.
|
225
|
+
#
|
226
|
+
# If a block is given, which is a deprecated form, works the same as
|
227
|
+
# `each_byte`.
|
228
|
+
#
|
229
|
+
def bytes: () -> Array[Integer]
|
230
|
+
| () { (Integer byte) -> void } -> String
|
231
|
+
|
232
|
+
# Returns the length of `str` in bytes.
|
233
|
+
#
|
234
|
+
# "\x80\u3042".bytesize #=> 4
|
235
|
+
# "hello".bytesize #=> 5
|
236
|
+
#
|
237
|
+
def bytesize: () -> Integer
|
238
|
+
|
239
|
+
# Byte Reference---If passed a single Integer, returns a substring of one byte
|
240
|
+
# at that position. If passed two Integer objects, returns a substring starting
|
241
|
+
# at the offset given by the first, and a length given by the second. If given a
|
242
|
+
# Range, a substring containing bytes at offsets given by the range is returned.
|
243
|
+
# In all three cases, if an offset is negative, it is counted from the end of
|
244
|
+
# *str*. Returns `nil` if the initial offset falls outside the string, the
|
245
|
+
# length is negative, or the beginning of the range is greater than the end. The
|
246
|
+
# encoding of the resulted string keeps original encoding.
|
247
|
+
#
|
248
|
+
# "hello".byteslice(1) #=> "e"
|
249
|
+
# "hello".byteslice(-1) #=> "o"
|
250
|
+
# "hello".byteslice(1, 2) #=> "el"
|
251
|
+
# "\x80\u3042".byteslice(1, 3) #=> "\u3042"
|
252
|
+
# "\x03\u3042\xff".byteslice(1..3) #=> "\u3042"
|
253
|
+
#
|
254
|
+
def byteslice: (int start, ?int length) -> String?
|
255
|
+
| (Range[Integer] | Range[Integer?] range) -> String?
|
256
|
+
|
257
|
+
# Returns a copy of *str* with the first character converted to uppercase and
|
258
|
+
# the remainder to lowercase.
|
259
|
+
#
|
260
|
+
# See String#downcase for meaning of `options` and use with different encodings.
|
261
|
+
#
|
262
|
+
# "hello".capitalize #=> "Hello"
|
263
|
+
# "HELLO".capitalize #=> "Hello"
|
264
|
+
# "123ABC".capitalize #=> "123abc"
|
265
|
+
#
|
266
|
+
def capitalize: () -> String
|
267
|
+
| (:ascii | :lithuanian | :turkic) -> String
|
268
|
+
| (:lithuanian, :turkic) -> String
|
269
|
+
| (:turkic, :lithuanian) -> String
|
270
|
+
|
271
|
+
# Modifies *str* by converting the first character to uppercase and the
|
272
|
+
# remainder to lowercase. Returns `nil` if no changes are made. There is an
|
273
|
+
# exception for modern Georgian (mkhedruli/MTAVRULI), where the result is the
|
274
|
+
# same as for String#downcase, to avoid mixed case.
|
275
|
+
#
|
276
|
+
# See String#downcase for meaning of `options` and use with different encodings.
|
277
|
+
#
|
278
|
+
# a = "hello"
|
279
|
+
# a.capitalize! #=> "Hello"
|
280
|
+
# a #=> "Hello"
|
281
|
+
# a.capitalize! #=> nil
|
282
|
+
#
|
283
|
+
def capitalize!: () -> String?
|
284
|
+
| (:ascii | :lithuanian | :turkic) -> String?
|
285
|
+
| (:lithuanian, :turkic) -> String?
|
286
|
+
| (:turkic, :lithuanian) -> String?
|
287
|
+
|
288
|
+
# Case-insensitive version of String#<=>. Currently, case-insensitivity only
|
289
|
+
# works on characters A-Z/a-z, not all of Unicode. This is different from
|
290
|
+
# String#casecmp?.
|
291
|
+
#
|
292
|
+
# "aBcDeF".casecmp("abcde") #=> 1
|
293
|
+
# "aBcDeF".casecmp("abcdef") #=> 0
|
294
|
+
# "aBcDeF".casecmp("abcdefg") #=> -1
|
295
|
+
# "abcdef".casecmp("ABCDEF") #=> 0
|
296
|
+
#
|
297
|
+
# `nil` is returned if the two strings have incompatible encodings, or if
|
298
|
+
# `other_str` is not a string.
|
299
|
+
#
|
300
|
+
# "foo".casecmp(2) #=> nil
|
301
|
+
# "\u{e4 f6 fc}".encode("ISO-8859-1").casecmp("\u{c4 d6 dc}") #=> nil
|
302
|
+
#
|
303
|
+
def casecmp: (untyped other) -> Integer?
|
304
|
+
|
305
|
+
# Returns `true` if `str` and `other_str` are equal after Unicode case folding,
|
306
|
+
# `false` if they are not equal.
|
307
|
+
#
|
308
|
+
# "aBcDeF".casecmp?("abcde") #=> false
|
309
|
+
# "aBcDeF".casecmp?("abcdef") #=> true
|
310
|
+
# "aBcDeF".casecmp?("abcdefg") #=> false
|
311
|
+
# "abcdef".casecmp?("ABCDEF") #=> true
|
312
|
+
# "\u{e4 f6 fc}".casecmp?("\u{c4 d6 dc}") #=> true
|
313
|
+
#
|
314
|
+
# `nil` is returned if the two strings have incompatible encodings, or if
|
315
|
+
# `other_str` is not a string.
|
316
|
+
#
|
317
|
+
# "foo".casecmp?(2) #=> nil
|
318
|
+
# "\u{e4 f6 fc}".encode("ISO-8859-1").casecmp?("\u{c4 d6 dc}") #=> nil
|
319
|
+
#
|
320
|
+
def casecmp?: (untyped other) -> bool
|
321
|
+
|
322
|
+
# Centers `str` in `width`. If `width` is greater than the length of `str`,
|
323
|
+
# returns a new String of length `width` with `str` centered and padded with
|
324
|
+
# `padstr`; otherwise, returns `str`.
|
325
|
+
#
|
326
|
+
# "hello".center(4) #=> "hello"
|
327
|
+
# "hello".center(20) #=> " hello "
|
328
|
+
# "hello".center(20, '123') #=> "1231231hello12312312"
|
329
|
+
#
|
330
|
+
def center: (int width, ?string padstr) -> String
|
331
|
+
|
332
|
+
# Returns an array of characters in *str*. This is a shorthand for
|
333
|
+
# `str.each_char.to_a`.
|
334
|
+
#
|
335
|
+
# If a block is given, which is a deprecated form, works the same as
|
336
|
+
# `each_char`.
|
337
|
+
#
|
338
|
+
def chars: () -> Array[String]
|
339
|
+
| () { (String char) -> void } -> String
|
340
|
+
|
341
|
+
# Returns a new String with the given record separator removed from the end of
|
342
|
+
# *str* (if present). If `$/` has not been changed from the default Ruby record
|
343
|
+
# separator, then `chomp` also removes carriage return characters (that is it
|
344
|
+
# will remove `\n`, `\r`, and `\r\n`). If `$/` is an empty string, it will
|
345
|
+
# remove all trailing newlines from the string.
|
346
|
+
#
|
347
|
+
# "hello".chomp #=> "hello"
|
348
|
+
# "hello\n".chomp #=> "hello"
|
349
|
+
# "hello\r\n".chomp #=> "hello"
|
350
|
+
# "hello\n\r".chomp #=> "hello\n"
|
351
|
+
# "hello\r".chomp #=> "hello"
|
352
|
+
# "hello \n there".chomp #=> "hello \n there"
|
353
|
+
# "hello".chomp("llo") #=> "he"
|
354
|
+
# "hello\r\n\r\n".chomp('') #=> "hello"
|
355
|
+
# "hello\r\n\r\r\n".chomp('') #=> "hello\r\n\r"
|
356
|
+
#
|
357
|
+
def chomp: (?string separator) -> String
|
358
|
+
|
359
|
+
# Modifies *str* in place as described for String#chomp, returning *str*, or
|
360
|
+
# `nil` if no modifications were made.
|
361
|
+
#
|
362
|
+
def chomp!: (?string separator) -> String?
|
363
|
+
|
364
|
+
# Returns a new String with the last character removed. If the string ends with
|
365
|
+
# `\r\n`, both characters are removed. Applying `chop` to an empty string
|
366
|
+
# returns an empty string. String#chomp is often a safer alternative, as it
|
367
|
+
# leaves the string unchanged if it doesn't end in a record separator.
|
368
|
+
#
|
369
|
+
# "string\r\n".chop #=> "string"
|
370
|
+
# "string\n\r".chop #=> "string\n"
|
371
|
+
# "string\n".chop #=> "string"
|
372
|
+
# "string".chop #=> "strin"
|
373
|
+
# "x".chop.chop #=> ""
|
374
|
+
#
|
375
|
+
def chop: () -> String
|
376
|
+
|
377
|
+
# Processes *str* as for String#chop, returning *str*, or `nil` if *str* is the
|
378
|
+
# empty string. See also String#chomp!.
|
379
|
+
#
|
380
|
+
def chop!: () -> String?
|
381
|
+
|
382
|
+
# Returns a one-character string at the beginning of the string.
|
383
|
+
#
|
384
|
+
# a = "abcde"
|
385
|
+
# a.chr #=> "a"
|
386
|
+
#
|
387
|
+
def chr: () -> String
|
388
|
+
|
389
|
+
# Makes string empty.
|
390
|
+
#
|
391
|
+
# a = "abcde"
|
392
|
+
# a.clear #=> ""
|
393
|
+
#
|
394
|
+
def clear: () -> String
|
395
|
+
|
396
|
+
# Returns an array of the Integer ordinals of the characters in *str*. This is
|
397
|
+
# a shorthand for `str.each_codepoint.to_a`.
|
398
|
+
#
|
399
|
+
# If a block is given, which is a deprecated form, works the same as
|
400
|
+
# `each_codepoint`.
|
401
|
+
#
|
402
|
+
def codepoints: () -> ::Array[Integer]
|
403
|
+
| () { (Integer codepoint) -> void } -> String
|
404
|
+
|
405
|
+
# Concatenates the given object(s) to *str*. If an object is an Integer, it is
|
406
|
+
# considered a codepoint and converted to a character before concatenation.
|
407
|
+
#
|
408
|
+
# `concat` can take multiple arguments, and all the arguments are concatenated
|
409
|
+
# in order.
|
410
|
+
#
|
411
|
+
# a = "hello "
|
412
|
+
# a.concat("world", 33) #=> "hello world!"
|
413
|
+
# a #=> "hello world!"
|
414
|
+
#
|
415
|
+
# b = "sn"
|
416
|
+
# b.concat("_", b, "_", b) #=> "sn_sn_sn"
|
417
|
+
#
|
418
|
+
# See also String#<<, which takes a single argument.
|
419
|
+
#
|
420
|
+
def concat: (*string | Integer str_or_codepoint) -> String
|
421
|
+
|
422
|
+
# Each `other_str` parameter defines a set of characters to count. The
|
423
|
+
# intersection of these sets defines the characters to count in `str`. Any
|
424
|
+
# `other_str` that starts with a caret `^` is negated. The sequence `c1-c2`
|
425
|
+
# means all characters between c1 and c2. The backslash character `\` can be
|
426
|
+
# used to escape `^` or `-` and is otherwise ignored unless it appears at the
|
427
|
+
# end of a sequence or the end of a `other_str`.
|
428
|
+
#
|
429
|
+
# a = "hello world"
|
430
|
+
# a.count "lo" #=> 5
|
431
|
+
# a.count "lo", "o" #=> 2
|
432
|
+
# a.count "hello", "^l" #=> 4
|
433
|
+
# a.count "ej-m" #=> 4
|
434
|
+
#
|
435
|
+
# "hello^world".count "\\^aeiou" #=> 4
|
436
|
+
# "hello-world".count "a\\-eo" #=> 4
|
437
|
+
#
|
438
|
+
# c = "hello world\\r\\n"
|
439
|
+
# c.count "\\" #=> 2
|
440
|
+
# c.count "\\A" #=> 0
|
441
|
+
# c.count "X-\\w" #=> 3
|
442
|
+
#
|
443
|
+
def count: (string other_str, *string other_strs) -> Integer
|
444
|
+
|
445
|
+
# Returns the string generated by calling `crypt(3)` standard library function
|
446
|
+
# with `str` and `salt_str`, in this order, as its arguments. Please do not use
|
447
|
+
# this method any longer. It is legacy; provided only for backward
|
448
|
+
# compatibility with ruby scripts in earlier days. It is bad to use in
|
449
|
+
# contemporary programs for several reasons:
|
450
|
+
#
|
451
|
+
# * Behaviour of C's `crypt(3)` depends on the OS it is run. The generated
|
452
|
+
# string lacks data portability.
|
453
|
+
#
|
454
|
+
# * On some OSes such as Mac OS, `crypt(3)` never fails (i.e. silently ends up
|
455
|
+
# in unexpected results).
|
456
|
+
#
|
457
|
+
# * On some OSes such as Mac OS, `crypt(3)` is not thread safe.
|
458
|
+
#
|
459
|
+
# * So-called "traditional" usage of `crypt(3)` is very very very weak.
|
460
|
+
# According to its manpage, Linux's traditional `crypt(3)` output has only
|
461
|
+
# 2**56 variations; too easy to brute force today. And this is the default
|
462
|
+
# behaviour.
|
463
|
+
#
|
464
|
+
# * In order to make things robust some OSes implement so-called "modular"
|
465
|
+
# usage. To go through, you have to do a complex build-up of the `salt_str`
|
466
|
+
# parameter, by hand. Failure in generation of a proper salt string tends
|
467
|
+
# not to yield any errors; typos in parameters are normally not detectable.
|
468
|
+
#
|
469
|
+
# * For instance, in the following example, the second invocation of
|
470
|
+
# String#crypt is wrong; it has a typo in "round=" (lacks "s"). However
|
471
|
+
# the call does not fail and something unexpected is generated.
|
472
|
+
#
|
473
|
+
# "foo".crypt("$5$rounds=1000$salt$") # OK, proper usage
|
474
|
+
# "foo".crypt("$5$round=1000$salt$") # Typo not detected
|
475
|
+
#
|
476
|
+
#
|
477
|
+
# * Even in the "modular" mode, some hash functions are considered archaic and
|
478
|
+
# no longer recommended at all; for instance module `$1$` is officially
|
479
|
+
# abandoned by its author: see http://phk.freebsd.dk/sagas/md5crypt_eol.html
|
480
|
+
# . For another instance module `$3$` is considered completely broken: see
|
481
|
+
# the manpage of FreeBSD.
|
482
|
+
#
|
483
|
+
# * On some OS such as Mac OS, there is no modular mode. Yet, as written
|
484
|
+
# above, `crypt(3)` on Mac OS never fails. This means even if you build up a
|
485
|
+
# proper salt string it generates a traditional DES hash anyways, and there
|
486
|
+
# is no way for you to be aware of.
|
487
|
+
#
|
488
|
+
# "foo".crypt("$5$rounds=1000$salt$") # => "$5fNPQMxC5j6."
|
489
|
+
#
|
490
|
+
#
|
491
|
+
# If for some reason you cannot migrate to other secure contemporary password
|
492
|
+
# hashing algorithms, install the string-crypt gem and `require 'string/crypt'`
|
493
|
+
# to continue using it.
|
494
|
+
#
|
495
|
+
def crypt: (string salt_str) -> String
|
496
|
+
|
497
|
+
# Returns a copy of *str* with all characters in the intersection of its
|
498
|
+
# arguments deleted. Uses the same rules for building the set of characters as
|
499
|
+
# String#count.
|
500
|
+
#
|
501
|
+
# "hello".delete "l","lo" #=> "heo"
|
502
|
+
# "hello".delete "lo" #=> "he"
|
503
|
+
# "hello".delete "aeiou", "^e" #=> "hell"
|
504
|
+
# "hello".delete "ej-m" #=> "ho"
|
505
|
+
#
|
506
|
+
def delete: (string other_str, *string other_strs) -> String
|
507
|
+
|
508
|
+
# Performs a `delete` operation in place, returning *str*, or `nil` if *str* was
|
509
|
+
# not modified.
|
510
|
+
#
|
511
|
+
def delete!: (string other_str, *string other_strs) -> String?
|
512
|
+
|
513
|
+
# Returns a copy of *str* with leading `prefix` deleted.
|
514
|
+
#
|
515
|
+
# "hello".delete_prefix("hel") #=> "lo"
|
516
|
+
# "hello".delete_prefix("llo") #=> "hello"
|
517
|
+
#
|
518
|
+
def delete_prefix: (string prefix) -> String
|
519
|
+
|
520
|
+
# Deletes leading `prefix` from *str*, returning `nil` if no change was made.
|
521
|
+
#
|
522
|
+
# "hello".delete_prefix!("hel") #=> "lo"
|
523
|
+
# "hello".delete_prefix!("llo") #=> nil
|
524
|
+
#
|
525
|
+
def delete_prefix!: (string prefix) -> String?
|
526
|
+
|
527
|
+
# Returns a copy of *str* with trailing `suffix` deleted.
|
528
|
+
#
|
529
|
+
# "hello".delete_suffix("llo") #=> "he"
|
530
|
+
# "hello".delete_suffix("hel") #=> "hello"
|
531
|
+
#
|
532
|
+
def delete_suffix: (string suffix) -> String
|
533
|
+
|
534
|
+
# Deletes trailing `suffix` from *str*, returning `nil` if no change was made.
|
535
|
+
#
|
536
|
+
# "hello".delete_suffix!("llo") #=> "he"
|
537
|
+
# "hello".delete_suffix!("hel") #=> nil
|
538
|
+
#
|
539
|
+
def delete_suffix!: (string suffix) -> String?
|
540
|
+
|
541
|
+
# Returns a copy of *str* with all uppercase letters replaced with their
|
542
|
+
# lowercase counterparts. Which letters exactly are replaced, and by which other
|
543
|
+
# letters, depends on the presence or absence of options, and on the `encoding`
|
544
|
+
# of the string.
|
545
|
+
#
|
546
|
+
# The meaning of the `options` is as follows:
|
547
|
+
#
|
548
|
+
# No option
|
549
|
+
# : Full Unicode case mapping, suitable for most languages (see :turkic and
|
550
|
+
# :lithuanian options below for exceptions). Context-dependent case mapping
|
551
|
+
# as described in Table 3-14 of the Unicode standard is currently not
|
552
|
+
# supported.
|
553
|
+
# :ascii
|
554
|
+
# : Only the ASCII region, i.e. the characters ``A'' to ``Z'' and ``a'' to
|
555
|
+
# ``z'', are affected. This option cannot be combined with any other option.
|
556
|
+
# :turkic
|
557
|
+
# : Full Unicode case mapping, adapted for Turkic languages (Turkish,
|
558
|
+
# Azerbaijani, ...). This means that upper case I is mapped to lower case
|
559
|
+
# dotless i, and so on.
|
560
|
+
# :lithuanian
|
561
|
+
# : Currently, just full Unicode case mapping. In the future, full Unicode
|
562
|
+
# case mapping adapted for Lithuanian (keeping the dot on the lower case i
|
563
|
+
# even if there is an accent on top).
|
564
|
+
# :fold
|
565
|
+
# : Only available on `downcase` and `downcase!`. Unicode case **folding**,
|
566
|
+
# which is more far-reaching than Unicode case mapping. This option
|
567
|
+
# currently cannot be combined with any other option (i.e. there is
|
568
|
+
# currently no variant for turkic languages).
|
569
|
+
#
|
570
|
+
#
|
571
|
+
# Please note that several assumptions that are valid for ASCII-only case
|
572
|
+
# conversions do not hold for more general case conversions. For example, the
|
573
|
+
# length of the result may not be the same as the length of the input (neither
|
574
|
+
# in characters nor in bytes), some roundtrip assumptions (e.g. str.downcase ==
|
575
|
+
# str.upcase.downcase) may not apply, and Unicode normalization (i.e.
|
576
|
+
# String#unicode_normalize) is not necessarily maintained by case mapping
|
577
|
+
# operations.
|
578
|
+
#
|
579
|
+
# Non-ASCII case mapping/folding is currently supported for UTF-8, UTF-16BE/LE,
|
580
|
+
# UTF-32BE/LE, and ISO-8859-1~16 Strings/Symbols. This support will be extended
|
581
|
+
# to other encodings.
|
582
|
+
#
|
583
|
+
# "hEllO".downcase #=> "hello"
|
584
|
+
#
|
585
|
+
def downcase: () -> String
|
586
|
+
| (:ascii | :fold | :lithuanian | :turkic) -> String
|
587
|
+
| (:lithuanian, :turkic) -> String
|
588
|
+
| (:turkic, :lithuanian) -> String
|
589
|
+
|
590
|
+
# Downcases the contents of *str*, returning `nil` if no changes were made.
|
591
|
+
#
|
592
|
+
# See String#downcase for meaning of `options` and use with different encodings.
|
593
|
+
#
|
594
|
+
def downcase!: () -> String?
|
595
|
+
| (:ascii | :fold | :lithuanian | :turkic) -> String?
|
596
|
+
| (:lithuanian, :turkic) -> String?
|
597
|
+
| (:turkic, :lithuanian) -> String?
|
598
|
+
|
599
|
+
# Returns a quoted version of the string with all non-printing characters
|
600
|
+
# replaced by `\xHH` notation and all special characters escaped.
|
601
|
+
#
|
602
|
+
# This method can be used for round-trip: if the resulting `new_str` is eval'ed,
|
603
|
+
# it will produce the original string.
|
604
|
+
#
|
605
|
+
# "hello \n ''".dump #=> "\"hello \\n ''\""
|
606
|
+
# "\f\x00\xff\\\"".dump #=> "\"\\f\\x00\\xFF\\\\\\\"\""
|
607
|
+
#
|
608
|
+
# See also String#undump.
|
609
|
+
#
|
610
|
+
def dump: () -> String
|
611
|
+
|
612
|
+
# Passes each byte in *str* to the given block, or returns an enumerator if no
|
613
|
+
# block is given.
|
614
|
+
#
|
615
|
+
# "hello".each_byte {|c| print c, ' ' }
|
616
|
+
#
|
617
|
+
# *produces:*
|
618
|
+
#
|
619
|
+
# 104 101 108 108 111
|
620
|
+
#
|
621
|
+
def each_byte: () { (Integer byte) -> void } -> self
|
622
|
+
| () -> ::Enumerator[Integer, self]
|
623
|
+
|
624
|
+
# Passes each character in *str* to the given block, or returns an enumerator if
|
625
|
+
# no block is given.
|
626
|
+
#
|
627
|
+
# "hello".each_char {|c| print c, ' ' }
|
628
|
+
#
|
629
|
+
# *produces:*
|
630
|
+
#
|
631
|
+
# h e l l o
|
632
|
+
#
|
633
|
+
def each_char: () { (String char) -> void } -> self
|
634
|
+
| () -> ::Enumerator[String, self]
|
635
|
+
|
636
|
+
# Passes the Integer ordinal of each character in *str*, also known as a
|
637
|
+
# *codepoint* when applied to Unicode strings to the given block. For encodings
|
638
|
+
# other than UTF-8/UTF-16(BE|LE)/UTF-32(BE|LE), values are directly derived from
|
639
|
+
# the binary representation of each character.
|
640
|
+
#
|
641
|
+
# If no block is given, an enumerator is returned instead.
|
642
|
+
#
|
643
|
+
# "hello\u0639".each_codepoint {|c| print c, ' ' }
|
644
|
+
#
|
645
|
+
# *produces:*
|
646
|
+
#
|
647
|
+
# 104 101 108 108 111 1593
|
648
|
+
#
|
649
|
+
def each_codepoint: () { (Integer codepoint) -> void } -> self
|
650
|
+
| () -> ::Enumerator[Integer, self]
|
651
|
+
|
652
|
+
# Passes each grapheme cluster in *str* to the given block, or returns an
|
653
|
+
# enumerator if no block is given. Unlike String#each_char, this enumerates by
|
654
|
+
# grapheme clusters defined by Unicode Standard Annex #29
|
655
|
+
# http://unicode.org/reports/tr29/
|
656
|
+
#
|
657
|
+
# "a\u0300".each_char.to_a.size #=> 2
|
658
|
+
# "a\u0300".each_grapheme_cluster.to_a.size #=> 1
|
659
|
+
#
|
660
|
+
def each_grapheme_cluster: () { (String grapheme) -> void } -> self
|
661
|
+
| () -> ::Enumerator[String, self]
|
662
|
+
|
663
|
+
# Splits *str* using the supplied parameter as the record separator (`$/` by
|
664
|
+
# default), passing each substring in turn to the supplied block. If a
|
665
|
+
# zero-length record separator is supplied, the string is split into paragraphs
|
666
|
+
# delimited by multiple successive newlines.
|
667
|
+
#
|
668
|
+
# If `chomp` is `true`, `separator` will be removed from the end of each line.
|
669
|
+
#
|
670
|
+
# If no block is given, an enumerator is returned instead.
|
671
|
+
#
|
672
|
+
# "hello\nworld".each_line {|s| p s}
|
673
|
+
# # prints:
|
674
|
+
# # "hello\n"
|
675
|
+
# # "world"
|
676
|
+
#
|
677
|
+
# "hello\nworld".each_line('l') {|s| p s}
|
678
|
+
# # prints:
|
679
|
+
# # "hel"
|
680
|
+
# # "l"
|
681
|
+
# # "o\nworl"
|
682
|
+
# # "d"
|
683
|
+
#
|
684
|
+
# "hello\n\n\nworld".each_line('') {|s| p s}
|
685
|
+
# # prints
|
686
|
+
# # "hello\n\n"
|
687
|
+
# # "world"
|
688
|
+
#
|
689
|
+
# "hello\nworld".each_line(chomp: true) {|s| p s}
|
690
|
+
# # prints:
|
691
|
+
# # "hello"
|
692
|
+
# # "world"
|
693
|
+
#
|
694
|
+
# "hello\nworld".each_line('l', chomp: true) {|s| p s}
|
695
|
+
# # prints:
|
696
|
+
# # "he"
|
697
|
+
# # ""
|
698
|
+
# # "o\nwor"
|
699
|
+
# # "d"
|
700
|
+
#
|
701
|
+
def each_line: (?string separator, ?chomp: bool) { (String line) -> void } -> self
|
702
|
+
| (?string separator, ?chomp: bool) -> Enumerator[String, self]
|
703
|
+
|
704
|
+
# Returns `true` if *str* has a length of zero.
|
705
|
+
#
|
706
|
+
# "hello".empty? #=> false
|
707
|
+
# " ".empty? #=> false
|
708
|
+
# "".empty? #=> true
|
709
|
+
#
|
710
|
+
def empty?: () -> bool
|
711
|
+
|
712
|
+
# The first form returns a copy of `str` transcoded to encoding `encoding`. The
|
713
|
+
# second form returns a copy of `str` transcoded from src_encoding to
|
714
|
+
# dst_encoding. The last form returns a copy of `str` transcoded to
|
715
|
+
# `Encoding.default_internal`.
|
716
|
+
#
|
717
|
+
# By default, the first and second form raise Encoding::UndefinedConversionError
|
718
|
+
# for characters that are undefined in the destination encoding, and
|
719
|
+
# Encoding::InvalidByteSequenceError for invalid byte sequences in the source
|
720
|
+
# encoding. The last form by default does not raise exceptions but uses
|
721
|
+
# replacement strings.
|
722
|
+
#
|
723
|
+
# The `options` Hash gives details for conversion and can have the following
|
724
|
+
# keys:
|
725
|
+
#
|
726
|
+
# :invalid
|
727
|
+
# : If the value is `:replace`, #encode replaces invalid byte sequences in
|
728
|
+
# `str` with the replacement character. The default is to raise the
|
729
|
+
# Encoding::InvalidByteSequenceError exception
|
730
|
+
# :undef
|
731
|
+
# : If the value is `:replace`, #encode replaces characters which are
|
732
|
+
# undefined in the destination encoding with the replacement character. The
|
733
|
+
# default is to raise the Encoding::UndefinedConversionError.
|
734
|
+
# :replace
|
735
|
+
# : Sets the replacement string to the given value. The default replacement
|
736
|
+
# string is "uFFFD" for Unicode encoding forms, and "?" otherwise.
|
737
|
+
# :fallback
|
738
|
+
# : Sets the replacement string by the given object for undefined character.
|
739
|
+
# The object should be a Hash, a Proc, a Method, or an object which has []
|
740
|
+
# method. Its key is an undefined character encoded in the source encoding
|
741
|
+
# of current transcoder. Its value can be any encoding until it can be
|
742
|
+
# converted into the destination encoding of the transcoder.
|
743
|
+
# :xml
|
744
|
+
# : The value must be `:text` or `:attr`. If the value is `:text` #encode
|
745
|
+
# replaces undefined characters with their (upper-case hexadecimal) numeric
|
746
|
+
# character references. '&', '<', and '>' are converted to "&", "<",
|
747
|
+
# and ">", respectively. If the value is `:attr`, #encode also quotes the
|
748
|
+
# replacement result (using '"'), and replaces '"' with """.
|
749
|
+
# :cr_newline
|
750
|
+
# : Replaces LF ("n") with CR ("r") if value is true.
|
751
|
+
# :crlf_newline
|
752
|
+
# : Replaces LF ("n") with CRLF ("r\n") if value is true.
|
753
|
+
# :universal_newline
|
754
|
+
# : Replaces CRLF ("r\n") and CR ("r") with LF ("n") if value is true.
|
755
|
+
#
|
756
|
+
#
|
757
|
+
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
|
758
|
+
|
759
|
+
# The first form transcodes the contents of *str* from str.encoding to
|
760
|
+
# `encoding`. The second form transcodes the contents of *str* from src_encoding
|
761
|
+
# to dst_encoding. The options Hash gives details for conversion. See
|
762
|
+
# String#encode for details. Returns the string even if no changes were made.
|
763
|
+
#
|
764
|
+
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
|
765
|
+
|
766
|
+
# Returns the Encoding object that represents the encoding of obj.
|
767
|
+
#
|
768
|
+
def encoding: () -> Encoding
|
769
|
+
|
770
|
+
# Returns true if `str` ends with one of the `suffixes` given.
|
771
|
+
#
|
772
|
+
# "hello".end_with?("ello") #=> true
|
773
|
+
#
|
774
|
+
# # returns true if one of the +suffixes+ matches.
|
775
|
+
# "hello".end_with?("heaven", "ello") #=> true
|
776
|
+
# "hello".end_with?("heaven", "paradise") #=> false
|
777
|
+
#
|
778
|
+
def end_with?: (*string suffixes) -> bool
|
779
|
+
|
780
|
+
# Two strings are equal if they have the same length and content.
|
781
|
+
#
|
782
|
+
def eql?: (untyped other) -> bool
|
783
|
+
|
784
|
+
# Changes the encoding to `encoding` and returns self.
|
785
|
+
#
|
786
|
+
def force_encoding: (string | Encoding encoding) -> self
|
787
|
+
|
788
|
+
def freeze: () -> self
|
789
|
+
|
790
|
+
# returns the *index*th byte as an integer.
|
791
|
+
#
|
792
|
+
def getbyte: (int index) -> Integer?
|
793
|
+
|
794
|
+
# Returns an array of grapheme clusters in *str*. This is a shorthand for
|
795
|
+
# `str.each_grapheme_cluster.to_a`.
|
796
|
+
#
|
797
|
+
# If a block is given, which is a deprecated form, works the same as
|
798
|
+
# `each_grapheme_cluster`.
|
799
|
+
#
|
800
|
+
def grapheme_clusters: () -> ::Array[::String]
|
801
|
+
|
802
|
+
# Returns a copy of *str* with *all* occurrences of *pattern* substituted for
|
803
|
+
# the second argument. The *pattern* is typically a Regexp; if given as a
|
804
|
+
# String, any regular expression metacharacters it contains will be interpreted
|
805
|
+
# literally, e.g. `\d` will match a backslash followed by 'd', instead of a
|
806
|
+
# digit.
|
807
|
+
#
|
808
|
+
# If `replacement` is a String it will be substituted for the matched text. It
|
809
|
+
# may contain back-references to the pattern's capture groups of the form `\d`,
|
810
|
+
# where *d* is a group number, or `\k<n>`, where *n* is a group name. Similarly,
|
811
|
+
# `\&`, `\'`, `\``, and `+` correspond to special variables, `$&`, `$'`, `$``,
|
812
|
+
# and `$+`, respectively. (See regexp.rdoc for details.) `\0` is the same as
|
813
|
+
# `\&`. `\\\` is interpreted as an escape, i.e., a single backslash. Note that,
|
814
|
+
# within `replacement` the special match variables, such as `$&`, will not refer
|
815
|
+
# to the current match.
|
816
|
+
#
|
817
|
+
# If the second argument is a Hash, and the matched text is one of its keys, the
|
818
|
+
# corresponding value is the replacement string.
|
819
|
+
#
|
820
|
+
# In the block form, the current match string is passed in as a parameter, and
|
821
|
+
# variables such as `$1`, `$2`, `$``, `$&`, and `$'` will be set appropriately.
|
822
|
+
# (See regexp.rdoc for details.) The value returned by the block will be
|
823
|
+
# substituted for the match on each call.
|
824
|
+
#
|
825
|
+
# When neither a block nor a second argument is supplied, an Enumerator is
|
826
|
+
# returned.
|
827
|
+
#
|
828
|
+
# "hello".gsub(/[aeiou]/, '*') #=> "h*ll*"
|
829
|
+
# "hello".gsub(/([aeiou])/, '<\1>') #=> "h<e>ll<o>"
|
830
|
+
# "hello".gsub(/./) {|s| s.ord.to_s + ' '} #=> "104 101 108 108 111 "
|
831
|
+
# "hello".gsub(/(?<foo>[aeiou])/, '{\k<foo>}') #=> "h{e}ll{o}"
|
832
|
+
# 'hello'.gsub(/[eo]/, 'e' => 3, 'o' => '*') #=> "h3ll*"
|
833
|
+
#
|
834
|
+
# Note that a string literal consumes backslashes. (See syntax/literals.rdoc for
|
835
|
+
# details on string literals.) Back-references are typically preceded by an
|
836
|
+
# additional backslash. For example, if you want to write a back-reference `\&`
|
837
|
+
# in `replacement` with a double-quoted string literal, you need to write:
|
838
|
+
# `"..\\\\&.."`. If you want to write a non-back-reference string `\&` in
|
839
|
+
# `replacement`, you need first to escape the backslash to prevent this method
|
840
|
+
# from interpreting it as a back-reference, and then you need to escape the
|
841
|
+
# backslashes again to prevent a string literal from consuming them:
|
842
|
+
# `"..\\\\\\\\&.."`. You may want to use the block form to avoid a lot of
|
843
|
+
# backslashes.
|
844
|
+
#
|
845
|
+
def gsub: (Regexp | string pattern, string replacement) -> String
|
846
|
+
| (Regexp | string pattern, Hash[String, String] hash) -> String
|
847
|
+
| (Regexp | string pattern) { (String match) -> _ToS } -> String
|
848
|
+
| (Regexp | string pattern) -> ::Enumerator[String, self]
|
849
|
+
|
850
|
+
# Performs the substitutions of String#gsub in place, returning *str*, or `nil`
|
851
|
+
# if no substitutions were performed. If no block and no *replacement* is
|
852
|
+
# given, an enumerator is returned instead.
|
853
|
+
#
|
854
|
+
def gsub!: (Regexp | string pattern, string replacement) -> String?
|
855
|
+
| (Regexp | string pattern, Hash[String, String] hash) -> String?
|
856
|
+
| (Regexp | string pattern) { (String match) -> _ToS } -> String?
|
857
|
+
| (Regexp | string pattern) -> ::Enumerator[String, self]
|
858
|
+
|
859
|
+
# Returns a hash based on the string's length, content and encoding.
|
860
|
+
#
|
861
|
+
# See also Object#hash.
|
862
|
+
#
|
863
|
+
def hash: () -> Integer
|
864
|
+
|
865
|
+
# Treats leading characters from *str* as a string of hexadecimal digits (with
|
866
|
+
# an optional sign and an optional `0x`) and returns the corresponding number.
|
867
|
+
# Zero is returned on error.
|
868
|
+
#
|
869
|
+
# "0x0a".hex #=> 10
|
870
|
+
# "-1234".hex #=> -4660
|
871
|
+
# "0".hex #=> 0
|
872
|
+
# "wombat".hex #=> 0
|
873
|
+
#
|
874
|
+
def hex: () -> Integer
|
875
|
+
|
876
|
+
# Returns `true` if *str* contains the given string or character.
|
877
|
+
#
|
878
|
+
# "hello".include? "lo" #=> true
|
879
|
+
# "hello".include? "ol" #=> false
|
880
|
+
# "hello".include? ?h #=> true
|
881
|
+
#
|
882
|
+
def include?: (string other_str) -> bool
|
883
|
+
|
884
|
+
# Returns the index of the first occurrence of the given *substring* or pattern
|
885
|
+
# (*regexp*) in *str*. Returns `nil` if not found. If the second parameter is
|
886
|
+
# present, it specifies the position in the string to begin the search.
|
887
|
+
#
|
888
|
+
# "hello".index('e') #=> 1
|
889
|
+
# "hello".index('lo') #=> 3
|
890
|
+
# "hello".index('a') #=> nil
|
891
|
+
# "hello".index(?e) #=> 1
|
892
|
+
# "hello".index(/[aeiou]/, -3) #=> 4
|
893
|
+
#
|
894
|
+
def index: (Regexp | string substr_or_regexp, ?int offset) -> Integer?
|
895
|
+
|
896
|
+
# Inserts *other_str* before the character at the given *index*, modifying
|
897
|
+
# *str*. Negative indices count from the end of the string, and insert *after*
|
898
|
+
# the given character. The intent is insert *aString* so that it starts at the
|
899
|
+
# given *index*.
|
900
|
+
#
|
901
|
+
# "abcd".insert(0, 'X') #=> "Xabcd"
|
902
|
+
# "abcd".insert(3, 'X') #=> "abcXd"
|
903
|
+
# "abcd".insert(4, 'X') #=> "abcdX"
|
904
|
+
# "abcd".insert(-3, 'X') #=> "abXcd"
|
905
|
+
# "abcd".insert(-1, 'X') #=> "abcdX"
|
906
|
+
#
|
907
|
+
def insert: (int index, string other_str) -> String
|
908
|
+
|
909
|
+
# Returns a printable version of *str*, surrounded by quote marks, with special
|
910
|
+
# characters escaped.
|
911
|
+
#
|
912
|
+
# str = "hello"
|
913
|
+
# str[3] = "\b"
|
914
|
+
# str.inspect #=> "\"hel\\bo\""
|
915
|
+
#
|
916
|
+
def inspect: () -> String
|
917
|
+
|
918
|
+
# Returns the Symbol corresponding to *str*, creating the symbol if it did not
|
919
|
+
# previously exist. See Symbol#id2name.
|
920
|
+
#
|
921
|
+
# "Koala".intern #=> :Koala
|
922
|
+
# s = 'cat'.to_sym #=> :cat
|
923
|
+
# s == :cat #=> true
|
924
|
+
# s = '@cat'.to_sym #=> :@cat
|
925
|
+
# s == :@cat #=> true
|
926
|
+
#
|
927
|
+
# This can also be used to create symbols that cannot be represented using the
|
928
|
+
# `:xxx` notation.
|
929
|
+
#
|
930
|
+
# 'cat and dog'.to_sym #=> :"cat and dog"
|
931
|
+
#
|
932
|
+
def intern: () -> Symbol
|
933
|
+
|
934
|
+
# Returns the character length of *str*.
|
935
|
+
#
|
936
|
+
def length: () -> Integer
|
937
|
+
|
938
|
+
# Returns an array of lines in *str* split using the supplied record separator
|
939
|
+
# (`$/` by default). This is a shorthand for `str.each_line(separator,
|
940
|
+
# getline_args).to_a`.
|
941
|
+
#
|
942
|
+
# If `chomp` is `true`, `separator` will be removed from the end of each line.
|
943
|
+
#
|
944
|
+
# "hello\nworld\n".lines #=> ["hello\n", "world\n"]
|
945
|
+
# "hello world".lines(' ') #=> ["hello ", " ", "world"]
|
946
|
+
# "hello\nworld\n".lines(chomp: true) #=> ["hello", "world"]
|
947
|
+
#
|
948
|
+
# If a block is given, which is a deprecated form, works the same as
|
949
|
+
# `each_line`.
|
950
|
+
#
|
951
|
+
def lines: (?string separator, ?chomp: bool) -> Array[String]
|
952
|
+
|
953
|
+
# If *integer* is greater than the length of *str*, returns a new String of
|
954
|
+
# length *integer* with *str* left justified and padded with *padstr*;
|
955
|
+
# otherwise, returns *str*.
|
956
|
+
#
|
957
|
+
# "hello".ljust(4) #=> "hello"
|
958
|
+
# "hello".ljust(20) #=> "hello "
|
959
|
+
# "hello".ljust(20, '1234') #=> "hello123412341234123"
|
960
|
+
#
|
961
|
+
def ljust: (int integer, ?string padstr) -> String
|
962
|
+
|
963
|
+
# Returns a copy of the receiver with leading whitespace removed. See also
|
964
|
+
# String#rstrip and String#strip.
|
965
|
+
#
|
966
|
+
# Refer to String#strip for the definition of whitespace.
|
967
|
+
#
|
968
|
+
# " hello ".lstrip #=> "hello "
|
969
|
+
# "hello".lstrip #=> "hello"
|
970
|
+
#
|
971
|
+
def lstrip: () -> String
|
972
|
+
|
973
|
+
# Removes leading whitespace from the receiver. Returns the altered receiver, or
|
974
|
+
# `nil` if no change was made. See also String#rstrip! and String#strip!.
|
975
|
+
#
|
976
|
+
# Refer to String#strip for the definition of whitespace.
|
977
|
+
#
|
978
|
+
# " hello ".lstrip! #=> "hello "
|
979
|
+
# "hello ".lstrip! #=> nil
|
980
|
+
# "hello".lstrip! #=> nil
|
981
|
+
#
|
982
|
+
def lstrip!: () -> self?
|
983
|
+
|
984
|
+
# Converts *pattern* to a Regexp (if it isn't already one), then invokes its
|
985
|
+
# `match` method on the receiver. If the second parameter is present, it
|
986
|
+
# specifies the position in the string to begin the search.
|
987
|
+
#
|
988
|
+
# 'hello'.match('(.)\1') #=> #<MatchData "ll" 1:"l">
|
989
|
+
# 'hello'.match('(.)\1')[0] #=> "ll"
|
990
|
+
# 'hello'.match(/(.)\1/)[0] #=> "ll"
|
991
|
+
# 'hello'.match(/(.)\1/, 3) #=> nil
|
992
|
+
# 'hello'.match('xx') #=> nil
|
993
|
+
#
|
994
|
+
# If a block is given, invokes the block with MatchData if match succeeds, so
|
995
|
+
# that you can write
|
996
|
+
#
|
997
|
+
# str.match(pat) {|m| block }
|
998
|
+
#
|
999
|
+
# instead of
|
1000
|
+
#
|
1001
|
+
# if m = str.match(pat)
|
1002
|
+
# # ...
|
1003
|
+
# end
|
1004
|
+
#
|
1005
|
+
# The return value in this case is the value from block execution.
|
1006
|
+
#
|
1007
|
+
def match: (Regexp | string pattern, ?int pos) -> MatchData?
|
1008
|
+
| [A] (Regexp | string pattern, ?int pos) { (MatchData) -> A } -> A
|
1009
|
+
|
1010
|
+
# Converts *pattern* to a `Regexp` (if it isn't already one), then returns a
|
1011
|
+
# `true` or `false` indicates whether the regexp is matched *str* or not without
|
1012
|
+
# updating `$~` and other related variables. If the second parameter is
|
1013
|
+
# present, it specifies the position in the string to begin the search.
|
1014
|
+
#
|
1015
|
+
# "Ruby".match?(/R.../) #=> true
|
1016
|
+
# "Ruby".match?(/R.../, 1) #=> false
|
1017
|
+
# "Ruby".match?(/P.../) #=> false
|
1018
|
+
# $& #=> nil
|
1019
|
+
#
|
1020
|
+
def match?: (Regexp | string pattern, ?int pos) -> bool
|
1021
|
+
|
1022
|
+
# Returns the successor to *str*. The successor is calculated by incrementing
|
1023
|
+
# characters starting from the rightmost alphanumeric (or the rightmost
|
1024
|
+
# character if there are no alphanumerics) in the string. Incrementing a digit
|
1025
|
+
# always results in another digit, and incrementing a letter results in another
|
1026
|
+
# letter of the same case. Incrementing nonalphanumerics uses the underlying
|
1027
|
+
# character set's collating sequence.
|
1028
|
+
#
|
1029
|
+
# If the increment generates a ``carry,'' the character to the left of it is
|
1030
|
+
# incremented. This process repeats until there is no carry, adding an
|
1031
|
+
# additional character if necessary.
|
1032
|
+
#
|
1033
|
+
# "abcd".succ #=> "abce"
|
1034
|
+
# "THX1138".succ #=> "THX1139"
|
1035
|
+
# "<<koala>>".succ #=> "<<koalb>>"
|
1036
|
+
# "1999zzz".succ #=> "2000aaa"
|
1037
|
+
# "ZZZ9999".succ #=> "AAAA0000"
|
1038
|
+
# "***".succ #=> "**+"
|
1039
|
+
#
|
1040
|
+
def next: () -> String
|
1041
|
+
|
1042
|
+
# Equivalent to String#succ, but modifies the receiver in place.
|
1043
|
+
#
|
1044
|
+
def next!: () -> self
|
1045
|
+
|
1046
|
+
# Treats leading characters of *str* as a string of octal digits (with an
|
1047
|
+
# optional sign) and returns the corresponding number. Returns 0 if the
|
1048
|
+
# conversion fails.
|
1049
|
+
#
|
1050
|
+
# "123".oct #=> 83
|
1051
|
+
# "-377".oct #=> -255
|
1052
|
+
# "bad".oct #=> 0
|
1053
|
+
# "0377bad".oct #=> 255
|
1054
|
+
#
|
1055
|
+
# If `str` starts with `0`, radix indicators are honored. See Kernel#Integer.
|
1056
|
+
#
|
1057
|
+
def oct: () -> Integer
|
1058
|
+
|
1059
|
+
# Returns the Integer ordinal of a one-character string.
|
1060
|
+
#
|
1061
|
+
# "a".ord #=> 97
|
1062
|
+
#
|
1063
|
+
def ord: () -> Integer
|
1064
|
+
|
1065
|
+
# Searches *sep* or pattern (*regexp*) in the string and returns the part before
|
1066
|
+
# it, the match, and the part after it. If it is not found, returns two empty
|
1067
|
+
# strings and *str*.
|
1068
|
+
#
|
1069
|
+
# "hello".partition("l") #=> ["he", "l", "lo"]
|
1070
|
+
# "hello".partition("x") #=> ["hello", "", ""]
|
1071
|
+
# "hello".partition(/.l/) #=> ["h", "el", "lo"]
|
1072
|
+
#
|
1073
|
+
def partition: (Regexp | string sep_or_regexp) -> [ String, String, String ]
|
1074
|
+
|
1075
|
+
# Prepend---Prepend the given strings to *str*.
|
1076
|
+
#
|
1077
|
+
# a = "!"
|
1078
|
+
# a.prepend("hello ", "world") #=> "hello world!"
|
1079
|
+
# a #=> "hello world!"
|
1080
|
+
#
|
1081
|
+
# See also String#concat.
|
1082
|
+
#
|
1083
|
+
def prepend: (*string other_strs) -> String
|
1084
|
+
|
1085
|
+
# Replaces the contents of *str* with the corresponding values in *other_str*.
|
1086
|
+
#
|
1087
|
+
# s = "hello" #=> "hello"
|
1088
|
+
# s.replace "world" #=> "world"
|
1089
|
+
#
|
1090
|
+
def replace: (string other_str) -> String
|
1091
|
+
|
1092
|
+
# Returns a new string with the characters from *str* in reverse order.
|
1093
|
+
#
|
1094
|
+
# "stressed".reverse #=> "desserts"
|
1095
|
+
#
|
1096
|
+
def reverse: () -> String
|
1097
|
+
|
1098
|
+
# Reverses *str* in place.
|
1099
|
+
#
|
1100
|
+
def reverse!: () -> self
|
1101
|
+
|
1102
|
+
# Returns the index of the last occurrence of the given *substring* or pattern
|
1103
|
+
# (*regexp*) in *str*. Returns `nil` if not found. If the second parameter is
|
1104
|
+
# present, it specifies the position in the string to end the
|
1105
|
+
# search---characters beyond this point will not be considered.
|
1106
|
+
#
|
1107
|
+
# "hello".rindex('e') #=> 1
|
1108
|
+
# "hello".rindex('l') #=> 3
|
1109
|
+
# "hello".rindex('a') #=> nil
|
1110
|
+
# "hello".rindex(?e) #=> 1
|
1111
|
+
# "hello".rindex(/[aeiou]/, -2) #=> 1
|
1112
|
+
#
|
1113
|
+
def rindex: (string | Regexp substr_or_regexp, ?int pos) -> Integer?
|
1114
|
+
|
1115
|
+
# If *integer* is greater than the length of *str*, returns a new String of
|
1116
|
+
# length *integer* with *str* right justified and padded with *padstr*;
|
1117
|
+
# otherwise, returns *str*.
|
1118
|
+
#
|
1119
|
+
# "hello".rjust(4) #=> "hello"
|
1120
|
+
# "hello".rjust(20) #=> " hello"
|
1121
|
+
# "hello".rjust(20, '1234') #=> "123412341234123hello"
|
1122
|
+
#
|
1123
|
+
def rjust: (int integer, ?string padstr) -> String
|
1124
|
+
|
1125
|
+
# Searches *sep* or pattern (*regexp*) in the string from the end of the string,
|
1126
|
+
# and returns the part before it, the match, and the part after it. If it is not
|
1127
|
+
# found, returns two empty strings and *str*.
|
1128
|
+
#
|
1129
|
+
# "hello".rpartition("l") #=> ["hel", "l", "o"]
|
1130
|
+
# "hello".rpartition("x") #=> ["", "", "hello"]
|
1131
|
+
# "hello".rpartition(/.l/) #=> ["he", "ll", "o"]
|
1132
|
+
#
|
1133
|
+
def rpartition: (string | Regexp sep_or_regexp) -> [ String, String, String ]
|
1134
|
+
|
1135
|
+
# Returns a copy of the receiver with trailing whitespace removed. See also
|
1136
|
+
# String#lstrip and String#strip.
|
1137
|
+
#
|
1138
|
+
# Refer to String#strip for the definition of whitespace.
|
1139
|
+
#
|
1140
|
+
# " hello ".rstrip #=> " hello"
|
1141
|
+
# "hello".rstrip #=> "hello"
|
1142
|
+
#
|
1143
|
+
def rstrip: () -> String
|
1144
|
+
|
1145
|
+
# Removes trailing whitespace from the receiver. Returns the altered receiver,
|
1146
|
+
# or `nil` if no change was made. See also String#lstrip! and String#strip!.
|
1147
|
+
#
|
1148
|
+
# Refer to String#strip for the definition of whitespace.
|
1149
|
+
#
|
1150
|
+
# " hello ".rstrip! #=> " hello"
|
1151
|
+
# " hello".rstrip! #=> nil
|
1152
|
+
# "hello".rstrip! #=> nil
|
1153
|
+
#
|
1154
|
+
def rstrip!: () -> self?
|
1155
|
+
|
1156
|
+
# Both forms iterate through *str*, matching the pattern (which may be a Regexp
|
1157
|
+
# or a String). For each match, a result is generated and either added to the
|
1158
|
+
# result array or passed to the block. If the pattern contains no groups, each
|
1159
|
+
# individual result consists of the matched string, `$&`. If the pattern
|
1160
|
+
# contains groups, each individual result is itself an array containing one
|
1161
|
+
# entry per group.
|
1162
|
+
#
|
1163
|
+
# a = "cruel world"
|
1164
|
+
# a.scan(/\w+/) #=> ["cruel", "world"]
|
1165
|
+
# a.scan(/.../) #=> ["cru", "el ", "wor"]
|
1166
|
+
# a.scan(/(...)/) #=> [["cru"], ["el "], ["wor"]]
|
1167
|
+
# a.scan(/(..)(..)/) #=> [["cr", "ue"], ["l ", "wo"]]
|
1168
|
+
#
|
1169
|
+
# And the block form:
|
1170
|
+
#
|
1171
|
+
# a.scan(/\w+/) {|w| print "<<#{w}>> " }
|
1172
|
+
# print "\n"
|
1173
|
+
# a.scan(/(.)(.)/) {|x,y| print y, x }
|
1174
|
+
# print "\n"
|
1175
|
+
#
|
1176
|
+
# *produces:*
|
1177
|
+
#
|
1178
|
+
# <<cruel>> <<world>>
|
1179
|
+
# rceu lowlr
|
1180
|
+
#
|
1181
|
+
def scan: (Regexp | string pattern) -> Array[String | Array[String]]
|
1182
|
+
| (Regexp | string pattern) { (String | Array[String]) -> void } -> self
|
1183
|
+
|
1184
|
+
# If the string is invalid byte sequence then replace invalid bytes with given
|
1185
|
+
# replacement character, else returns self. If block is given, replace invalid
|
1186
|
+
# bytes with returned value of the block.
|
1187
|
+
#
|
1188
|
+
# "abc\u3042\x81".scrub #=> "abc\u3042\uFFFD"
|
1189
|
+
# "abc\u3042\x81".scrub("*") #=> "abc\u3042*"
|
1190
|
+
# "abc\u3042\xE3\x80".scrub{|bytes| '<'+bytes.unpack('H*')[0]+'>' } #=> "abc\u3042<e380>"
|
1191
|
+
#
|
1192
|
+
def scrub: (?string repl) -> String
|
1193
|
+
| () { (String bytes) -> string } -> String
|
1194
|
+
|
1195
|
+
# If the string is invalid byte sequence then replace invalid bytes with given
|
1196
|
+
# replacement character, else returns self. If block is given, replace invalid
|
1197
|
+
# bytes with returned value of the block.
|
1198
|
+
#
|
1199
|
+
# "abc\u3042\x81".scrub! #=> "abc\u3042\uFFFD"
|
1200
|
+
# "abc\u3042\x81".scrub!("*") #=> "abc\u3042*"
|
1201
|
+
# "abc\u3042\xE3\x80".scrub!{|bytes| '<'+bytes.unpack('H*')[0]+'>' } #=> "abc\u3042<e380>"
|
1202
|
+
#
|
1203
|
+
def scrub!: (?string repl) -> self
|
1204
|
+
| () { (String bytes) -> string } -> self
|
1205
|
+
|
1206
|
+
# modifies the *index*th byte as *integer*.
|
1207
|
+
#
|
1208
|
+
def setbyte: (int index, int integer) -> int
|
1209
|
+
|
1210
|
+
# Returns the character length of *str*.
|
1211
|
+
#
|
1212
|
+
alias size length
|
1213
|
+
|
1214
|
+
# Element Reference --- If passed a single `index`, returns a substring of one
|
1215
|
+
# character at that index. If passed a `start` index and a `length`, returns a
|
1216
|
+
# substring containing `length` characters starting at the `start` index. If
|
1217
|
+
# passed a `range`, its beginning and end are interpreted as offsets delimiting
|
1218
|
+
# the substring to be returned.
|
1219
|
+
#
|
1220
|
+
# In these three cases, if an index is negative, it is counted from the end of
|
1221
|
+
# the string. For the `start` and `range` cases the starting index is just
|
1222
|
+
# before a character and an index matching the string's size. Additionally, an
|
1223
|
+
# empty string is returned when the starting index for a character range is at
|
1224
|
+
# the end of the string.
|
1225
|
+
#
|
1226
|
+
# Returns `nil` if the initial index falls outside the string or the length is
|
1227
|
+
# negative.
|
1228
|
+
#
|
1229
|
+
# If a `Regexp` is supplied, the matching portion of the string is returned. If
|
1230
|
+
# a `capture` follows the regular expression, which may be a capture group index
|
1231
|
+
# or name, follows the regular expression that component of the MatchData is
|
1232
|
+
# returned instead.
|
1233
|
+
#
|
1234
|
+
# If a `match_str` is given, that string is returned if it occurs in the string.
|
1235
|
+
#
|
1236
|
+
# Returns `nil` if the regular expression does not match or the match string
|
1237
|
+
# cannot be found.
|
1238
|
+
#
|
1239
|
+
# a = "hello there"
|
1240
|
+
#
|
1241
|
+
# a[1] #=> "e"
|
1242
|
+
# a[2, 3] #=> "llo"
|
1243
|
+
# a[2..3] #=> "ll"
|
1244
|
+
#
|
1245
|
+
# a[-3, 2] #=> "er"
|
1246
|
+
# a[7..-2] #=> "her"
|
1247
|
+
# a[-4..-2] #=> "her"
|
1248
|
+
# a[-2..-4] #=> ""
|
1249
|
+
#
|
1250
|
+
# a[11, 0] #=> ""
|
1251
|
+
# a[11] #=> nil
|
1252
|
+
# a[12, 0] #=> nil
|
1253
|
+
# a[12..-1] #=> nil
|
1254
|
+
#
|
1255
|
+
# a[/[aeiou](.)\1/] #=> "ell"
|
1256
|
+
# a[/[aeiou](.)\1/, 0] #=> "ell"
|
1257
|
+
# a[/[aeiou](.)\1/, 1] #=> "l"
|
1258
|
+
# a[/[aeiou](.)\1/, 2] #=> nil
|
1259
|
+
#
|
1260
|
+
# a[/(?<vowel>[aeiou])(?<non_vowel>[^aeiou])/, "non_vowel"] #=> "l"
|
1261
|
+
# a[/(?<vowel>[aeiou])(?<non_vowel>[^aeiou])/, "vowel"] #=> "e"
|
1262
|
+
#
|
1263
|
+
# a["lo"] #=> "lo"
|
1264
|
+
# a["bye"] #=> nil
|
1265
|
+
#
|
1266
|
+
alias slice []
|
1267
|
+
|
1268
|
+
# Deletes the specified portion from *str*, and returns the portion deleted.
|
1269
|
+
#
|
1270
|
+
# string = "this is a string"
|
1271
|
+
# string.slice!(2) #=> "i"
|
1272
|
+
# string.slice!(3..6) #=> " is "
|
1273
|
+
# string.slice!(/s.*t/) #=> "sa st"
|
1274
|
+
# string.slice!("r") #=> "r"
|
1275
|
+
# string #=> "thing"
|
1276
|
+
#
|
1277
|
+
def slice!: (int integer, ?int integer) -> String?
|
1278
|
+
| (Range[Integer] | Range[Integer?] range) -> String?
|
1279
|
+
| (Regexp regexp, ?int | String capture) -> String?
|
1280
|
+
| (String other_str) -> String?
|
1281
|
+
|
1282
|
+
# Divides *str* into substrings based on a delimiter, returning an array of
|
1283
|
+
# these substrings.
|
1284
|
+
#
|
1285
|
+
# If *pattern* is a String, then its contents are used as the delimiter when
|
1286
|
+
# splitting *str*. If *pattern* is a single space, *str* is split on whitespace,
|
1287
|
+
# with leading and trailing whitespace and runs of contiguous whitespace
|
1288
|
+
# characters ignored.
|
1289
|
+
#
|
1290
|
+
# If *pattern* is a Regexp, *str* is divided where the pattern matches. Whenever
|
1291
|
+
# the pattern matches a zero-length string, *str* is split into individual
|
1292
|
+
# characters. If *pattern* contains groups, the respective matches will be
|
1293
|
+
# returned in the array as well.
|
1294
|
+
#
|
1295
|
+
# If *pattern* is `nil`, the value of `$;` is used. If `$;` is `nil` (which is
|
1296
|
+
# the default), *str* is split on whitespace as if ' ' were specified.
|
1297
|
+
#
|
1298
|
+
# If the *limit* parameter is omitted, trailing null fields are suppressed. If
|
1299
|
+
# *limit* is a positive number, at most that number of split substrings will be
|
1300
|
+
# returned (captured groups will be returned as well, but are not counted
|
1301
|
+
# towards the limit). If *limit* is `1`, the entire string is returned as the
|
1302
|
+
# only entry in an array. If negative, there is no limit to the number of fields
|
1303
|
+
# returned, and trailing null fields are not suppressed.
|
1304
|
+
#
|
1305
|
+
# When the input `str` is empty an empty Array is returned as the string is
|
1306
|
+
# considered to have no fields to split.
|
1307
|
+
#
|
1308
|
+
# " now's the time ".split #=> ["now's", "the", "time"]
|
1309
|
+
# " now's the time ".split(' ') #=> ["now's", "the", "time"]
|
1310
|
+
# " now's the time".split(/ /) #=> ["", "now's", "", "the", "time"]
|
1311
|
+
# "1, 2.34,56, 7".split(%r{,\s*}) #=> ["1", "2.34", "56", "7"]
|
1312
|
+
# "hello".split(//) #=> ["h", "e", "l", "l", "o"]
|
1313
|
+
# "hello".split(//, 3) #=> ["h", "e", "llo"]
|
1314
|
+
# "hi mom".split(%r{\s*}) #=> ["h", "i", "m", "o", "m"]
|
1315
|
+
#
|
1316
|
+
# "mellow yellow".split("ello") #=> ["m", "w y", "w"]
|
1317
|
+
# "1,2,,3,4,,".split(',') #=> ["1", "2", "", "3", "4"]
|
1318
|
+
# "1,2,,3,4,,".split(',', 4) #=> ["1", "2", "", "3,4,,"]
|
1319
|
+
# "1,2,,3,4,,".split(',', -4) #=> ["1", "2", "", "3", "4", "", ""]
|
1320
|
+
#
|
1321
|
+
# "1:2:3".split(/(:)()()/, 2) #=> ["1", ":", "", "", "2:3"]
|
1322
|
+
#
|
1323
|
+
# "".split(',', -1) #=> []
|
1324
|
+
#
|
1325
|
+
# If a block is given, invoke the block with each split substring.
|
1326
|
+
#
|
1327
|
+
def split: (?Regexp | string pattern, ?int limit) -> Array[String]
|
1328
|
+
| (?Regexp | string pattern, ?int limit) { (String) -> void } -> self
|
1329
|
+
|
1330
|
+
# Builds a set of characters from the *other_str* parameter(s) using the
|
1331
|
+
# procedure described for String#count. Returns a new string where runs of the
|
1332
|
+
# same character that occur in this set are replaced by a single character. If
|
1333
|
+
# no arguments are given, all runs of identical characters are replaced by a
|
1334
|
+
# single character.
|
1335
|
+
#
|
1336
|
+
# "yellow moon".squeeze #=> "yelow mon"
|
1337
|
+
# " now is the".squeeze(" ") #=> " now is the"
|
1338
|
+
# "putters shoot balls".squeeze("m-z") #=> "puters shot balls"
|
1339
|
+
#
|
1340
|
+
def squeeze: (*string other_str) -> String
|
1341
|
+
|
1342
|
+
# Squeezes *str* in place, returning either *str*, or `nil` if no changes were
|
1343
|
+
# made.
|
1344
|
+
#
|
1345
|
+
def squeeze!: (*string other_str) -> self?
|
1346
|
+
|
1347
|
+
# Returns true if `str` starts with one of the `prefixes` given. Each of the
|
1348
|
+
# `prefixes` should be a String or a Regexp.
|
1349
|
+
#
|
1350
|
+
# "hello".start_with?("hell") #=> true
|
1351
|
+
# "hello".start_with?(/H/i) #=> true
|
1352
|
+
#
|
1353
|
+
# # returns true if one of the prefixes matches.
|
1354
|
+
# "hello".start_with?("heaven", "hell") #=> true
|
1355
|
+
# "hello".start_with?("heaven", "paradise") #=> false
|
1356
|
+
#
|
1357
|
+
def start_with?: (*string prefixes) -> bool
|
1358
|
+
|
1359
|
+
# Returns a copy of the receiver with leading and trailing whitespace removed.
|
1360
|
+
#
|
1361
|
+
# Whitespace is defined as any of the following characters: null, horizontal
|
1362
|
+
# tab, line feed, vertical tab, form feed, carriage return, space.
|
1363
|
+
#
|
1364
|
+
# " hello ".strip #=> "hello"
|
1365
|
+
# "\tgoodbye\r\n".strip #=> "goodbye"
|
1366
|
+
# "\x00\t\n\v\f\r ".strip #=> ""
|
1367
|
+
# "hello".strip #=> "hello"
|
1368
|
+
#
|
1369
|
+
def strip: () -> String
|
1370
|
+
|
1371
|
+
# Removes leading and trailing whitespace from the receiver. Returns the altered
|
1372
|
+
# receiver, or `nil` if there was no change.
|
1373
|
+
#
|
1374
|
+
# Refer to String#strip for the definition of whitespace.
|
1375
|
+
#
|
1376
|
+
# " hello ".strip! #=> "hello"
|
1377
|
+
# "hello".strip! #=> nil
|
1378
|
+
#
|
1379
|
+
def strip!: () -> self?
|
1380
|
+
|
1381
|
+
# Returns a copy of `str` with the *first* occurrence of `pattern` replaced by
|
1382
|
+
# the second argument. The `pattern` is typically a Regexp; if given as a
|
1383
|
+
# String, any regular expression metacharacters it contains will be interpreted
|
1384
|
+
# literally, e.g. `\d` will match a backslash followed by 'd', instead of a
|
1385
|
+
# digit.
|
1386
|
+
#
|
1387
|
+
# If `replacement` is a String it will be substituted for the matched text. It
|
1388
|
+
# may contain back-references to the pattern's capture groups of the form `\d`,
|
1389
|
+
# where *d* is a group number, or `\k<n>`, where *n* is a group name. Similarly,
|
1390
|
+
# `\&`, `\'`, `\``, and `+` correspond to special variables, `$&`, `$'`, `$``,
|
1391
|
+
# and `$+`, respectively. (See regexp.rdoc for details.) `\0` is the same as
|
1392
|
+
# `\&`. `\\\` is interpreted as an escape, i.e., a single backslash. Note that,
|
1393
|
+
# within `replacement` the special match variables, such as `$&`, will not refer
|
1394
|
+
# to the current match.
|
1395
|
+
#
|
1396
|
+
# If the second argument is a Hash, and the matched text is one of its keys, the
|
1397
|
+
# corresponding value is the replacement string.
|
1398
|
+
#
|
1399
|
+
# In the block form, the current match string is passed in as a parameter, and
|
1400
|
+
# variables such as `$1`, `$2`, `$``, `$&`, and `$'` will be set appropriately.
|
1401
|
+
# (See regexp.rdoc for details.) The value returned by the block will be
|
1402
|
+
# substituted for the match on each call.
|
1403
|
+
#
|
1404
|
+
# "hello".sub(/[aeiou]/, '*') #=> "h*llo"
|
1405
|
+
# "hello".sub(/([aeiou])/, '<\1>') #=> "h<e>llo"
|
1406
|
+
# "hello".sub(/./) {|s| s.ord.to_s + ' ' } #=> "104 ello"
|
1407
|
+
# "hello".sub(/(?<foo>[aeiou])/, '*\k<foo>*') #=> "h*e*llo"
|
1408
|
+
# 'Is SHELL your preferred shell?'.sub(/[[:upper:]]{2,}/, ENV)
|
1409
|
+
# #=> "Is /bin/bash your preferred shell?"
|
1410
|
+
#
|
1411
|
+
# Note that a string literal consumes backslashes. (See syntax/literals.rdoc for
|
1412
|
+
# details about string literals.) Back-references are typically preceded by an
|
1413
|
+
# additional backslash. For example, if you want to write a back-reference `\&`
|
1414
|
+
# in `replacement` with a double-quoted string literal, you need to write:
|
1415
|
+
# `"..\\\\&.."`. If you want to write a non-back-reference string `\&` in
|
1416
|
+
# `replacement`, you need first to escape the backslash to prevent this method
|
1417
|
+
# from interpreting it as a back-reference, and then you need to escape the
|
1418
|
+
# backslashes again to prevent a string literal from consuming them:
|
1419
|
+
# `"..\\\\\\\\&.."`. You may want to use the block form to avoid a lot of
|
1420
|
+
# backslashes.
|
1421
|
+
#
|
1422
|
+
def sub: (Regexp | string pattern, string | Hash[String, String] replacement) -> String
|
1423
|
+
| (Regexp | string pattern) { (String match) -> _ToS } -> String
|
1424
|
+
|
1425
|
+
# Performs the same substitution as String#sub in-place.
|
1426
|
+
#
|
1427
|
+
# Returns `str` if a substitution was performed or `nil` if no substitution was
|
1428
|
+
# performed.
|
1429
|
+
#
|
1430
|
+
def sub!: (Regexp | string pattern, string | Hash[String, String] replacement) -> self?
|
1431
|
+
| (Regexp | string pattern) { (String match) -> _ToS } -> String?
|
1432
|
+
|
1433
|
+
# Returns the successor to *str*. The successor is calculated by incrementing
|
1434
|
+
# characters starting from the rightmost alphanumeric (or the rightmost
|
1435
|
+
# character if there are no alphanumerics) in the string. Incrementing a digit
|
1436
|
+
# always results in another digit, and incrementing a letter results in another
|
1437
|
+
# letter of the same case. Incrementing nonalphanumerics uses the underlying
|
1438
|
+
# character set's collating sequence.
|
1439
|
+
#
|
1440
|
+
# If the increment generates a ``carry,'' the character to the left of it is
|
1441
|
+
# incremented. This process repeats until there is no carry, adding an
|
1442
|
+
# additional character if necessary.
|
1443
|
+
#
|
1444
|
+
# "abcd".succ #=> "abce"
|
1445
|
+
# "THX1138".succ #=> "THX1139"
|
1446
|
+
# "<<koala>>".succ #=> "<<koalb>>"
|
1447
|
+
# "1999zzz".succ #=> "2000aaa"
|
1448
|
+
# "ZZZ9999".succ #=> "AAAA0000"
|
1449
|
+
# "***".succ #=> "**+"
|
1450
|
+
#
|
1451
|
+
def succ: () -> String
|
1452
|
+
|
1453
|
+
# Equivalent to String#succ, but modifies the receiver in place.
|
1454
|
+
#
|
1455
|
+
def succ!: () -> String
|
1456
|
+
|
1457
|
+
# Returns a basic *n*-bit checksum of the characters in *str*, where *n* is the
|
1458
|
+
# optional Integer parameter, defaulting to 16. The result is simply the sum of
|
1459
|
+
# the binary value of each byte in *str* modulo `2**n - 1`. This is not a
|
1460
|
+
# particularly good checksum.
|
1461
|
+
#
|
1462
|
+
def sum: (?int n) -> Integer
|
1463
|
+
|
1464
|
+
# Returns a copy of *str* with uppercase alphabetic characters converted to
|
1465
|
+
# lowercase and lowercase characters converted to uppercase.
|
1466
|
+
#
|
1467
|
+
# See String#downcase for meaning of `options` and use with different encodings.
|
1468
|
+
#
|
1469
|
+
# "Hello".swapcase #=> "hELLO"
|
1470
|
+
# "cYbEr_PuNk11".swapcase #=> "CyBeR_pUnK11"
|
1471
|
+
#
|
1472
|
+
def swapcase: () -> String
|
1473
|
+
| (:ascii | :lithuanian | :turkic) -> String
|
1474
|
+
| (:lithuanian, :turkic) -> String
|
1475
|
+
| (:turkic, :lithuanian) -> String
|
1476
|
+
|
1477
|
+
# Equivalent to String#swapcase, but modifies the receiver in place, returning
|
1478
|
+
# *str*, or `nil` if no changes were made.
|
1479
|
+
#
|
1480
|
+
# See String#downcase for meaning of `options` and use with different encodings.
|
1481
|
+
#
|
1482
|
+
def swapcase!: () -> self?
|
1483
|
+
| (:ascii | :lithuanian | :turkic) -> self?
|
1484
|
+
| (:lithuanian, :turkic) -> self?
|
1485
|
+
| (:turkic, :lithuanian) -> self?
|
1486
|
+
|
1487
|
+
# Returns a complex which denotes the string form. The parser ignores leading
|
1488
|
+
# whitespaces and trailing garbage. Any digit sequences can be separated by an
|
1489
|
+
# underscore. Returns zero for null or garbage string.
|
1490
|
+
#
|
1491
|
+
# '9'.to_c #=> (9+0i)
|
1492
|
+
# '2.5'.to_c #=> (2.5+0i)
|
1493
|
+
# '2.5/1'.to_c #=> ((5/2)+0i)
|
1494
|
+
# '-3/2'.to_c #=> ((-3/2)+0i)
|
1495
|
+
# '-i'.to_c #=> (0-1i)
|
1496
|
+
# '45i'.to_c #=> (0+45i)
|
1497
|
+
# '3-4i'.to_c #=> (3-4i)
|
1498
|
+
# '-4e2-4e-2i'.to_c #=> (-400.0-0.04i)
|
1499
|
+
# '-0.0-0.0i'.to_c #=> (-0.0-0.0i)
|
1500
|
+
# '1/2+3/4i'.to_c #=> ((1/2)+(3/4)*i)
|
1501
|
+
# 'ruby'.to_c #=> (0+0i)
|
1502
|
+
#
|
1503
|
+
# See Kernel.Complex.
|
1504
|
+
#
|
1505
|
+
def to_c: () -> Complex
|
1506
|
+
|
1507
|
+
# Returns the result of interpreting leading characters in *str* as a floating
|
1508
|
+
# point number. Extraneous characters past the end of a valid number are
|
1509
|
+
# ignored. If there is not a valid number at the start of *str*, `0.0` is
|
1510
|
+
# returned. This method never raises an exception.
|
1511
|
+
#
|
1512
|
+
# "123.45e1".to_f #=> 1234.5
|
1513
|
+
# "45.67 degrees".to_f #=> 45.67
|
1514
|
+
# "thx1138".to_f #=> 0.0
|
1515
|
+
#
|
1516
|
+
def to_f: () -> Float
|
1517
|
+
|
1518
|
+
# Returns the result of interpreting leading characters in *str* as an integer
|
1519
|
+
# base *base* (between 2 and 36). Extraneous characters past the end of a valid
|
1520
|
+
# number are ignored. If there is not a valid number at the start of *str*, `0`
|
1521
|
+
# is returned. This method never raises an exception when *base* is valid.
|
1522
|
+
#
|
1523
|
+
# "12345".to_i #=> 12345
|
1524
|
+
# "99 red balloons".to_i #=> 99
|
1525
|
+
# "0a".to_i #=> 0
|
1526
|
+
# "0a".to_i(16) #=> 10
|
1527
|
+
# "hello".to_i #=> 0
|
1528
|
+
# "1100101".to_i(2) #=> 101
|
1529
|
+
# "1100101".to_i(8) #=> 294977
|
1530
|
+
# "1100101".to_i(10) #=> 1100101
|
1531
|
+
# "1100101".to_i(16) #=> 17826049
|
1532
|
+
#
|
1533
|
+
def to_i: (?int base) -> Integer
|
1534
|
+
|
1535
|
+
# Returns the result of interpreting leading characters in `str` as a rational.
|
1536
|
+
# Leading whitespace and extraneous characters past the end of a valid number
|
1537
|
+
# are ignored. Digit sequences can be separated by an underscore. If there is
|
1538
|
+
# not a valid number at the start of `str`, zero is returned. This method never
|
1539
|
+
# raises an exception.
|
1540
|
+
#
|
1541
|
+
# ' 2 '.to_r #=> (2/1)
|
1542
|
+
# '300/2'.to_r #=> (150/1)
|
1543
|
+
# '-9.2'.to_r #=> (-46/5)
|
1544
|
+
# '-9.2e2'.to_r #=> (-920/1)
|
1545
|
+
# '1_234_567'.to_r #=> (1234567/1)
|
1546
|
+
# '21 June 09'.to_r #=> (21/1)
|
1547
|
+
# '21/06/09'.to_r #=> (7/2)
|
1548
|
+
# 'BWV 1079'.to_r #=> (0/1)
|
1549
|
+
#
|
1550
|
+
# NOTE: "0.3".to_r isn't the same as 0.3.to_r. The former is equivalent to
|
1551
|
+
# "3/10".to_r, but the latter isn't so.
|
1552
|
+
#
|
1553
|
+
# "0.3".to_r == 3/10r #=> true
|
1554
|
+
# 0.3.to_r == 3/10r #=> false
|
1555
|
+
#
|
1556
|
+
# See also Kernel#Rational.
|
1557
|
+
#
|
1558
|
+
def to_r: () -> Rational
|
1559
|
+
|
1560
|
+
# Returns `self`.
|
1561
|
+
#
|
1562
|
+
# If called on a subclass of String, converts the receiver to a String object.
|
1563
|
+
#
|
1564
|
+
def to_s: () -> String
|
1565
|
+
|
1566
|
+
# Returns `self`.
|
1567
|
+
#
|
1568
|
+
# If called on a subclass of String, converts the receiver to a String object.
|
1569
|
+
#
|
1570
|
+
def to_str: () -> String
|
1571
|
+
|
1572
|
+
# Returns the Symbol corresponding to *str*, creating the symbol if it did not
|
1573
|
+
# previously exist. See Symbol#id2name.
|
1574
|
+
#
|
1575
|
+
# "Koala".intern #=> :Koala
|
1576
|
+
# s = 'cat'.to_sym #=> :cat
|
1577
|
+
# s == :cat #=> true
|
1578
|
+
# s = '@cat'.to_sym #=> :@cat
|
1579
|
+
# s == :@cat #=> true
|
1580
|
+
#
|
1581
|
+
# This can also be used to create symbols that cannot be represented using the
|
1582
|
+
# `:xxx` notation.
|
1583
|
+
#
|
1584
|
+
# 'cat and dog'.to_sym #=> :"cat and dog"
|
1585
|
+
#
|
1586
|
+
def to_sym: () -> Symbol
|
1587
|
+
|
1588
|
+
# Returns a copy of `str` with the characters in `from_str` replaced by the
|
1589
|
+
# corresponding characters in `to_str`. If `to_str` is shorter than `from_str`,
|
1590
|
+
# it is padded with its last character in order to maintain the correspondence.
|
1591
|
+
#
|
1592
|
+
# "hello".tr('el', 'ip') #=> "hippo"
|
1593
|
+
# "hello".tr('aeiou', '*') #=> "h*ll*"
|
1594
|
+
# "hello".tr('aeiou', 'AA*') #=> "hAll*"
|
1595
|
+
#
|
1596
|
+
# Both strings may use the `c1-c2` notation to denote ranges of characters, and
|
1597
|
+
# `from_str` may start with a `^`, which denotes all characters except those
|
1598
|
+
# listed.
|
1599
|
+
#
|
1600
|
+
# "hello".tr('a-y', 'b-z') #=> "ifmmp"
|
1601
|
+
# "hello".tr('^aeiou', '*') #=> "*e**o"
|
1602
|
+
#
|
1603
|
+
# The backslash character `\` can be used to escape `^` or `-` and is otherwise
|
1604
|
+
# ignored unless it appears at the end of a range or the end of the `from_str`
|
1605
|
+
# or `to_str`:
|
1606
|
+
#
|
1607
|
+
# "hello^world".tr("\\^aeiou", "*") #=> "h*ll**w*rld"
|
1608
|
+
# "hello-world".tr("a\\-eo", "*") #=> "h*ll**w*rld"
|
1609
|
+
#
|
1610
|
+
# "hello\r\nworld".tr("\r", "") #=> "hello\nworld"
|
1611
|
+
# "hello\r\nworld".tr("\\r", "") #=> "hello\r\nwold"
|
1612
|
+
# "hello\r\nworld".tr("\\\r", "") #=> "hello\nworld"
|
1613
|
+
#
|
1614
|
+
# "X['\\b']".tr("X\\", "") #=> "['b']"
|
1615
|
+
# "X['\\b']".tr("X-\\]", "") #=> "'b'"
|
1616
|
+
#
|
1617
|
+
def tr: (string from_str, string to_str) -> String
|
1618
|
+
|
1619
|
+
# Translates *str* in place, using the same rules as String#tr. Returns *str*,
|
1620
|
+
# or `nil` if no changes were made.
|
1621
|
+
#
|
1622
|
+
def tr!: (string from_str, string to_str) -> String?
|
1623
|
+
|
1624
|
+
# Processes a copy of *str* as described under String#tr, then removes duplicate
|
1625
|
+
# characters in regions that were affected by the translation.
|
1626
|
+
#
|
1627
|
+
# "hello".tr_s('l', 'r') #=> "hero"
|
1628
|
+
# "hello".tr_s('el', '*') #=> "h*o"
|
1629
|
+
# "hello".tr_s('el', 'hx') #=> "hhxo"
|
1630
|
+
#
|
1631
|
+
def tr_s: (string from_str, string to_str) -> String
|
1632
|
+
|
1633
|
+
# Performs String#tr_s processing on *str* in place, returning *str*, or `nil`
|
1634
|
+
# if no changes were made.
|
1635
|
+
#
|
1636
|
+
def tr_s!: (string from_str, string to_str) -> String?
|
1637
|
+
|
1638
|
+
# Returns an unescaped version of the string. This does the inverse of
|
1639
|
+
# String#dump.
|
1640
|
+
#
|
1641
|
+
# "\"hello \\n ''\"".undump #=> "hello \n ''"
|
1642
|
+
#
|
1643
|
+
def undump: () -> String
|
1644
|
+
|
1645
|
+
# Unicode Normalization---Returns a normalized form of `str`, using Unicode
|
1646
|
+
# normalizations NFC, NFD, NFKC, or NFKD. The normalization form used is
|
1647
|
+
# determined by `form`, which can be any of the four values `:nfc`, `:nfd`,
|
1648
|
+
# `:nfkc`, or `:nfkd`. The default is `:nfc`.
|
1649
|
+
#
|
1650
|
+
# If the string is not in a Unicode Encoding, then an Exception is raised. In
|
1651
|
+
# this context, 'Unicode Encoding' means any of UTF-8, UTF-16BE/LE, and
|
1652
|
+
# UTF-32BE/LE, as well as GB18030, UCS_2BE, and UCS_4BE. Anything other than
|
1653
|
+
# UTF-8 is implemented by converting to UTF-8, which makes it slower than UTF-8.
|
1654
|
+
#
|
1655
|
+
# "a\u0300".unicode_normalize #=> "\u00E0"
|
1656
|
+
# "a\u0300".unicode_normalize(:nfc) #=> "\u00E0"
|
1657
|
+
# "\u00E0".unicode_normalize(:nfd) #=> "a\u0300"
|
1658
|
+
# "\xE0".force_encoding('ISO-8859-1').unicode_normalize(:nfd)
|
1659
|
+
# #=> Encoding::CompatibilityError raised
|
1660
|
+
#
|
1661
|
+
def unicode_normalize: (?:nfc | :nfd | :nfkc | :nfkd) -> String
|
1662
|
+
|
1663
|
+
# Destructive version of String#unicode_normalize, doing Unicode normalization
|
1664
|
+
# in place.
|
1665
|
+
#
|
1666
|
+
def unicode_normalize!: (?:nfc | :nfd | :nfkc | :nfkd) -> String
|
1667
|
+
|
1668
|
+
# Checks whether `str` is in Unicode normalization form `form`, which can be any
|
1669
|
+
# of the four values `:nfc`, `:nfd`, `:nfkc`, or `:nfkd`. The default is `:nfc`.
|
1670
|
+
#
|
1671
|
+
# If the string is not in a Unicode Encoding, then an Exception is raised. For
|
1672
|
+
# details, see String#unicode_normalize.
|
1673
|
+
#
|
1674
|
+
# "a\u0300".unicode_normalized? #=> false
|
1675
|
+
# "a\u0300".unicode_normalized?(:nfd) #=> true
|
1676
|
+
# "\u00E0".unicode_normalized? #=> true
|
1677
|
+
# "\u00E0".unicode_normalized?(:nfd) #=> false
|
1678
|
+
# "\xE0".force_encoding('ISO-8859-1').unicode_normalized?
|
1679
|
+
# #=> Encoding::CompatibilityError raised
|
1680
|
+
#
|
1681
|
+
def unicode_normalized?: (?:nfc | :nfd | :nfkc | :nfkd) -> bool
|
1682
|
+
|
1683
|
+
# Decodes *str* (which may contain binary data) according to the format string,
|
1684
|
+
# returning an array of each value extracted. The format string consists of a
|
1685
|
+
# sequence of single-character directives, summarized in the table at the end of
|
1686
|
+
# this entry. Each directive may be followed by a number, indicating the number
|
1687
|
+
# of times to repeat with this directive. An asterisk (```*`'') will use up all
|
1688
|
+
# remaining elements. The directives `sSiIlL` may each be followed by an
|
1689
|
+
# underscore (```_`'') or exclamation mark (```!`'') to use the underlying
|
1690
|
+
# platform's native size for the specified type; otherwise, it uses a
|
1691
|
+
# platform-independent consistent size. Spaces are ignored in the format string.
|
1692
|
+
# See also String#unpack1, Array#pack.
|
1693
|
+
#
|
1694
|
+
# "abc \0\0abc \0\0".unpack('A6Z6') #=> ["abc", "abc "]
|
1695
|
+
# "abc \0\0".unpack('a3a3') #=> ["abc", " \000\000"]
|
1696
|
+
# "abc \0abc \0".unpack('Z*Z*') #=> ["abc ", "abc "]
|
1697
|
+
# "aa".unpack('b8B8') #=> ["10000110", "01100001"]
|
1698
|
+
# "aaa".unpack('h2H2c') #=> ["16", "61", 97]
|
1699
|
+
# "\xfe\xff\xfe\xff".unpack('sS') #=> [-2, 65534]
|
1700
|
+
# "now=20is".unpack('M*') #=> ["now is"]
|
1701
|
+
# "whole".unpack('xax2aX2aX1aX2a') #=> ["h", "e", "l", "l", "o"]
|
1702
|
+
#
|
1703
|
+
# This table summarizes the various formats and the Ruby classes returned by
|
1704
|
+
# each.
|
1705
|
+
#
|
1706
|
+
# Integer | |
|
1707
|
+
# Directive | Returns | Meaning
|
1708
|
+
# ------------------------------------------------------------------
|
1709
|
+
# C | Integer | 8-bit unsigned (unsigned char)
|
1710
|
+
# S | Integer | 16-bit unsigned, native endian (uint16_t)
|
1711
|
+
# L | Integer | 32-bit unsigned, native endian (uint32_t)
|
1712
|
+
# Q | Integer | 64-bit unsigned, native endian (uint64_t)
|
1713
|
+
# J | Integer | pointer width unsigned, native endian (uintptr_t)
|
1714
|
+
# | |
|
1715
|
+
# c | Integer | 8-bit signed (signed char)
|
1716
|
+
# s | Integer | 16-bit signed, native endian (int16_t)
|
1717
|
+
# l | Integer | 32-bit signed, native endian (int32_t)
|
1718
|
+
# q | Integer | 64-bit signed, native endian (int64_t)
|
1719
|
+
# j | Integer | pointer width signed, native endian (intptr_t)
|
1720
|
+
# | |
|
1721
|
+
# S_ S! | Integer | unsigned short, native endian
|
1722
|
+
# I I_ I! | Integer | unsigned int, native endian
|
1723
|
+
# L_ L! | Integer | unsigned long, native endian
|
1724
|
+
# Q_ Q! | Integer | unsigned long long, native endian (ArgumentError
|
1725
|
+
# | | if the platform has no long long type.)
|
1726
|
+
# J! | Integer | uintptr_t, native endian (same with J)
|
1727
|
+
# | |
|
1728
|
+
# s_ s! | Integer | signed short, native endian
|
1729
|
+
# i i_ i! | Integer | signed int, native endian
|
1730
|
+
# l_ l! | Integer | signed long, native endian
|
1731
|
+
# q_ q! | Integer | signed long long, native endian (ArgumentError
|
1732
|
+
# | | if the platform has no long long type.)
|
1733
|
+
# j! | Integer | intptr_t, native endian (same with j)
|
1734
|
+
# | |
|
1735
|
+
# S> s> S!> s!> | Integer | same as the directives without ">" except
|
1736
|
+
# L> l> L!> l!> | | big endian
|
1737
|
+
# I!> i!> | |
|
1738
|
+
# Q> q> Q!> q!> | | "S>" is same as "n"
|
1739
|
+
# J> j> J!> j!> | | "L>" is same as "N"
|
1740
|
+
# | |
|
1741
|
+
# S< s< S!< s!< | Integer | same as the directives without "<" except
|
1742
|
+
# L< l< L!< l!< | | little endian
|
1743
|
+
# I!< i!< | |
|
1744
|
+
# Q< q< Q!< q!< | | "S<" is same as "v"
|
1745
|
+
# J< j< J!< j!< | | "L<" is same as "V"
|
1746
|
+
# | |
|
1747
|
+
# n | Integer | 16-bit unsigned, network (big-endian) byte order
|
1748
|
+
# N | Integer | 32-bit unsigned, network (big-endian) byte order
|
1749
|
+
# v | Integer | 16-bit unsigned, VAX (little-endian) byte order
|
1750
|
+
# V | Integer | 32-bit unsigned, VAX (little-endian) byte order
|
1751
|
+
# | |
|
1752
|
+
# U | Integer | UTF-8 character
|
1753
|
+
# w | Integer | BER-compressed integer (see Array.pack)
|
1754
|
+
#
|
1755
|
+
# Float | |
|
1756
|
+
# Directive | Returns | Meaning
|
1757
|
+
# -----------------------------------------------------------------
|
1758
|
+
# D d | Float | double-precision, native format
|
1759
|
+
# F f | Float | single-precision, native format
|
1760
|
+
# E | Float | double-precision, little-endian byte order
|
1761
|
+
# e | Float | single-precision, little-endian byte order
|
1762
|
+
# G | Float | double-precision, network (big-endian) byte order
|
1763
|
+
# g | Float | single-precision, network (big-endian) byte order
|
1764
|
+
#
|
1765
|
+
# String | |
|
1766
|
+
# Directive | Returns | Meaning
|
1767
|
+
# -----------------------------------------------------------------
|
1768
|
+
# A | String | arbitrary binary string (remove trailing nulls and ASCII spaces)
|
1769
|
+
# a | String | arbitrary binary string
|
1770
|
+
# Z | String | null-terminated string
|
1771
|
+
# B | String | bit string (MSB first)
|
1772
|
+
# b | String | bit string (LSB first)
|
1773
|
+
# H | String | hex string (high nibble first)
|
1774
|
+
# h | String | hex string (low nibble first)
|
1775
|
+
# u | String | UU-encoded string
|
1776
|
+
# M | String | quoted-printable, MIME encoding (see RFC2045)
|
1777
|
+
# m | String | base64 encoded string (RFC 2045) (default)
|
1778
|
+
# | | base64 encoded string (RFC 4648) if followed by 0
|
1779
|
+
# P | String | pointer to a structure (fixed-length string)
|
1780
|
+
# p | String | pointer to a null-terminated string
|
1781
|
+
#
|
1782
|
+
# Misc. | |
|
1783
|
+
# Directive | Returns | Meaning
|
1784
|
+
# -----------------------------------------------------------------
|
1785
|
+
# @ | --- | skip to the offset given by the length argument
|
1786
|
+
# X | --- | skip backward one byte
|
1787
|
+
# x | --- | skip forward one byte
|
1788
|
+
#
|
1789
|
+
# HISTORY
|
1790
|
+
#
|
1791
|
+
# * J, J! j, and j! are available since Ruby 2.3.
|
1792
|
+
# * Q_, Q!, q_, and q! are available since Ruby 2.1.
|
1793
|
+
# * I!<, i!<, I!>, and i!> are available since Ruby 1.9.3.
|
1794
|
+
#
|
1795
|
+
#
|
1796
|
+
def unpack: (String format) -> Array[Integer | Float | String | nil]
|
1797
|
+
|
1798
|
+
# Decodes *str* (which may contain binary data) according to the format string,
|
1799
|
+
# returning the first value extracted. See also String#unpack, Array#pack.
|
1800
|
+
#
|
1801
|
+
# Contrast with String#unpack:
|
1802
|
+
#
|
1803
|
+
# "abc \0\0abc \0\0".unpack('A6Z6') #=> ["abc", "abc "]
|
1804
|
+
# "abc \0\0abc \0\0".unpack1('A6Z6') #=> "abc"
|
1805
|
+
#
|
1806
|
+
# In that case data would be lost but often it's the case that the array only
|
1807
|
+
# holds one value, especially when unpacking binary data. For instance:
|
1808
|
+
#
|
1809
|
+
# "xffx00x00x00".unpack("l") #=> [255] "xffx00x00x00".unpack1("l")
|
1810
|
+
# #=> 255
|
1811
|
+
#
|
1812
|
+
# Thus unpack1 is convenient, makes clear the intention and signals the expected
|
1813
|
+
# return value to those reading the code.
|
1814
|
+
#
|
1815
|
+
def unpack1: (String format) -> (Integer | Float | String | nil)
|
1816
|
+
|
1817
|
+
# Returns a copy of *str* with all lowercase letters replaced with their
|
1818
|
+
# uppercase counterparts.
|
1819
|
+
#
|
1820
|
+
# See String#downcase for meaning of `options` and use with different encodings.
|
1821
|
+
#
|
1822
|
+
# "hEllO".upcase #=> "HELLO"
|
1823
|
+
#
|
1824
|
+
def upcase: () -> String
|
1825
|
+
| (:ascii | :lithuanian | :turkic) -> String
|
1826
|
+
| (:lithuanian, :turkic) -> String
|
1827
|
+
| (:turkic, :lithuanian) -> String
|
1828
|
+
|
1829
|
+
# Upcases the contents of *str*, returning `nil` if no changes were made.
|
1830
|
+
#
|
1831
|
+
# See String#downcase for meaning of `options` and use with different encodings.
|
1832
|
+
#
|
1833
|
+
def upcase!: () -> self?
|
1834
|
+
| (:ascii | :lithuanian | :turkic) -> self?
|
1835
|
+
| (:lithuanian, :turkic) -> self?
|
1836
|
+
| (:turkic, :lithuanian) -> self?
|
1837
|
+
|
1838
|
+
# Iterates through successive values, starting at *str* and ending at
|
1839
|
+
# *other_str* inclusive, passing each value in turn to the block. The
|
1840
|
+
# String#succ method is used to generate each value. If optional second
|
1841
|
+
# argument exclusive is omitted or is false, the last value will be included;
|
1842
|
+
# otherwise it will be excluded.
|
1843
|
+
#
|
1844
|
+
# If no block is given, an enumerator is returned instead.
|
1845
|
+
#
|
1846
|
+
# "a8".upto("b6") {|s| print s, ' ' }
|
1847
|
+
# for s in "a8".."b6"
|
1848
|
+
# print s, ' '
|
1849
|
+
# end
|
1850
|
+
#
|
1851
|
+
# *produces:*
|
1852
|
+
#
|
1853
|
+
# a8 a9 b0 b1 b2 b3 b4 b5 b6
|
1854
|
+
# a8 a9 b0 b1 b2 b3 b4 b5 b6
|
1855
|
+
#
|
1856
|
+
# If *str* and *other_str* contains only ascii numeric characters, both are
|
1857
|
+
# recognized as decimal numbers. In addition, the width of string (e.g. leading
|
1858
|
+
# zeros) is handled appropriately.
|
1859
|
+
#
|
1860
|
+
# "9".upto("11").to_a #=> ["9", "10", "11"]
|
1861
|
+
# "25".upto("5").to_a #=> []
|
1862
|
+
# "07".upto("11").to_a #=> ["07", "08", "09", "10", "11"]
|
1863
|
+
#
|
1864
|
+
def upto: (string other_str, ?bool exclusive) -> Enumerator[String, self]
|
1865
|
+
| (string other_str, ?bool exclusive) { (String s) -> void } -> self
|
1866
|
+
|
1867
|
+
# Returns true for a string which is encoded correctly.
|
1868
|
+
#
|
1869
|
+
# "\xc2\xa1".force_encoding("UTF-8").valid_encoding? #=> true
|
1870
|
+
# "\xc2".force_encoding("UTF-8").valid_encoding? #=> false
|
1871
|
+
# "\x80".force_encoding("UTF-8").valid_encoding? #=> false
|
1872
|
+
#
|
1873
|
+
def valid_encoding?: () -> bool
|
1874
|
+
|
1875
|
+
private
|
1876
|
+
|
1877
|
+
# Returns a new string object containing a copy of *str*.
|
1878
|
+
#
|
1879
|
+
# The optional *encoding* keyword argument specifies the encoding of the new
|
1880
|
+
# string. If not specified, the encoding of *str* is used (or ASCII-8BIT, if
|
1881
|
+
# *str* is not specified).
|
1882
|
+
#
|
1883
|
+
# The optional *capacity* keyword argument specifies the size of the internal
|
1884
|
+
# buffer. This may improve performance, when the string will be concatenated
|
1885
|
+
# many times (causing many realloc calls).
|
1886
|
+
#
|
1887
|
+
def initialize: (?string str, ?encoding: encoding, ?capacity: int) -> void
|
1888
|
+
|
1889
|
+
# Replaces the contents of *str* with the corresponding values in *other_str*.
|
1890
|
+
#
|
1891
|
+
# s = "hello" #=> "hello"
|
1892
|
+
# s.replace "world" #=> "world"
|
1893
|
+
#
|
1894
|
+
alias initialize_copy replace
|
1895
|
+
end
|
1896
|
+
|
1897
|
+
interface _ArefFromStringToString
|
1898
|
+
def []: (String) -> String
|
1899
|
+
end
|
1900
|
+
|
1901
|
+
type String::encode_fallback = Hash[String, String] | Proc | Method | _ArefFromStringToString
|