rbs 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +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,267 @@
|
|
|
1
|
+
# Random provides an interface to Ruby's pseudo-random number generator, or
|
|
2
|
+
# PRNG. The PRNG produces a deterministic sequence of bits which approximate
|
|
3
|
+
# true randomness. The sequence may be represented by integers, floats, or
|
|
4
|
+
# binary strings.
|
|
5
|
+
#
|
|
6
|
+
# The generator may be initialized with either a system-generated or
|
|
7
|
+
# user-supplied seed value by using Random.srand.
|
|
8
|
+
#
|
|
9
|
+
# The class method Random.rand provides the base functionality of Kernel.rand
|
|
10
|
+
# along with better handling of floating point values. These are both interfaces
|
|
11
|
+
# to Random::DEFAULT, the Ruby system PRNG.
|
|
12
|
+
#
|
|
13
|
+
# Random.new will create a new PRNG with a state independent of Random::DEFAULT,
|
|
14
|
+
# allowing multiple generators with different seed values or sequence positions
|
|
15
|
+
# to exist simultaneously. Random objects can be marshaled, allowing sequences
|
|
16
|
+
# to be saved and resumed.
|
|
17
|
+
#
|
|
18
|
+
# PRNGs are currently implemented as a modified Mersenne Twister with a period
|
|
19
|
+
# of 2**19937-1.
|
|
20
|
+
#
|
|
21
|
+
class Random < Object
|
|
22
|
+
include Random::Formatter
|
|
23
|
+
|
|
24
|
+
# Returns true if the two generators have the same internal state, otherwise
|
|
25
|
+
# false. Equivalent generators will return the same sequence of pseudo-random
|
|
26
|
+
# numbers. Two generators will generally have the same state only if they were
|
|
27
|
+
# initialized with the same seed
|
|
28
|
+
#
|
|
29
|
+
# Random.new == Random.new # => false
|
|
30
|
+
# Random.new(1234) == Random.new(1234) # => true
|
|
31
|
+
#
|
|
32
|
+
# and have the same invocation history.
|
|
33
|
+
#
|
|
34
|
+
# prng1 = Random.new(1234)
|
|
35
|
+
# prng2 = Random.new(1234)
|
|
36
|
+
# prng1 == prng2 # => true
|
|
37
|
+
#
|
|
38
|
+
# prng1.rand # => 0.1915194503788923
|
|
39
|
+
# prng1 == prng2 # => false
|
|
40
|
+
#
|
|
41
|
+
# prng2.rand # => 0.1915194503788923
|
|
42
|
+
# prng1 == prng2 # => true
|
|
43
|
+
#
|
|
44
|
+
def ==: (untyped arg0) -> bool
|
|
45
|
+
|
|
46
|
+
# Returns a random binary string containing `size` bytes.
|
|
47
|
+
#
|
|
48
|
+
# random_string = Random.new.bytes(10) # => "\xD7:R\xAB?\x83\xCE\xFAkO"
|
|
49
|
+
# random_string.size # => 10
|
|
50
|
+
#
|
|
51
|
+
def bytes: (Integer size) -> String
|
|
52
|
+
|
|
53
|
+
# Creates a new PRNG using `seed` to set the initial state. If `seed` is
|
|
54
|
+
# omitted, the generator is initialized with Random.new_seed.
|
|
55
|
+
#
|
|
56
|
+
# See Random.srand for more information on the use of seed values.
|
|
57
|
+
#
|
|
58
|
+
def initialize: (?Integer seed) -> void
|
|
59
|
+
|
|
60
|
+
# When `max` is an Integer, `rand` returns a random integer greater than or
|
|
61
|
+
# equal to zero and less than `max`. Unlike Kernel.rand, when `max` is a
|
|
62
|
+
# negative integer or zero, `rand` raises an ArgumentError.
|
|
63
|
+
#
|
|
64
|
+
# prng = Random.new
|
|
65
|
+
# prng.rand(100) # => 42
|
|
66
|
+
#
|
|
67
|
+
# When `max` is a Float, `rand` returns a random floating point number between
|
|
68
|
+
# 0.0 and `max`, including 0.0 and excluding `max`.
|
|
69
|
+
#
|
|
70
|
+
# prng.rand(1.5) # => 1.4600282860034115
|
|
71
|
+
#
|
|
72
|
+
# When `max` is a Range, `rand` returns a random number where
|
|
73
|
+
# range.member?(number) == true.
|
|
74
|
+
#
|
|
75
|
+
# prng.rand(5..9) # => one of [5, 6, 7, 8, 9]
|
|
76
|
+
# prng.rand(5...9) # => one of [5, 6, 7, 8]
|
|
77
|
+
# prng.rand(5.0..9.0) # => between 5.0 and 9.0, including 9.0
|
|
78
|
+
# prng.rand(5.0...9.0) # => between 5.0 and 9.0, excluding 9.0
|
|
79
|
+
#
|
|
80
|
+
# Both the beginning and ending values of the range must respond to subtract
|
|
81
|
+
# (`-`) and add (`+`)methods, or rand will raise an ArgumentError.
|
|
82
|
+
#
|
|
83
|
+
def rand: () -> Float
|
|
84
|
+
| (Integer | ::Range[Integer] max) -> Integer
|
|
85
|
+
| (Float | ::Range[Float] max) -> Float
|
|
86
|
+
|
|
87
|
+
# Returns the seed value used to initialize the generator. This may be used to
|
|
88
|
+
# initialize another generator with the same state at a later time, causing it
|
|
89
|
+
# to produce the same sequence of numbers.
|
|
90
|
+
#
|
|
91
|
+
# prng1 = Random.new(1234)
|
|
92
|
+
# prng1.seed #=> 1234
|
|
93
|
+
# prng1.rand(100) #=> 47
|
|
94
|
+
#
|
|
95
|
+
# prng2 = Random.new(prng1.seed)
|
|
96
|
+
# prng2.rand(100) #=> 47
|
|
97
|
+
#
|
|
98
|
+
def seed: () -> Integer
|
|
99
|
+
|
|
100
|
+
# Returns an arbitrary seed value. This is used by Random.new when no seed value
|
|
101
|
+
# is specified as an argument.
|
|
102
|
+
#
|
|
103
|
+
# Random.new_seed #=> 115032730400174366788466674494640623225
|
|
104
|
+
#
|
|
105
|
+
def self.new_seed: () -> Integer
|
|
106
|
+
|
|
107
|
+
# Alias of Random::DEFAULT.rand.
|
|
108
|
+
#
|
|
109
|
+
def self.rand: (?Integer max) -> Numeric
|
|
110
|
+
|
|
111
|
+
# Seeds the system pseudo-random number generator, Random::DEFAULT, with
|
|
112
|
+
# `number`. The previous seed value is returned.
|
|
113
|
+
#
|
|
114
|
+
# If `number` is omitted, seeds the generator using a source of entropy provided
|
|
115
|
+
# by the operating system, if available (/dev/urandom on Unix systems or the RSA
|
|
116
|
+
# cryptographic provider on Windows), which is then combined with the time, the
|
|
117
|
+
# process id, and a sequence number.
|
|
118
|
+
#
|
|
119
|
+
# srand may be used to ensure repeatable sequences of pseudo-random numbers
|
|
120
|
+
# between different runs of the program. By setting the seed to a known value,
|
|
121
|
+
# programs can be made deterministic during testing.
|
|
122
|
+
#
|
|
123
|
+
# srand 1234 # => 268519324636777531569100071560086917274
|
|
124
|
+
# [ rand, rand ] # => [0.1915194503788923, 0.6221087710398319]
|
|
125
|
+
# [ rand(10), rand(1000) ] # => [4, 664]
|
|
126
|
+
# srand 1234 # => 1234
|
|
127
|
+
# [ rand, rand ] # => [0.1915194503788923, 0.6221087710398319]
|
|
128
|
+
#
|
|
129
|
+
def self.srand: (?Integer number) -> Numeric
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
# The default Pseudorandom number generator. Used by class methods of Random.
|
|
133
|
+
#
|
|
134
|
+
#
|
|
135
|
+
Random::DEFAULT: Random
|
|
136
|
+
|
|
137
|
+
# Format raw random number as Random does
|
|
138
|
+
#
|
|
139
|
+
#
|
|
140
|
+
module Random::Formatter
|
|
141
|
+
# SecureRandom.base64 generates a random base64 string.
|
|
142
|
+
#
|
|
143
|
+
# The argument *n* specifies the length, in bytes, of the random number to be
|
|
144
|
+
# generated. The length of the result string is about 4/3 of *n*.
|
|
145
|
+
#
|
|
146
|
+
# If *n* is not specified or is nil, 16 is assumed. It may be larger in the
|
|
147
|
+
# future.
|
|
148
|
+
#
|
|
149
|
+
# The result may contain A-Z, a-z, 0-9, "+", "/" and "=".
|
|
150
|
+
#
|
|
151
|
+
# require 'securerandom'
|
|
152
|
+
#
|
|
153
|
+
# SecureRandom.base64 #=> "/2BuBuLf3+WfSKyQbRcc/A=="
|
|
154
|
+
# SecureRandom.base64 #=> "6BbW0pxO0YENxn38HMUbcQ=="
|
|
155
|
+
#
|
|
156
|
+
# If a secure random number generator is not available, `NotImplementedError` is
|
|
157
|
+
# raised.
|
|
158
|
+
#
|
|
159
|
+
# See RFC 3548 for the definition of base64.
|
|
160
|
+
#
|
|
161
|
+
def base64: (?Integer? n) -> String
|
|
162
|
+
|
|
163
|
+
# SecureRandom.hex generates a random hexadecimal string.
|
|
164
|
+
#
|
|
165
|
+
# The argument *n* specifies the length, in bytes, of the random number to be
|
|
166
|
+
# generated. The length of the resulting hexadecimal string is twice of *n*.
|
|
167
|
+
#
|
|
168
|
+
# If *n* is not specified or is nil, 16 is assumed. It may be larger in the
|
|
169
|
+
# future.
|
|
170
|
+
#
|
|
171
|
+
# The result may contain 0-9 and a-f.
|
|
172
|
+
#
|
|
173
|
+
# require 'securerandom'
|
|
174
|
+
#
|
|
175
|
+
# SecureRandom.hex #=> "eb693ec8252cd630102fd0d0fb7c3485"
|
|
176
|
+
# SecureRandom.hex #=> "91dc3bfb4de5b11d029d376634589b61"
|
|
177
|
+
#
|
|
178
|
+
# If a secure random number generator is not available, `NotImplementedError` is
|
|
179
|
+
# raised.
|
|
180
|
+
#
|
|
181
|
+
def hex: (?Integer? n) -> String
|
|
182
|
+
|
|
183
|
+
# Generates formatted random number from raw random bytes. See Random#rand.
|
|
184
|
+
#
|
|
185
|
+
def rand: () -> Float
|
|
186
|
+
| (?Float? n) -> Float
|
|
187
|
+
| (?Integer? n) -> Integer
|
|
188
|
+
| (?Numeric? n) -> Numeric
|
|
189
|
+
| (?::Range[Float]? n) -> Float
|
|
190
|
+
| (?::Range[Integer]? n) -> Integer
|
|
191
|
+
| (?::Range[Numeric]? n) -> Numeric
|
|
192
|
+
|
|
193
|
+
# SecureRandom.random_bytes generates a random binary string.
|
|
194
|
+
#
|
|
195
|
+
# The argument *n* specifies the length of the result string.
|
|
196
|
+
#
|
|
197
|
+
# If *n* is not specified or is nil, 16 is assumed. It may be larger in future.
|
|
198
|
+
#
|
|
199
|
+
# The result may contain any byte: "x00" - "xff".
|
|
200
|
+
#
|
|
201
|
+
# require 'securerandom'
|
|
202
|
+
#
|
|
203
|
+
# SecureRandom.random_bytes #=> "\xD8\\\xE0\xF4\r\xB2\xFC*WM\xFF\x83\x18\xF45\xB6"
|
|
204
|
+
# SecureRandom.random_bytes #=> "m\xDC\xFC/\a\x00Uf\xB2\xB2P\xBD\xFF6S\x97"
|
|
205
|
+
#
|
|
206
|
+
# If a secure random number generator is not available, `NotImplementedError` is
|
|
207
|
+
# raised.
|
|
208
|
+
#
|
|
209
|
+
def random_bytes: (?Integer? n) -> String
|
|
210
|
+
|
|
211
|
+
# Generates formatted random number from raw random bytes. See Random#rand.
|
|
212
|
+
#
|
|
213
|
+
def random_number: () -> Float
|
|
214
|
+
| (?Float? n) -> Float
|
|
215
|
+
| (?Integer? n) -> Integer
|
|
216
|
+
| (?Numeric? n) -> Numeric
|
|
217
|
+
| (?::Range[Float]? n) -> Float
|
|
218
|
+
| (?::Range[Integer]? n) -> Integer
|
|
219
|
+
| (?::Range[Numeric]? n) -> Numeric
|
|
220
|
+
|
|
221
|
+
# SecureRandom.urlsafe_base64 generates a random URL-safe base64 string.
|
|
222
|
+
#
|
|
223
|
+
# The argument *n* specifies the length, in bytes, of the random number to be
|
|
224
|
+
# generated. The length of the result string is about 4/3 of *n*.
|
|
225
|
+
#
|
|
226
|
+
# If *n* is not specified or is nil, 16 is assumed. It may be larger in the
|
|
227
|
+
# future.
|
|
228
|
+
#
|
|
229
|
+
# The boolean argument *padding* specifies the padding. If it is false or nil,
|
|
230
|
+
# padding is not generated. Otherwise padding is generated. By default, padding
|
|
231
|
+
# is not generated because "=" may be used as a URL delimiter.
|
|
232
|
+
#
|
|
233
|
+
# The result may contain A-Z, a-z, 0-9, "-" and "_". "=" is also used if
|
|
234
|
+
# *padding* is true.
|
|
235
|
+
#
|
|
236
|
+
# require 'securerandom'
|
|
237
|
+
#
|
|
238
|
+
# SecureRandom.urlsafe_base64 #=> "b4GOKm4pOYU_-BOXcrUGDg"
|
|
239
|
+
# SecureRandom.urlsafe_base64 #=> "UZLdOkzop70Ddx-IJR0ABg"
|
|
240
|
+
#
|
|
241
|
+
# SecureRandom.urlsafe_base64(nil, true) #=> "i0XQ-7gglIsHGV2_BNPrdQ=="
|
|
242
|
+
# SecureRandom.urlsafe_base64(nil, true) #=> "-M8rLhr7JEpJlqFGUMmOxg=="
|
|
243
|
+
#
|
|
244
|
+
# If a secure random number generator is not available, `NotImplementedError` is
|
|
245
|
+
# raised.
|
|
246
|
+
#
|
|
247
|
+
# See RFC 3548 for the definition of URL-safe base64.
|
|
248
|
+
#
|
|
249
|
+
def urlsafe_base64: (?Integer? n, ?bool padding) -> String
|
|
250
|
+
|
|
251
|
+
# SecureRandom.uuid generates a random v4 UUID (Universally Unique IDentifier).
|
|
252
|
+
#
|
|
253
|
+
# require 'securerandom'
|
|
254
|
+
#
|
|
255
|
+
# SecureRandom.uuid #=> "2d931510-d99f-494a-8c67-87feb05e1594"
|
|
256
|
+
# SecureRandom.uuid #=> "bad85eb9-0713-4da7-8d36-07a8e4b00eab"
|
|
257
|
+
# SecureRandom.uuid #=> "62936e70-1815-439b-bf89-8492855a7e6b"
|
|
258
|
+
#
|
|
259
|
+
# The version 4 UUID is purely random (except the version). It doesn't contain
|
|
260
|
+
# meaningful information such as MAC addresses, timestamps, etc.
|
|
261
|
+
#
|
|
262
|
+
# The result contains 122 random bits (15.25 random bytes).
|
|
263
|
+
#
|
|
264
|
+
# See RFC 4122 for details of UUID.
|
|
265
|
+
#
|
|
266
|
+
def uuid: () -> String
|
|
267
|
+
end
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
# A `Range` represents an interval—a set of values with a beginning and an
|
|
2
|
+
# end. Ranges may be constructed using the *s* `..` *e* and *s* `...` *e*
|
|
3
|
+
# literals, or with [::new](Range#method-c-new).
|
|
4
|
+
# Ranges constructed using `..` run from the beginning to the end
|
|
5
|
+
# inclusively. Those created using `...` exclude the end value. When used
|
|
6
|
+
# as an iterator, ranges return each value in the sequence.
|
|
7
|
+
#
|
|
8
|
+
# ```ruby
|
|
9
|
+
# (-1..-5).to_a #=> []
|
|
10
|
+
# (-5..-1).to_a #=> [-5, -4, -3, -2, -1]
|
|
11
|
+
# ('a'..'e').to_a #=> ["a", "b", "c", "d", "e"]
|
|
12
|
+
# ('a'...'e').to_a #=> ["a", "b", "c", "d"]
|
|
13
|
+
# ```
|
|
14
|
+
#
|
|
15
|
+
#
|
|
16
|
+
# An “endless range” represents a semi-infinite range. Literal notation
|
|
17
|
+
# for an endless range is:
|
|
18
|
+
#
|
|
19
|
+
# (1..)
|
|
20
|
+
# # or similarly
|
|
21
|
+
# (1...)
|
|
22
|
+
#
|
|
23
|
+
# Which is equivalent to
|
|
24
|
+
#
|
|
25
|
+
# ```ruby
|
|
26
|
+
# (1..nil) # or similarly (1...nil)
|
|
27
|
+
# Range.new(1, nil) # or Range.new(1, nil, true)
|
|
28
|
+
# ```
|
|
29
|
+
#
|
|
30
|
+
# Endless ranges are useful, for example, for idiomatic slicing of arrays:
|
|
31
|
+
#
|
|
32
|
+
# [1, 2, 3, 4, 5][2...] # => [3, 4, 5]
|
|
33
|
+
#
|
|
34
|
+
# Some implementation details:
|
|
35
|
+
#
|
|
36
|
+
# - `end` of endless range is `nil` ;
|
|
37
|
+
#
|
|
38
|
+
# - `each` of endless range enumerates infinite sequence (may be useful
|
|
39
|
+
# in combination with
|
|
40
|
+
# [Enumerable\#take\_while](https://ruby-doc.org/core-2.6.3/Enumerable.html#method-i-take_while)
|
|
41
|
+
# or similar methods);
|
|
42
|
+
#
|
|
43
|
+
# - `(1..)` and `(1...)` are not equal, although technically
|
|
44
|
+
# representing the same sequence.
|
|
45
|
+
#
|
|
46
|
+
#
|
|
47
|
+
# Ranges can be constructed using any objects that can be compared using
|
|
48
|
+
# the `<=>` operator. Methods that treat the range as a sequence (\#each
|
|
49
|
+
# and methods inherited from
|
|
50
|
+
# [Enumerable](https://ruby-doc.org/core-2.6.3/Enumerable.html) ) expect
|
|
51
|
+
# the begin object to implement a `succ` method to return the next object
|
|
52
|
+
# in sequence. The [step](Range#method-i-step) and
|
|
53
|
+
# [include?](Range#method-i-include-3F) methods
|
|
54
|
+
# require the begin object to implement `succ` or to be numeric.
|
|
55
|
+
#
|
|
56
|
+
# In the `Xs` class below both `<=>` and `succ` are implemented so `Xs`
|
|
57
|
+
# can be used to construct ranges. Note that the
|
|
58
|
+
# [Comparable](https://ruby-doc.org/core-2.6.3/Comparable.html) module is
|
|
59
|
+
# included so the `==` method is defined in terms of `<=>` .
|
|
60
|
+
#
|
|
61
|
+
# ```ruby
|
|
62
|
+
# class Xs # represent a string of 'x's
|
|
63
|
+
# include Comparable
|
|
64
|
+
# attr :length
|
|
65
|
+
# def initialize(n)
|
|
66
|
+
# @length = n
|
|
67
|
+
# end
|
|
68
|
+
# def succ
|
|
69
|
+
# Xs.new(@length + 1)
|
|
70
|
+
# end
|
|
71
|
+
# def <=>(other)
|
|
72
|
+
# @length <=> other.length
|
|
73
|
+
# end
|
|
74
|
+
# def to_s
|
|
75
|
+
# sprintf "%2d #{inspect}", @length
|
|
76
|
+
# end
|
|
77
|
+
# def inspect
|
|
78
|
+
# 'x' * @length
|
|
79
|
+
# end
|
|
80
|
+
# end
|
|
81
|
+
# ```
|
|
82
|
+
#
|
|
83
|
+
# An example of using `Xs` to construct a range:
|
|
84
|
+
#
|
|
85
|
+
# ```ruby
|
|
86
|
+
# r = Xs.new(3)..Xs.new(6) #=> xxx..xxxxxx
|
|
87
|
+
# r.to_a #=> [xxx, xxxx, xxxxx, xxxxxx]
|
|
88
|
+
# r.member?(Xs.new(5)) #=> true
|
|
89
|
+
# ```
|
|
90
|
+
class Range[Elem] < Object
|
|
91
|
+
include Enumerable[Elem, Range[Elem]]
|
|
92
|
+
|
|
93
|
+
def self.new: [U] (U from, U to, ?bool exclude_end) -> ::Range[U]
|
|
94
|
+
|
|
95
|
+
def ==: (untyped obj) -> bool
|
|
96
|
+
|
|
97
|
+
def ===: (untyped obj) -> bool
|
|
98
|
+
|
|
99
|
+
# Returns the object that defines the beginning of the range.
|
|
100
|
+
#
|
|
101
|
+
# ```ruby
|
|
102
|
+
# (1..10).begin #=> 1
|
|
103
|
+
# ```
|
|
104
|
+
def begin: () -> Elem
|
|
105
|
+
|
|
106
|
+
def bsearch: [U] () { (Elem arg0) -> bool } -> U?
|
|
107
|
+
|
|
108
|
+
def cover?: (untyped obj) -> bool
|
|
109
|
+
|
|
110
|
+
def each: () { (Elem arg0) -> untyped } -> self
|
|
111
|
+
| () -> ::Enumerator[Elem, self]
|
|
112
|
+
|
|
113
|
+
# Returns the object that defines the end of the range.
|
|
114
|
+
#
|
|
115
|
+
# ```ruby
|
|
116
|
+
# (1..10).end #=> 10
|
|
117
|
+
# (1...10).end #=> 10
|
|
118
|
+
# ```
|
|
119
|
+
def `end`: () -> Elem
|
|
120
|
+
|
|
121
|
+
# Returns `true` if the range excludes its end value.
|
|
122
|
+
#
|
|
123
|
+
# ```ruby
|
|
124
|
+
# (1..5).exclude_end? #=> false
|
|
125
|
+
# (1...5).exclude_end? #=> true
|
|
126
|
+
# ```
|
|
127
|
+
def `exclude_end?`: () -> bool
|
|
128
|
+
|
|
129
|
+
# Returns the first object in the range, or an array of the first `n`
|
|
130
|
+
# elements.
|
|
131
|
+
#
|
|
132
|
+
# ```ruby
|
|
133
|
+
# (10..20).first #=> 10
|
|
134
|
+
# (10..20).first(3) #=> [10, 11, 12]
|
|
135
|
+
# ```
|
|
136
|
+
def first: () -> Elem
|
|
137
|
+
| (?Integer n) -> ::Array[Elem]
|
|
138
|
+
|
|
139
|
+
# Compute a hash-code for this range. Two ranges with equal begin and end
|
|
140
|
+
# points (using `eql?` ), and the same
|
|
141
|
+
# [exclude\_end?](Range.downloaded.ruby_doc#method-i-exclude_end-3F) value
|
|
142
|
+
# will generate the same hash-code.
|
|
143
|
+
#
|
|
144
|
+
# See also Object\#hash.
|
|
145
|
+
def hash: () -> Integer
|
|
146
|
+
|
|
147
|
+
def `include?`: (untyped obj) -> bool
|
|
148
|
+
|
|
149
|
+
def initialize: (Elem _begin, Elem _end, ?bool exclude_end) -> void
|
|
150
|
+
|
|
151
|
+
# Convert this range object to a printable form (using `inspect` to
|
|
152
|
+
# convert the begin and end objects).
|
|
153
|
+
def inspect: () -> String
|
|
154
|
+
|
|
155
|
+
# Returns the last object in the range, or an array of the last `n`
|
|
156
|
+
# elements.
|
|
157
|
+
#
|
|
158
|
+
# Note that with no arguments `last` will return the object that defines
|
|
159
|
+
# the end of the range even if
|
|
160
|
+
# [exclude\_end?](Range.downloaded.ruby_doc#method-i-exclude_end-3F) is
|
|
161
|
+
# `true` .
|
|
162
|
+
#
|
|
163
|
+
# ```ruby
|
|
164
|
+
# (10..20).last #=> 20
|
|
165
|
+
# (10...20).last #=> 20
|
|
166
|
+
# (10..20).last(3) #=> [18, 19, 20]
|
|
167
|
+
# (10...20).last(3) #=> [17, 18, 19]
|
|
168
|
+
# ```
|
|
169
|
+
def last: () -> Elem
|
|
170
|
+
| (?Integer n) -> ::Array[Elem]
|
|
171
|
+
|
|
172
|
+
# Returns the maximum value in the range. Returns `nil` if the begin value
|
|
173
|
+
# of the range larger than the end value. Returns `nil` if the begin value
|
|
174
|
+
# of an exclusive range is equal to the end value.
|
|
175
|
+
#
|
|
176
|
+
# Can be given an optional block to override the default comparison method
|
|
177
|
+
# `a <=> b` .
|
|
178
|
+
#
|
|
179
|
+
# ```ruby
|
|
180
|
+
# (10..20).max #=> 20
|
|
181
|
+
# ```
|
|
182
|
+
def max: () -> Elem
|
|
183
|
+
| () { (Elem arg0, Elem arg1) -> Integer } -> Elem
|
|
184
|
+
| (?Integer n) -> ::Array[Elem]
|
|
185
|
+
| (?Integer n) { (Elem arg0, Elem arg1) -> Integer } -> ::Array[Elem]
|
|
186
|
+
|
|
187
|
+
# Returns the minimum value in the range. Returns `nil` if the begin value
|
|
188
|
+
# of the range is larger than the end value. Returns `nil` if the begin
|
|
189
|
+
# value of an exclusive range is equal to the end value.
|
|
190
|
+
#
|
|
191
|
+
# Can be given an optional block to override the default comparison method
|
|
192
|
+
# `a <=> b` .
|
|
193
|
+
#
|
|
194
|
+
# ```ruby
|
|
195
|
+
# (10..20).min #=> 10
|
|
196
|
+
# ```
|
|
197
|
+
def min: () -> Elem
|
|
198
|
+
| () { (Elem arg0, Elem arg1) -> Integer } -> Elem
|
|
199
|
+
| (?Integer n) -> ::Array[Elem]
|
|
200
|
+
| (?Integer n) { (Elem arg0, Elem arg1) -> Integer } -> ::Array[Elem]
|
|
201
|
+
|
|
202
|
+
# Returns the number of elements in the range. Both the begin and the end
|
|
203
|
+
# of the [Range](Range.downloaded.ruby_doc) must be
|
|
204
|
+
# [Numeric](https://ruby-doc.org/core-2.6.3/Numeric.html), otherwise nil
|
|
205
|
+
# is returned.
|
|
206
|
+
#
|
|
207
|
+
# ```ruby
|
|
208
|
+
# (10..20).size #=> 11
|
|
209
|
+
# ('a'..'z').size #=> nil
|
|
210
|
+
# (-Float::INFINITY..Float::INFINITY).size #=> Infinity
|
|
211
|
+
# ```
|
|
212
|
+
def size: () -> Integer?
|
|
213
|
+
| () -> Float?
|
|
214
|
+
|
|
215
|
+
def step: (?Integer n) { (Elem arg0) -> untyped } -> self
|
|
216
|
+
| (?Integer n) -> ::Enumerator[Elem, void]
|
|
217
|
+
|
|
218
|
+
# Convert this range object to a printable form (using
|
|
219
|
+
# [to\_s](Range.downloaded.ruby_doc#method-i-to_s) to convert the begin
|
|
220
|
+
# and end objects).
|
|
221
|
+
def to_s: () -> String
|
|
222
|
+
|
|
223
|
+
def eql?: (untyped obj) -> bool
|
|
224
|
+
|
|
225
|
+
def member?: (untyped obj) -> bool
|
|
226
|
+
end
|