steep 0.11.1 → 0.12.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 +4 -4
- data/.github/workflows/ruby.yml +27 -0
- data/.gitmodules +3 -0
- data/CHANGELOG.md +5 -0
- data/README.md +48 -90
- data/Rakefile +10 -6
- data/Steepfile +1 -0
- data/bin/setup +1 -0
- data/bin/smoke_runner.rb +9 -14
- data/exe/rbs +3 -0
- data/exe/ruby-signature +3 -0
- data/exe/steep +1 -0
- data/lib/steep.rb +32 -26
- data/lib/steep/annotation_parser.rb +167 -0
- data/lib/steep/ast/annotation/collection.rb +7 -7
- data/lib/steep/ast/types.rb +60 -0
- data/lib/steep/ast/types/any.rb +1 -1
- data/lib/steep/ast/types/factory.rb +535 -0
- data/lib/steep/ast/types/name.rb +3 -3
- data/lib/steep/ast/types/var.rb +1 -1
- data/lib/steep/cli.rb +56 -240
- data/lib/steep/drivers/annotations.rb +36 -19
- data/lib/steep/drivers/check.rb +55 -91
- data/lib/steep/drivers/init.rb +54 -0
- data/lib/steep/drivers/langserver.rb +241 -150
- data/lib/steep/drivers/print_project.rb +56 -0
- data/lib/steep/drivers/signature_error_printer.rb +25 -0
- data/lib/steep/drivers/trace_printer.rb +25 -0
- data/lib/steep/drivers/utils/driver_helper.rb +26 -0
- data/lib/steep/drivers/validate.rb +18 -38
- data/lib/steep/drivers/vendor.rb +46 -0
- data/lib/steep/drivers/watch.rb +78 -140
- data/lib/steep/errors.rb +22 -13
- data/lib/steep/interface/interface.rb +91 -0
- data/lib/steep/interface/method.rb +0 -4
- data/lib/steep/interface/method_type.rb +362 -2
- data/lib/steep/interface/substitution.rb +22 -0
- data/lib/steep/project.rb +25 -233
- data/lib/steep/project/dsl.rb +132 -0
- data/lib/steep/project/file.rb +93 -76
- data/lib/steep/project/file_loader.rb +63 -0
- data/lib/steep/project/options.rb +7 -0
- data/lib/steep/project/target.rb +190 -0
- data/lib/steep/signature/errors.rb +25 -77
- data/lib/steep/signature/validator.rb +122 -0
- data/lib/steep/source.rb +12 -7
- data/lib/steep/subtyping/check.rb +357 -633
- data/lib/steep/subtyping/constraints.rb +2 -2
- data/lib/steep/subtyping/trace.rb +23 -0
- data/lib/steep/type_construction.rb +509 -455
- data/lib/steep/type_inference/constant_env.rb +16 -24
- data/lib/steep/type_inference/type_env.rb +26 -18
- data/lib/steep/version.rb +1 -1
- data/sample/Steepfile +6 -0
- data/sample/lib/conference.rb +12 -0
- data/sample/sig/conference.rbs +6 -0
- data/smoke/alias/Steepfile +4 -0
- data/smoke/alias/a.rb +2 -2
- data/smoke/alias/{a.rbi → a.rbs} +1 -1
- data/smoke/and/Steepfile +4 -0
- data/smoke/array/Steepfile +4 -0
- data/smoke/array/a.rb +2 -2
- data/smoke/array/b.rb +4 -4
- data/smoke/array/c.rb +2 -2
- data/smoke/block/Steepfile +5 -0
- data/smoke/block/{a.rbi → a.rbs} +1 -1
- data/smoke/block/{c.rbi → c.rbs} +0 -0
- data/smoke/block/d.rb +6 -6
- data/smoke/case/Steepfile +4 -0
- data/smoke/case/a.rb +4 -3
- data/smoke/class/Steepfile +4 -0
- data/smoke/class/a.rb +1 -4
- data/smoke/class/a.rbs +24 -0
- data/smoke/class/h.rb +6 -2
- data/smoke/class/{h.rbi → h.rbs} +1 -2
- data/smoke/class/i.rb +1 -2
- data/smoke/class/i.rbs +9 -0
- data/smoke/const/Steepfile +4 -0
- data/smoke/dstr/Steepfile +4 -0
- data/smoke/ensure/Steepfile +4 -0
- data/smoke/ensure/a.rb +1 -1
- data/smoke/enumerator/Steepfile +4 -0
- data/smoke/enumerator/a.rb +7 -7
- data/smoke/enumerator/b.rb +6 -6
- data/smoke/extension/Steepfile +4 -0
- data/smoke/extension/{a.rbi → a.rbs} +2 -2
- data/smoke/extension/{e.rbi → e.rbs} +2 -2
- data/smoke/hash/Steepfile +4 -0
- data/smoke/hash/{a.rbi → a.rbs} +0 -0
- data/smoke/hash/b.rb +2 -2
- data/smoke/hash/c.rb +1 -1
- data/smoke/hash/e.rbs +3 -0
- data/smoke/hash/f.rb +1 -1
- data/smoke/hello/Steepfile +4 -0
- data/smoke/hello/hello.rbs +7 -0
- data/smoke/if/Steepfile +4 -0
- data/smoke/implements/Steepfile +4 -0
- data/smoke/implements/a.rbs +6 -0
- data/smoke/initialize/Steepfile +4 -0
- data/smoke/initialize/a.rbs +3 -0
- data/smoke/integer/Steepfile +4 -0
- data/smoke/integer/a.rb +5 -3
- data/smoke/interface/Steepfile +4 -0
- data/smoke/interface/{a.rbi → a.rbs} +0 -0
- data/smoke/kwbegin/Steepfile +4 -0
- data/smoke/lambda/Steepfile +4 -0
- data/smoke/lambda/a.rb +9 -2
- data/smoke/literal/Steepfile +4 -0
- data/smoke/literal/{literal_methods.rbi → literal_methods.rbs} +0 -0
- data/smoke/map/Steepfile +4 -0
- data/smoke/map/a.rb +1 -1
- data/smoke/method/Steepfile +4 -0
- data/smoke/method/{a.rbi → a.rbs} +0 -0
- data/smoke/method/b.rb +1 -4
- data/smoke/method/d.rb +1 -0
- data/smoke/method/d.rbs +3 -0
- data/smoke/module/Steepfile +4 -0
- data/smoke/module/a.rb +1 -1
- data/smoke/module/a.rbs +16 -0
- data/smoke/module/c.rb +1 -1
- data/smoke/regexp/Steepfile +4 -0
- data/smoke/regexp/a.rb +2 -2
- data/smoke/regexp/b.rb +16 -16
- data/smoke/regression/Steepfile +5 -0
- data/smoke/regression/array.rb +2 -2
- data/smoke/regression/hash.rb +2 -2
- data/smoke/regression/poly_new.rb +2 -0
- data/smoke/regression/poly_new.rbs +4 -0
- data/smoke/regression/set_divide.rb +2 -2
- data/smoke/rescue/Steepfile +4 -0
- data/smoke/rescue/a.rb +1 -1
- data/smoke/self/Steepfile +4 -0
- data/smoke/self/a.rbs +4 -0
- data/smoke/skip/Steepfile +4 -0
- data/smoke/stdout/Steepfile +4 -0
- data/smoke/stdout/{a.rbi → a.rbs} +1 -1
- data/smoke/super/Steepfile +4 -0
- data/smoke/super/a.rbs +10 -0
- data/smoke/type_case/Steepfile +4 -0
- data/smoke/type_case/a.rb +1 -1
- data/smoke/yield/Steepfile +4 -0
- data/smoke/yield/a.rb +2 -2
- data/steep.gemspec +14 -7
- data/vendor/ruby-signature/.github/workflows/ruby.yml +27 -0
- data/vendor/ruby-signature/.gitignore +12 -0
- data/vendor/ruby-signature/.rubocop.yml +15 -0
- data/vendor/ruby-signature/BSDL +22 -0
- data/vendor/ruby-signature/COPYING +56 -0
- data/vendor/ruby-signature/Gemfile +6 -0
- data/vendor/ruby-signature/README.md +93 -0
- data/vendor/ruby-signature/Rakefile +66 -0
- data/vendor/ruby-signature/bin/annotate-with-rdoc +156 -0
- data/vendor/ruby-signature/bin/console +14 -0
- data/vendor/ruby-signature/bin/query-rdoc +103 -0
- data/vendor/ruby-signature/bin/setup +10 -0
- data/vendor/ruby-signature/bin/sort +88 -0
- data/vendor/ruby-signature/bin/test_runner.rb +17 -0
- data/vendor/ruby-signature/docs/CONTRIBUTING.md +97 -0
- data/vendor/ruby-signature/docs/sigs.md +148 -0
- data/vendor/ruby-signature/docs/stdlib.md +152 -0
- data/vendor/ruby-signature/docs/syntax.md +528 -0
- data/vendor/ruby-signature/exe/rbs +3 -0
- data/vendor/ruby-signature/exe/ruby-signature +7 -0
- data/vendor/ruby-signature/lib/ruby/signature.rb +64 -0
- data/vendor/ruby-signature/lib/ruby/signature/ast/annotation.rb +29 -0
- data/vendor/ruby-signature/lib/ruby/signature/ast/comment.rb +29 -0
- data/vendor/ruby-signature/lib/ruby/signature/ast/declarations.rb +391 -0
- data/vendor/ruby-signature/lib/ruby/signature/ast/members.rb +364 -0
- data/vendor/ruby-signature/lib/ruby/signature/buffer.rb +52 -0
- data/vendor/ruby-signature/lib/ruby/signature/builtin_names.rb +54 -0
- data/vendor/ruby-signature/lib/ruby/signature/cli.rb +534 -0
- data/vendor/ruby-signature/lib/ruby/signature/constant.rb +28 -0
- data/vendor/ruby-signature/lib/ruby/signature/constant_table.rb +152 -0
- data/vendor/ruby-signature/lib/ruby/signature/definition.rb +172 -0
- data/vendor/ruby-signature/lib/ruby/signature/definition_builder.rb +921 -0
- data/vendor/ruby-signature/lib/ruby/signature/environment.rb +283 -0
- data/vendor/ruby-signature/lib/ruby/signature/environment_loader.rb +138 -0
- data/vendor/ruby-signature/lib/ruby/signature/environment_walker.rb +126 -0
- data/vendor/ruby-signature/lib/ruby/signature/errors.rb +189 -0
- data/vendor/ruby-signature/lib/ruby/signature/location.rb +104 -0
- data/vendor/ruby-signature/lib/ruby/signature/method_type.rb +125 -0
- data/vendor/ruby-signature/lib/ruby/signature/namespace.rb +93 -0
- data/vendor/ruby-signature/lib/ruby/signature/parser.y +1343 -0
- data/vendor/ruby-signature/lib/ruby/signature/prototype/rb.rb +441 -0
- data/vendor/ruby-signature/lib/ruby/signature/prototype/rbi.rb +579 -0
- data/vendor/ruby-signature/lib/ruby/signature/prototype/runtime.rb +383 -0
- data/vendor/ruby-signature/lib/ruby/signature/substitution.rb +48 -0
- data/vendor/ruby-signature/lib/ruby/signature/test.rb +28 -0
- data/vendor/ruby-signature/lib/ruby/signature/test/errors.rb +63 -0
- data/vendor/ruby-signature/lib/ruby/signature/test/hook.rb +290 -0
- data/vendor/ruby-signature/lib/ruby/signature/test/setup.rb +58 -0
- data/vendor/ruby-signature/lib/ruby/signature/test/spy.rb +324 -0
- data/vendor/ruby-signature/lib/ruby/signature/test/test_helper.rb +185 -0
- data/vendor/ruby-signature/lib/ruby/signature/test/type_check.rb +256 -0
- data/vendor/ruby-signature/lib/ruby/signature/type_name.rb +72 -0
- data/vendor/ruby-signature/lib/ruby/signature/types.rb +932 -0
- data/vendor/ruby-signature/lib/ruby/signature/variance_calculator.rb +140 -0
- data/vendor/ruby-signature/lib/ruby/signature/vendorer.rb +49 -0
- data/vendor/ruby-signature/lib/ruby/signature/version.rb +5 -0
- data/vendor/ruby-signature/lib/ruby/signature/writer.rb +271 -0
- data/vendor/ruby-signature/ruby-signature.gemspec +45 -0
- data/vendor/ruby-signature/stdlib/abbrev/abbrev.rbs +3 -0
- data/vendor/ruby-signature/stdlib/base64/base64.rbs +15 -0
- data/vendor/ruby-signature/stdlib/builtin/array.rbs +1997 -0
- data/vendor/ruby-signature/stdlib/builtin/basic_object.rbs +280 -0
- data/vendor/ruby-signature/stdlib/builtin/binding.rbs +177 -0
- data/vendor/ruby-signature/stdlib/builtin/builtin.rbs +35 -0
- data/vendor/ruby-signature/stdlib/builtin/class.rbs +145 -0
- data/vendor/ruby-signature/stdlib/builtin/comparable.rbs +116 -0
- data/vendor/ruby-signature/stdlib/builtin/complex.rbs +400 -0
- data/vendor/ruby-signature/stdlib/builtin/constants.rbs +37 -0
- data/vendor/ruby-signature/stdlib/builtin/data.rbs +5 -0
- data/vendor/ruby-signature/stdlib/builtin/deprecated.rbs +2 -0
- data/vendor/ruby-signature/stdlib/builtin/dir.rbs +419 -0
- data/vendor/ruby-signature/stdlib/builtin/encoding.rbs +606 -0
- data/vendor/ruby-signature/stdlib/builtin/enumerable.rbs +404 -0
- data/vendor/ruby-signature/stdlib/builtin/enumerator.rbs +260 -0
- data/vendor/ruby-signature/stdlib/builtin/errno.rbs +781 -0
- data/vendor/ruby-signature/stdlib/builtin/errors.rbs +582 -0
- data/vendor/ruby-signature/stdlib/builtin/exception.rbs +193 -0
- data/vendor/ruby-signature/stdlib/builtin/false_class.rbs +40 -0
- data/vendor/ruby-signature/stdlib/builtin/fiber.rbs +68 -0
- data/vendor/ruby-signature/stdlib/builtin/fiber_error.rbs +12 -0
- data/vendor/ruby-signature/stdlib/builtin/file.rbs +476 -0
- data/vendor/ruby-signature/stdlib/builtin/file_test.rbs +59 -0
- data/vendor/ruby-signature/stdlib/builtin/float.rbs +696 -0
- data/vendor/ruby-signature/stdlib/builtin/gc.rbs +121 -0
- data/vendor/ruby-signature/stdlib/builtin/hash.rbs +1029 -0
- data/vendor/ruby-signature/stdlib/builtin/integer.rbs +710 -0
- data/vendor/ruby-signature/stdlib/builtin/io.rbs +683 -0
- data/vendor/ruby-signature/stdlib/builtin/kernel.rbs +574 -0
- data/vendor/ruby-signature/stdlib/builtin/marshal.rbs +135 -0
- data/vendor/ruby-signature/stdlib/builtin/match_data.rbs +141 -0
- data/vendor/ruby-signature/stdlib/builtin/math.rbs +66 -0
- data/vendor/ruby-signature/stdlib/builtin/method.rbs +182 -0
- data/vendor/ruby-signature/stdlib/builtin/module.rbs +248 -0
- data/vendor/ruby-signature/stdlib/builtin/nil_class.rbs +82 -0
- data/vendor/ruby-signature/stdlib/builtin/numeric.rbs +409 -0
- data/vendor/ruby-signature/stdlib/builtin/object.rbs +824 -0
- data/vendor/ruby-signature/stdlib/builtin/proc.rbs +426 -0
- data/vendor/ruby-signature/stdlib/builtin/process.rbs +354 -0
- data/vendor/ruby-signature/stdlib/builtin/random.rbs +93 -0
- data/vendor/ruby-signature/stdlib/builtin/range.rbs +226 -0
- data/vendor/ruby-signature/stdlib/builtin/rational.rbs +424 -0
- data/vendor/ruby-signature/stdlib/builtin/rb_config.rbs +10 -0
- data/vendor/ruby-signature/stdlib/builtin/regexp.rbs +131 -0
- data/vendor/ruby-signature/stdlib/builtin/ruby_vm.rbs +14 -0
- data/vendor/ruby-signature/stdlib/builtin/signal.rbs +55 -0
- data/vendor/ruby-signature/stdlib/builtin/string.rbs +770 -0
- data/vendor/ruby-signature/stdlib/builtin/string_io.rbs +13 -0
- data/vendor/ruby-signature/stdlib/builtin/struct.rbs +40 -0
- data/vendor/ruby-signature/stdlib/builtin/symbol.rbs +230 -0
- data/vendor/ruby-signature/stdlib/builtin/thread.rbs +1112 -0
- data/vendor/ruby-signature/stdlib/builtin/thread_group.rbs +23 -0
- data/vendor/ruby-signature/stdlib/builtin/time.rbs +739 -0
- data/vendor/ruby-signature/stdlib/builtin/trace_point.rbs +91 -0
- data/vendor/ruby-signature/stdlib/builtin/true_class.rbs +46 -0
- data/vendor/ruby-signature/stdlib/builtin/unbound_method.rbs +159 -0
- data/vendor/ruby-signature/stdlib/builtin/warning.rbs +17 -0
- data/vendor/ruby-signature/stdlib/erb/erb.rbs +18 -0
- data/vendor/ruby-signature/stdlib/find/find.rbs +44 -0
- data/vendor/ruby-signature/stdlib/pathname/pathname.rbs +21 -0
- data/vendor/ruby-signature/stdlib/prime/integer-extension.rbs +23 -0
- data/vendor/ruby-signature/stdlib/prime/prime.rbs +188 -0
- data/vendor/ruby-signature/stdlib/securerandom/securerandom.rbs +9 -0
- data/vendor/ruby-signature/stdlib/set/set.rbs +77 -0
- data/vendor/ruby-signature/stdlib/tmpdir/tmpdir.rbs +53 -0
- metadata +244 -54
- data/.travis.yml +0 -7
- data/lib/steep/ast/signature/alias.rb +0 -19
- data/lib/steep/ast/signature/class.rb +0 -33
- data/lib/steep/ast/signature/const.rb +0 -17
- data/lib/steep/ast/signature/env.rb +0 -138
- data/lib/steep/ast/signature/extension.rb +0 -21
- data/lib/steep/ast/signature/gvar.rb +0 -17
- data/lib/steep/ast/signature/interface.rb +0 -31
- data/lib/steep/ast/signature/members.rb +0 -115
- data/lib/steep/ast/signature/module.rb +0 -21
- data/lib/steep/drivers/print_interface.rb +0 -94
- data/lib/steep/drivers/scaffold.rb +0 -321
- data/lib/steep/drivers/utils/each_signature.rb +0 -31
- data/lib/steep/interface/abstract.rb +0 -68
- data/lib/steep/interface/builder.rb +0 -637
- data/lib/steep/interface/instantiated.rb +0 -163
- data/lib/steep/interface/ivar_chain.rb +0 -26
- data/lib/steep/parser.y +0 -1278
- data/lib/steep/project/listener.rb +0 -53
- data/smoke/class/a.rbi +0 -24
- data/smoke/class/d.rb +0 -9
- data/smoke/class/e.rb +0 -12
- data/smoke/class/i.rbi +0 -9
- data/smoke/hash/e.rbi +0 -3
- data/smoke/hello/hello.rbi +0 -7
- data/smoke/implements/a.rbi +0 -6
- data/smoke/initialize/a.rbi +0 -3
- data/smoke/module/a.rbi +0 -16
- data/smoke/self/a.rbi +0 -4
- data/smoke/super/a.rbi +0 -10
- data/stdlib/builtin.rbi +0 -787
@@ -0,0 +1,93 @@
|
|
1
|
+
# [Random](Random) provides an interface to Ruby's
|
2
|
+
# pseudo-random number generator, or PRNG. The PRNG produces a
|
3
|
+
# deterministic sequence of bits which approximate true randomness. The
|
4
|
+
# sequence may be represented by integers, floats, or binary strings.
|
5
|
+
#
|
6
|
+
# The generator may be initialized with either a system-generated or
|
7
|
+
# user-supplied seed value by using
|
8
|
+
# [::srand](Random#method-c-srand).
|
9
|
+
#
|
10
|
+
# The class method [\#rand](Random#method-i-rand)
|
11
|
+
# provides the base functionality of
|
12
|
+
# [Kernel\#rand](https://ruby-doc.org/core-2.6.3/Kernel.html#method-i-rand)
|
13
|
+
# along with better handling of floating point values. These are both
|
14
|
+
# interfaces to Random::DEFAULT, the Ruby system PRNG.
|
15
|
+
#
|
16
|
+
# [::new](Random#method-c-new) will create a new PRNG
|
17
|
+
# with a state independent of Random::DEFAULT, allowing multiple
|
18
|
+
# generators with different seed values or sequence positions to exist
|
19
|
+
# simultaneously. [Random](Random) objects can be
|
20
|
+
# marshaled, allowing sequences to be saved and resumed.
|
21
|
+
#
|
22
|
+
# PRNGs are currently implemented as a modified Mersenne Twister with a
|
23
|
+
# period of 2\*\*19937-1.
|
24
|
+
class Random < Object
|
25
|
+
include Random::Formatter
|
26
|
+
|
27
|
+
def ==: (untyped arg0) -> bool
|
28
|
+
|
29
|
+
def bytes: (Integer size) -> String
|
30
|
+
|
31
|
+
def initialize: (?Integer seed) -> void
|
32
|
+
|
33
|
+
def rand: () -> Float
|
34
|
+
| (Integer | ::Range[Integer] max) -> Integer
|
35
|
+
| (Float | ::Range[Float] max) -> Float
|
36
|
+
|
37
|
+
# Returns the seed value used to initialize the generator. This may be
|
38
|
+
# used to initialize another generator with the same state at a later
|
39
|
+
# time, causing it to produce the same sequence of numbers.
|
40
|
+
#
|
41
|
+
# ```ruby
|
42
|
+
# prng1 = Random.new(1234)
|
43
|
+
# prng1.seed #=> 1234
|
44
|
+
# prng1.rand(100) #=> 47
|
45
|
+
#
|
46
|
+
# prng2 = Random.new(prng1.seed)
|
47
|
+
# prng2.rand(100) #=> 47
|
48
|
+
# ```
|
49
|
+
def seed: () -> Integer
|
50
|
+
|
51
|
+
# Returns an arbitrary seed value. This is used by
|
52
|
+
# [::new](Random.downloaded.ruby_doc#method-c-new) when no seed value is
|
53
|
+
# specified as an argument.
|
54
|
+
#
|
55
|
+
# ```ruby
|
56
|
+
# Random.new_seed #=> 115032730400174366788466674494640623225
|
57
|
+
# ```
|
58
|
+
def self.new_seed: () -> Integer
|
59
|
+
|
60
|
+
def self.rand: (?Integer max) -> Numeric
|
61
|
+
|
62
|
+
def self.srand: (?Integer number) -> Numeric
|
63
|
+
end
|
64
|
+
|
65
|
+
Random::DEFAULT: Random
|
66
|
+
|
67
|
+
module Random::Formatter
|
68
|
+
def base64: (?Integer? n) -> String
|
69
|
+
|
70
|
+
def hex: (?Integer? n) -> String
|
71
|
+
|
72
|
+
def rand: () -> Float
|
73
|
+
| (?Float? n) -> Float
|
74
|
+
| (?Integer? n) -> Integer
|
75
|
+
| (?Numeric? n) -> Numeric
|
76
|
+
| (?::Range[Float]? n) -> Float
|
77
|
+
| (?::Range[Integer]? n) -> Integer
|
78
|
+
| (?::Range[Numeric]? n) -> Numeric
|
79
|
+
|
80
|
+
def random_bytes: (?Integer? n) -> String
|
81
|
+
|
82
|
+
def random_number: () -> Float
|
83
|
+
| (?Float? n) -> Float
|
84
|
+
| (?Integer? n) -> Integer
|
85
|
+
| (?Numeric? n) -> Numeric
|
86
|
+
| (?::Range[Float]? n) -> Float
|
87
|
+
| (?::Range[Integer]? n) -> Integer
|
88
|
+
| (?::Range[Numeric]? n) -> Numeric
|
89
|
+
|
90
|
+
def urlsafe_base64: (?Integer? n, ?bool padding) -> String
|
91
|
+
|
92
|
+
def uuid: () -> String
|
93
|
+
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
|
@@ -0,0 +1,424 @@
|
|
1
|
+
# A rational number can be represented as a pair of integer numbers: a/b (b>0),
|
2
|
+
# where a is the numerator and b is the denominator. Integer a equals rational
|
3
|
+
# a/1 mathematically.
|
4
|
+
#
|
5
|
+
# In Ruby, you can create rational objects with the Kernel#Rational, to_r, or
|
6
|
+
# rationalize methods or by suffixing `r` to a literal. The return values will
|
7
|
+
# be irreducible fractions.
|
8
|
+
#
|
9
|
+
# Rational(1) #=> (1/1)
|
10
|
+
# Rational(2, 3) #=> (2/3)
|
11
|
+
# Rational(4, -6) #=> (-2/3)
|
12
|
+
# 3.to_r #=> (3/1)
|
13
|
+
# 2/3r #=> (2/3)
|
14
|
+
#
|
15
|
+
# You can also create rational objects from floating-point numbers or strings.
|
16
|
+
#
|
17
|
+
# Rational(0.3) #=> (5404319552844595/18014398509481984)
|
18
|
+
# Rational('0.3') #=> (3/10)
|
19
|
+
# Rational('2/3') #=> (2/3)
|
20
|
+
#
|
21
|
+
# 0.3.to_r #=> (5404319552844595/18014398509481984)
|
22
|
+
# '0.3'.to_r #=> (3/10)
|
23
|
+
# '2/3'.to_r #=> (2/3)
|
24
|
+
# 0.3.rationalize #=> (3/10)
|
25
|
+
#
|
26
|
+
# A rational object is an exact number, which helps you to write programs
|
27
|
+
# without any rounding errors.
|
28
|
+
#
|
29
|
+
# 10.times.inject(0) {|t| t + 0.1 } #=> 0.9999999999999999
|
30
|
+
# 10.times.inject(0) {|t| t + Rational('0.1') } #=> (1/1)
|
31
|
+
#
|
32
|
+
# However, when an expression includes an inexact component (numerical value or
|
33
|
+
# operation), it will produce an inexact result.
|
34
|
+
#
|
35
|
+
# Rational(10) / 3 #=> (10/3)
|
36
|
+
# Rational(10) / 3.0 #=> 3.3333333333333335
|
37
|
+
#
|
38
|
+
# Rational(-8) ** Rational(1, 3)
|
39
|
+
# #=> (1.0000000000000002+1.7320508075688772i)
|
40
|
+
#
|
41
|
+
class Rational < Numeric
|
42
|
+
public
|
43
|
+
|
44
|
+
def %: (Integer) -> Rational
|
45
|
+
| (Float) -> Float
|
46
|
+
| (Rational) -> Rational
|
47
|
+
| (Numeric) -> Numeric
|
48
|
+
|
49
|
+
# Performs multiplication.
|
50
|
+
#
|
51
|
+
# Rational(2, 3) * Rational(2, 3) #=> (4/9)
|
52
|
+
# Rational(900) * Rational(1) #=> (900/1)
|
53
|
+
# Rational(-2, 9) * Rational(-9, 2) #=> (1/1)
|
54
|
+
# Rational(9, 8) * 4 #=> (9/2)
|
55
|
+
# Rational(20, 9) * 9.8 #=> 21.77777777777778
|
56
|
+
#
|
57
|
+
def *: (Float) -> Float
|
58
|
+
| (Complex) -> Complex
|
59
|
+
| (Numeric) -> Numeric
|
60
|
+
|
61
|
+
# Performs exponentiation.
|
62
|
+
#
|
63
|
+
# Rational(2) ** Rational(3) #=> (8/1)
|
64
|
+
# Rational(10) ** -2 #=> (1/100)
|
65
|
+
# Rational(10) ** -2.0 #=> 0.01
|
66
|
+
# Rational(-4) ** Rational(1, 2) #=> (0.0+2.0i)
|
67
|
+
# Rational(1, 2) ** 0 #=> (1/1)
|
68
|
+
# Rational(1, 2) ** 0.0 #=> 1.0
|
69
|
+
#
|
70
|
+
def **: (Complex) -> Complex
|
71
|
+
| (Numeric) -> Numeric
|
72
|
+
|
73
|
+
# Performs addition.
|
74
|
+
#
|
75
|
+
# Rational(2, 3) + Rational(2, 3) #=> (4/3)
|
76
|
+
# Rational(900) + Rational(1) #=> (901/1)
|
77
|
+
# Rational(-2, 9) + Rational(-9, 2) #=> (-85/18)
|
78
|
+
# Rational(9, 8) + 4 #=> (41/8)
|
79
|
+
# Rational(20, 9) + 9.8 #=> 12.022222222222222
|
80
|
+
#
|
81
|
+
def +: (Float) -> Float
|
82
|
+
| (Complex) -> Complex
|
83
|
+
| (Numeric) -> Rational
|
84
|
+
|
85
|
+
def +@: () -> Rational
|
86
|
+
|
87
|
+
# Performs subtraction.
|
88
|
+
#
|
89
|
+
# Rational(2, 3) - Rational(2, 3) #=> (0/1)
|
90
|
+
# Rational(900) - Rational(1) #=> (899/1)
|
91
|
+
# Rational(-2, 9) - Rational(-9, 2) #=> (77/18)
|
92
|
+
# Rational(9, 8) - 4 #=> (-23/8)
|
93
|
+
# Rational(20, 9) - 9.8 #=> -7.577777777777778
|
94
|
+
#
|
95
|
+
def -: (Float) -> Float
|
96
|
+
| (Complex) -> Complex
|
97
|
+
| (Numeric) -> Rational
|
98
|
+
|
99
|
+
# Negates `rat`.
|
100
|
+
#
|
101
|
+
def -@: () -> Rational
|
102
|
+
|
103
|
+
# Performs division.
|
104
|
+
#
|
105
|
+
# Rational(2, 3) / Rational(2, 3) #=> (1/1)
|
106
|
+
# Rational(900) / Rational(1) #=> (900/1)
|
107
|
+
# Rational(-2, 9) / Rational(-9, 2) #=> (4/81)
|
108
|
+
# Rational(9, 8) / 4 #=> (9/32)
|
109
|
+
# Rational(20, 9) / 9.8 #=> 0.22675736961451246
|
110
|
+
#
|
111
|
+
def /: (Float) -> Float
|
112
|
+
| (Complex) -> Complex
|
113
|
+
| (Numeric) -> Rational
|
114
|
+
|
115
|
+
# Returns -1, 0, or +1 depending on whether `rational` is less than, equal to,
|
116
|
+
# or greater than `numeric`.
|
117
|
+
#
|
118
|
+
# `nil` is returned if the two values are incomparable.
|
119
|
+
#
|
120
|
+
# Rational(2, 3) <=> Rational(2, 3) #=> 0
|
121
|
+
# Rational(5) <=> 5 #=> 0
|
122
|
+
# Rational(2, 3) <=> Rational(1, 3) #=> 1
|
123
|
+
# Rational(1, 3) <=> 1 #=> -1
|
124
|
+
# Rational(1, 3) <=> 0.3 #=> 1
|
125
|
+
#
|
126
|
+
# Rational(1, 3) <=> "0.3" #=> nil
|
127
|
+
#
|
128
|
+
def <=>: (Numeric) -> Integer?
|
129
|
+
|
130
|
+
# Returns `true` if `rat` equals `object` numerically.
|
131
|
+
#
|
132
|
+
# Rational(2, 3) == Rational(2, 3) #=> true
|
133
|
+
# Rational(5) == 5 #=> true
|
134
|
+
# Rational(0) == 0.0 #=> true
|
135
|
+
# Rational('1/3') == 0.33 #=> false
|
136
|
+
# Rational('1/2') == '1/2' #=> false
|
137
|
+
#
|
138
|
+
def ==: (untyped) -> bool
|
139
|
+
|
140
|
+
# Returns the absolute value of `rat`.
|
141
|
+
#
|
142
|
+
# (1/2r).abs #=> (1/2)
|
143
|
+
# (-1/2r).abs #=> (1/2)
|
144
|
+
#
|
145
|
+
# Rational#magnitude is an alias for Rational#abs.
|
146
|
+
#
|
147
|
+
def abs: () -> Rational
|
148
|
+
|
149
|
+
def abs2: () -> Rational
|
150
|
+
|
151
|
+
def angle: () -> (Integer | Float)
|
152
|
+
|
153
|
+
alias arg angle
|
154
|
+
|
155
|
+
# Returns the smallest number greater than or equal to `rat` with a precision of
|
156
|
+
# `ndigits` decimal digits (default: 0).
|
157
|
+
#
|
158
|
+
# When the precision is negative, the returned value is an integer with at least
|
159
|
+
# `ndigits.abs` trailing zeros.
|
160
|
+
#
|
161
|
+
# Returns a rational when `ndigits` is positive, otherwise returns an integer.
|
162
|
+
#
|
163
|
+
# Rational(3).ceil #=> 3
|
164
|
+
# Rational(2, 3).ceil #=> 1
|
165
|
+
# Rational(-3, 2).ceil #=> -1
|
166
|
+
#
|
167
|
+
# # decimal - 1 2 3 . 4 5 6
|
168
|
+
# # ^ ^ ^ ^ ^ ^
|
169
|
+
# # precision -3 -2 -1 0 +1 +2
|
170
|
+
#
|
171
|
+
# Rational('-123.456').ceil(+1).to_f #=> -123.4
|
172
|
+
# Rational('-123.456').ceil(-1) #=> -120
|
173
|
+
#
|
174
|
+
def ceil: () -> Integer
|
175
|
+
| (Integer digits) -> (Integer | Rational)
|
176
|
+
|
177
|
+
def clone: (?freeze: bool) -> self
|
178
|
+
|
179
|
+
def coerce: (Numeric) -> [Numeric, Numeric]
|
180
|
+
|
181
|
+
def conj: () -> Rational
|
182
|
+
|
183
|
+
def conjugate: () -> Rational
|
184
|
+
|
185
|
+
# Returns the denominator (always positive).
|
186
|
+
#
|
187
|
+
# Rational(7).denominator #=> 1
|
188
|
+
# Rational(7, 1).denominator #=> 1
|
189
|
+
# Rational(9, -4).denominator #=> 4
|
190
|
+
# Rational(-2, -10).denominator #=> 5
|
191
|
+
#
|
192
|
+
def denominator: () -> Integer
|
193
|
+
|
194
|
+
def div: (Numeric) -> Integer
|
195
|
+
|
196
|
+
def divmod: (Numeric) -> [Numeric, Numeric]
|
197
|
+
|
198
|
+
def dup: () -> self
|
199
|
+
|
200
|
+
def eql?: (untyped) -> bool
|
201
|
+
|
202
|
+
# Performs division and returns the value as a Float.
|
203
|
+
#
|
204
|
+
# Rational(2, 3).fdiv(1) #=> 0.6666666666666666
|
205
|
+
# Rational(2, 3).fdiv(0.5) #=> 1.3333333333333333
|
206
|
+
# Rational(2).fdiv(3) #=> 0.6666666666666666
|
207
|
+
#
|
208
|
+
def fdiv: (Numeric) -> Float
|
209
|
+
|
210
|
+
def finite?: () -> bool
|
211
|
+
|
212
|
+
# Returns the largest number less than or equal to `rat` with a precision of
|
213
|
+
# `ndigits` decimal digits (default: 0).
|
214
|
+
#
|
215
|
+
# When the precision is negative, the returned value is an integer with at least
|
216
|
+
# `ndigits.abs` trailing zeros.
|
217
|
+
#
|
218
|
+
# Returns a rational when `ndigits` is positive, otherwise returns an integer.
|
219
|
+
#
|
220
|
+
# Rational(3).floor #=> 3
|
221
|
+
# Rational(2, 3).floor #=> 0
|
222
|
+
# Rational(-3, 2).floor #=> -2
|
223
|
+
#
|
224
|
+
# # decimal - 1 2 3 . 4 5 6
|
225
|
+
# # ^ ^ ^ ^ ^ ^
|
226
|
+
# # precision -3 -2 -1 0 +1 +2
|
227
|
+
#
|
228
|
+
# Rational('-123.456').floor(+1).to_f #=> -123.5
|
229
|
+
# Rational('-123.456').floor(-1) #=> -130
|
230
|
+
#
|
231
|
+
def floor: () -> Integer
|
232
|
+
| (Integer digits) -> (Integer | Rational)
|
233
|
+
|
234
|
+
def hash: () -> Integer
|
235
|
+
|
236
|
+
def i: () -> Complex
|
237
|
+
|
238
|
+
def imag: () -> Integer
|
239
|
+
|
240
|
+
def imaginary: () -> Integer
|
241
|
+
|
242
|
+
def infinite?: () -> Integer?
|
243
|
+
|
244
|
+
# Returns the value as a string for inspection.
|
245
|
+
#
|
246
|
+
# Rational(2).inspect #=> "(2/1)"
|
247
|
+
# Rational(-8, 6).inspect #=> "(-4/3)"
|
248
|
+
# Rational('1/2').inspect #=> "(1/2)"
|
249
|
+
#
|
250
|
+
def inspect: () -> String
|
251
|
+
|
252
|
+
def integer?: () -> bool
|
253
|
+
|
254
|
+
# Returns the absolute value of `rat`.
|
255
|
+
#
|
256
|
+
# (1/2r).abs #=> (1/2)
|
257
|
+
# (-1/2r).abs #=> (1/2)
|
258
|
+
#
|
259
|
+
# Rational#magnitude is an alias for Rational#abs.
|
260
|
+
#
|
261
|
+
alias magnitude abs
|
262
|
+
|
263
|
+
def modulo: (Float) -> Float
|
264
|
+
| (Numeric) -> Rational
|
265
|
+
|
266
|
+
# Returns `true` if `rat` is less than 0.
|
267
|
+
#
|
268
|
+
def negative?: () -> bool
|
269
|
+
|
270
|
+
def nonzero?: () -> self?
|
271
|
+
|
272
|
+
# Returns the numerator.
|
273
|
+
#
|
274
|
+
# Rational(7).numerator #=> 7
|
275
|
+
# Rational(7, 1).numerator #=> 7
|
276
|
+
# Rational(9, -4).numerator #=> -9
|
277
|
+
# Rational(-2, -10).numerator #=> 1
|
278
|
+
#
|
279
|
+
def numerator: () -> Integer
|
280
|
+
|
281
|
+
alias phase angle
|
282
|
+
|
283
|
+
def polar: () -> [ Rational, Integer | Float ]
|
284
|
+
|
285
|
+
# Returns `true` if `rat` is greater than 0.
|
286
|
+
#
|
287
|
+
def positive?: () -> bool
|
288
|
+
|
289
|
+
# Performs division.
|
290
|
+
#
|
291
|
+
# Rational(2, 3) / Rational(2, 3) #=> (1/1)
|
292
|
+
# Rational(900) / Rational(1) #=> (900/1)
|
293
|
+
# Rational(-2, 9) / Rational(-9, 2) #=> (4/81)
|
294
|
+
# Rational(9, 8) / 4 #=> (9/32)
|
295
|
+
# Rational(20, 9) / 9.8 #=> 0.22675736961451246
|
296
|
+
#
|
297
|
+
def quo: (Float) -> Float
|
298
|
+
| (Complex) -> Complex
|
299
|
+
| (Numeric) -> Rational
|
300
|
+
|
301
|
+
# Returns a simpler approximation of the value if the optional argument `eps` is
|
302
|
+
# given (rat-|eps| <= result <= rat+|eps|), self otherwise.
|
303
|
+
#
|
304
|
+
# r = Rational(5033165, 16777216)
|
305
|
+
# r.rationalize #=> (5033165/16777216)
|
306
|
+
# r.rationalize(Rational('0.01')) #=> (3/10)
|
307
|
+
# r.rationalize(Rational('0.1')) #=> (1/3)
|
308
|
+
#
|
309
|
+
def rationalize: (?Numeric eps) -> Rational
|
310
|
+
|
311
|
+
def real: () -> Rational
|
312
|
+
|
313
|
+
def real?: () -> true
|
314
|
+
|
315
|
+
def rect: () -> [ Rational, Numeric ]
|
316
|
+
|
317
|
+
alias rectangular rect
|
318
|
+
|
319
|
+
def remainder: (Float) -> Float
|
320
|
+
| (Numeric) -> Rational
|
321
|
+
|
322
|
+
# Returns `rat` rounded to the nearest value with a precision of `ndigits`
|
323
|
+
# decimal digits (default: 0).
|
324
|
+
#
|
325
|
+
# When the precision is negative, the returned value is an integer with at least
|
326
|
+
# `ndigits.abs` trailing zeros.
|
327
|
+
#
|
328
|
+
# Returns a rational when `ndigits` is positive, otherwise returns an integer.
|
329
|
+
#
|
330
|
+
# Rational(3).round #=> 3
|
331
|
+
# Rational(2, 3).round #=> 1
|
332
|
+
# Rational(-3, 2).round #=> -2
|
333
|
+
#
|
334
|
+
# # decimal - 1 2 3 . 4 5 6
|
335
|
+
# # ^ ^ ^ ^ ^ ^
|
336
|
+
# # precision -3 -2 -1 0 +1 +2
|
337
|
+
#
|
338
|
+
# Rational('-123.456').round(+1).to_f #=> -123.5
|
339
|
+
# Rational('-123.456').round(-1) #=> -120
|
340
|
+
#
|
341
|
+
# The optional `half` keyword argument is available similar to Float#round.
|
342
|
+
#
|
343
|
+
# Rational(25, 100).round(1, half: :up) #=> (3/10)
|
344
|
+
# Rational(25, 100).round(1, half: :down) #=> (1/5)
|
345
|
+
# Rational(25, 100).round(1, half: :even) #=> (1/5)
|
346
|
+
# Rational(35, 100).round(1, half: :up) #=> (2/5)
|
347
|
+
# Rational(35, 100).round(1, half: :down) #=> (3/10)
|
348
|
+
# Rational(35, 100).round(1, half: :even) #=> (2/5)
|
349
|
+
# Rational(-25, 100).round(1, half: :up) #=> (-3/10)
|
350
|
+
# Rational(-25, 100).round(1, half: :down) #=> (-1/5)
|
351
|
+
# Rational(-25, 100).round(1, half: :even) #=> (-1/5)
|
352
|
+
#
|
353
|
+
def round: (?half: :up | :down | :even) -> Integer
|
354
|
+
| (Integer digits, ?half: :up | :down | :even) -> (Integer | Rational)
|
355
|
+
|
356
|
+
def step: (?Numeric limit, ?Numeric step) { (Rational) -> void } -> self
|
357
|
+
| (?Numeric limit, ?Numeric step) -> Enumerator[Rational, self]
|
358
|
+
| (?by: Numeric, ?to: Numeric) { (Rational) -> void } -> self
|
359
|
+
| (?by: Numeric, ?to: Numeric) -> Enumerator[Rational, self]
|
360
|
+
|
361
|
+
def to_c: () -> Complex
|
362
|
+
|
363
|
+
# Returns the value as a Float.
|
364
|
+
#
|
365
|
+
# Rational(2).to_f #=> 2.0
|
366
|
+
# Rational(9, 4).to_f #=> 2.25
|
367
|
+
# Rational(-3, 4).to_f #=> -0.75
|
368
|
+
# Rational(20, 3).to_f #=> 6.666666666666667
|
369
|
+
#
|
370
|
+
def to_f: () -> Float
|
371
|
+
|
372
|
+
# Returns the truncated value as an integer.
|
373
|
+
#
|
374
|
+
# Equivalent to Rational#truncate.
|
375
|
+
#
|
376
|
+
# Rational(2, 3).to_i #=> 0
|
377
|
+
# Rational(3).to_i #=> 3
|
378
|
+
# Rational(300.6).to_i #=> 300
|
379
|
+
# Rational(98, 71).to_i #=> 1
|
380
|
+
# Rational(-31, 2).to_i #=> -15
|
381
|
+
#
|
382
|
+
def to_i: () -> Integer
|
383
|
+
|
384
|
+
alias to_int to_i
|
385
|
+
|
386
|
+
# Returns self.
|
387
|
+
#
|
388
|
+
# Rational(2).to_r #=> (2/1)
|
389
|
+
# Rational(-8, 6).to_r #=> (-4/3)
|
390
|
+
#
|
391
|
+
def to_r: () -> Rational
|
392
|
+
|
393
|
+
# Returns the value as a string.
|
394
|
+
#
|
395
|
+
# Rational(2).to_s #=> "2/1"
|
396
|
+
# Rational(-8, 6).to_s #=> "-4/3"
|
397
|
+
# Rational('1/2').to_s #=> "1/2"
|
398
|
+
#
|
399
|
+
def to_s: () -> String
|
400
|
+
|
401
|
+
# Returns `rat` truncated (toward zero) to a precision of `ndigits` decimal
|
402
|
+
# digits (default: 0).
|
403
|
+
#
|
404
|
+
# When the precision is negative, the returned value is an integer with at least
|
405
|
+
# `ndigits.abs` trailing zeros.
|
406
|
+
#
|
407
|
+
# Returns a rational when `ndigits` is positive, otherwise returns an integer.
|
408
|
+
#
|
409
|
+
# Rational(3).truncate #=> 3
|
410
|
+
# Rational(2, 3).truncate #=> 0
|
411
|
+
# Rational(-3, 2).truncate #=> -1
|
412
|
+
#
|
413
|
+
# # decimal - 1 2 3 . 4 5 6
|
414
|
+
# # ^ ^ ^ ^ ^ ^
|
415
|
+
# # precision -3 -2 -1 0 +1 +2
|
416
|
+
#
|
417
|
+
# Rational('-123.456').truncate(+1).to_f #=> -123.4
|
418
|
+
# Rational('-123.456').truncate(-1) #=> -120
|
419
|
+
#
|
420
|
+
def truncate: () -> Integer
|
421
|
+
| (Integer ndigits) -> (Integer | Rational)
|
422
|
+
|
423
|
+
def zero?: () -> bool
|
424
|
+
end
|