rbs 3.9.5 → 3.10.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/.clang-format +74 -0
- data/.clangd +2 -0
- data/.github/workflows/c-check.yml +54 -0
- data/.github/workflows/comments.yml +3 -3
- data/.github/workflows/ruby.yml +34 -19
- data/.github/workflows/typecheck.yml +1 -1
- data/.github/workflows/windows.yml +1 -1
- data/.gitignore +4 -0
- data/CHANGELOG.md +49 -0
- data/README.md +38 -1
- data/Rakefile +152 -23
- data/config.yml +190 -62
- data/core/array.rbs +100 -46
- data/core/complex.rbs +32 -21
- data/core/dir.rbs +2 -2
- data/core/encoding.rbs +6 -9
- data/core/enumerable.rbs +90 -3
- data/core/enumerator.rbs +18 -1
- data/core/errno.rbs +8 -0
- data/core/errors.rbs +28 -1
- data/core/exception.rbs +2 -2
- data/core/fiber.rbs +5 -4
- data/core/file.rbs +27 -12
- data/core/file_test.rbs +1 -1
- data/core/float.rbs +209 -22
- data/core/gc.rbs +417 -281
- data/core/hash.rbs +1024 -727
- data/core/integer.rbs +78 -38
- data/core/io/buffer.rbs +18 -7
- data/core/io/wait.rbs +11 -33
- data/core/io.rbs +14 -12
- data/core/kernel.rbs +57 -51
- data/core/marshal.rbs +1 -1
- data/core/match_data.rbs +1 -1
- data/core/math.rbs +42 -3
- data/core/method.rbs +14 -6
- data/core/module.rbs +88 -17
- data/core/nil_class.rbs +3 -3
- data/core/numeric.rbs +16 -16
- data/core/object.rbs +3 -3
- data/core/object_space.rbs +21 -15
- data/{stdlib/pathname/0 → core}/pathname.rbs +255 -355
- data/core/proc.rbs +15 -8
- data/core/process.rbs +2 -2
- data/core/ractor.rbs +278 -437
- data/core/range.rbs +7 -8
- data/core/rational.rbs +37 -24
- data/core/rbs/unnamed/argf.rbs +2 -2
- data/core/rbs/unnamed/env_class.rbs +1 -1
- data/core/rbs/unnamed/random.rbs +4 -2
- data/core/regexp.rbs +25 -20
- data/core/ruby.rbs +53 -0
- data/core/ruby_vm.rbs +6 -4
- data/core/rubygems/errors.rbs +3 -70
- data/core/rubygems/rubygems.rbs +11 -79
- data/core/rubygems/version.rbs +2 -3
- data/core/set.rbs +488 -359
- data/core/string.rbs +3145 -1231
- data/core/struct.rbs +1 -1
- data/core/symbol.rbs +4 -4
- data/core/thread.rbs +92 -29
- data/core/time.rbs +35 -9
- data/core/trace_point.rbs +7 -4
- data/core/unbound_method.rbs +14 -6
- data/docs/aliases.md +79 -0
- data/docs/collection.md +2 -2
- data/docs/encoding.md +56 -0
- data/docs/gem.md +0 -1
- data/docs/sigs.md +3 -3
- data/ext/rbs_extension/ast_translation.c +1016 -0
- data/ext/rbs_extension/ast_translation.h +37 -0
- data/ext/rbs_extension/class_constants.c +155 -0
- data/{include/rbs/constants.h → ext/rbs_extension/class_constants.h} +7 -1
- data/ext/rbs_extension/compat.h +10 -0
- data/ext/rbs_extension/extconf.rb +25 -1
- data/ext/rbs_extension/legacy_location.c +317 -0
- data/ext/rbs_extension/legacy_location.h +45 -0
- data/ext/rbs_extension/main.c +357 -14
- data/ext/rbs_extension/rbs_extension.h +6 -21
- data/ext/rbs_extension/rbs_string_bridging.c +9 -0
- data/ext/rbs_extension/rbs_string_bridging.h +24 -0
- data/include/rbs/ast.h +687 -0
- data/include/rbs/defines.h +86 -0
- data/include/rbs/lexer.h +199 -0
- data/include/rbs/location.h +59 -0
- data/include/rbs/parser.h +135 -0
- data/include/rbs/string.h +47 -0
- data/include/rbs/util/rbs_allocator.h +59 -0
- data/include/rbs/util/rbs_assert.h +20 -0
- data/include/rbs/util/rbs_buffer.h +83 -0
- data/include/rbs/util/rbs_constant_pool.h +6 -67
- data/include/rbs/util/rbs_encoding.h +282 -0
- data/include/rbs/util/rbs_unescape.h +24 -0
- data/include/rbs.h +1 -2
- data/lib/rbs/annotate/formatter.rb +3 -13
- data/lib/rbs/annotate/rdoc_annotator.rb +3 -1
- data/lib/rbs/annotate/rdoc_source.rb +1 -1
- data/lib/rbs/cli/validate.rb +2 -2
- data/lib/rbs/cli.rb +1 -1
- data/lib/rbs/collection/config/lockfile_generator.rb +1 -0
- data/lib/rbs/definition_builder/ancestor_builder.rb +5 -5
- data/lib/rbs/environment.rb +64 -59
- data/lib/rbs/environment_loader.rb +1 -1
- data/lib/rbs/errors.rb +1 -1
- data/lib/rbs/parser_aux.rb +5 -0
- data/lib/rbs/resolver/constant_resolver.rb +2 -2
- data/lib/rbs/resolver/type_name_resolver.rb +124 -38
- data/lib/rbs/test/type_check.rb +14 -0
- data/lib/rbs/types.rb +3 -1
- data/lib/rbs/version.rb +1 -1
- data/lib/rbs.rb +1 -1
- data/lib/rdoc/discover.rb +1 -1
- data/lib/rdoc_plugin/parser.rb +3 -3
- data/sig/annotate/formatter.rbs +2 -2
- data/sig/annotate/rdoc_annotater.rbs +1 -1
- data/sig/environment.rbs +57 -6
- data/sig/manifest.yaml +0 -1
- data/sig/parser.rbs +20 -0
- data/sig/resolver/type_name_resolver.rbs +38 -7
- data/sig/types.rbs +4 -1
- data/src/ast.c +1256 -0
- data/src/lexer.c +2956 -0
- data/src/lexer.re +147 -0
- data/src/lexstate.c +205 -0
- data/src/location.c +71 -0
- data/src/parser.c +3507 -0
- data/src/string.c +41 -0
- data/src/util/rbs_allocator.c +152 -0
- data/src/util/rbs_assert.c +19 -0
- data/src/util/rbs_buffer.c +54 -0
- data/src/util/rbs_constant_pool.c +18 -88
- data/src/util/rbs_encoding.c +21308 -0
- data/src/util/rbs_unescape.c +167 -0
- data/stdlib/bigdecimal/0/big_decimal.rbs +100 -82
- data/stdlib/bigdecimal-math/0/big_math.rbs +169 -8
- data/stdlib/cgi/0/core.rbs +2 -396
- data/stdlib/cgi/0/manifest.yaml +1 -0
- data/stdlib/cgi-escape/0/escape.rbs +153 -0
- data/stdlib/coverage/0/coverage.rbs +3 -1
- data/stdlib/date/0/date.rbs +67 -59
- data/stdlib/date/0/date_time.rbs +1 -1
- data/stdlib/delegate/0/delegator.rbs +10 -7
- data/stdlib/erb/0/erb.rbs +737 -347
- data/stdlib/fileutils/0/fileutils.rbs +18 -13
- data/stdlib/forwardable/0/forwardable.rbs +3 -0
- data/stdlib/json/0/json.rbs +68 -48
- data/stdlib/net-http/0/net-http.rbs +3 -0
- data/stdlib/objspace/0/objspace.rbs +9 -4
- data/stdlib/open-uri/0/open-uri.rbs +40 -0
- data/stdlib/openssl/0/openssl.rbs +331 -228
- data/stdlib/optparse/0/optparse.rbs +3 -3
- data/stdlib/psych/0/psych.rbs +3 -3
- data/stdlib/rdoc/0/code_object.rbs +2 -2
- data/stdlib/rdoc/0/comment.rbs +2 -0
- data/stdlib/rdoc/0/options.rbs +76 -0
- data/stdlib/rdoc/0/rdoc.rbs +7 -5
- data/stdlib/rdoc/0/store.rbs +1 -1
- data/stdlib/resolv/0/resolv.rbs +25 -68
- data/stdlib/ripper/0/ripper.rbs +5 -2
- data/stdlib/singleton/0/singleton.rbs +3 -0
- data/stdlib/socket/0/socket.rbs +13 -1
- data/stdlib/socket/0/tcp_socket.rbs +10 -2
- data/stdlib/stringio/0/stringio.rbs +1176 -85
- data/stdlib/strscan/0/string_scanner.rbs +31 -31
- data/stdlib/tempfile/0/tempfile.rbs +3 -3
- data/stdlib/time/0/time.rbs +1 -1
- data/stdlib/timeout/0/timeout.rbs +63 -7
- data/stdlib/tsort/0/cyclic.rbs +3 -0
- data/stdlib/uri/0/common.rbs +11 -2
- data/stdlib/uri/0/file.rbs +1 -1
- data/stdlib/uri/0/generic.rbs +17 -16
- data/stdlib/uri/0/rfc2396_parser.rbs +6 -7
- data/stdlib/zlib/0/zstream.rbs +1 -0
- metadata +43 -18
- data/ext/rbs_extension/lexer.c +0 -2728
- data/ext/rbs_extension/lexer.h +0 -179
- data/ext/rbs_extension/lexer.re +0 -147
- data/ext/rbs_extension/lexstate.c +0 -175
- data/ext/rbs_extension/location.c +0 -325
- data/ext/rbs_extension/location.h +0 -85
- data/ext/rbs_extension/parser.c +0 -2982
- data/ext/rbs_extension/parser.h +0 -18
- data/ext/rbs_extension/parserstate.c +0 -411
- data/ext/rbs_extension/parserstate.h +0 -163
- data/ext/rbs_extension/unescape.c +0 -32
- data/include/rbs/ruby_objs.h +0 -72
- data/src/constants.c +0 -153
- data/src/ruby_objs.c +0 -799
data/core/enumerator.rbs
CHANGED
|
@@ -137,7 +137,7 @@ class Enumerator[unchecked out Elem, out Return = void] < Object
|
|
|
137
137
|
|
|
138
138
|
# <!--
|
|
139
139
|
# rdoc-file=enumerator.c
|
|
140
|
-
# - Enumerator.produce(initial = nil) { |prev| block } -> enumerator
|
|
140
|
+
# - Enumerator.produce(initial = nil, size: nil) { |prev| block } -> enumerator
|
|
141
141
|
# -->
|
|
142
142
|
# Creates an infinite enumerator from any block, just called over and over. The
|
|
143
143
|
# result of the previous iteration is passed to the next one. If `initial` is
|
|
@@ -169,6 +169,23 @@ class Enumerator[unchecked out Elem, out Return = void] < Object
|
|
|
169
169
|
# Enumerator.produce { scanner.scan(PATTERN) }.slice_after { scanner.eos? }.first
|
|
170
170
|
# # => ["7", "+", "38", "/", "6"]
|
|
171
171
|
#
|
|
172
|
+
# The optional `size` keyword argument specifies the size of the enumerator,
|
|
173
|
+
# which can be retrieved by Enumerator#size. It can be an integer,
|
|
174
|
+
# `Float::INFINITY`, a callable object (such as a lambda), or `nil` to indicate
|
|
175
|
+
# unknown size. When not specified, the size defaults to `Float::INFINITY`.
|
|
176
|
+
#
|
|
177
|
+
# # Infinite enumerator
|
|
178
|
+
# enum = Enumerator.produce(1, size: Float::INFINITY, &:succ)
|
|
179
|
+
# enum.size # => Float::INFINITY
|
|
180
|
+
#
|
|
181
|
+
# # Finite enumerator with known/computable size
|
|
182
|
+
# abs_dir = File.expand_path("./baz") # => "/foo/bar/baz"
|
|
183
|
+
# traverser = Enumerator.produce(abs_dir, size: -> { abs_dir.count("/") + 1 }) {
|
|
184
|
+
# raise StopIteration if it == "/"
|
|
185
|
+
# File.dirname(it)
|
|
186
|
+
# }
|
|
187
|
+
# traverser.size # => 4
|
|
188
|
+
#
|
|
172
189
|
def self.produce: [T] () { (T? prev) -> T } -> Enumerator[T, bot]
|
|
173
190
|
| [T] (T initial) { (T prev) -> T } -> Enumerator[T, bot]
|
|
174
191
|
|
data/core/errno.rbs
CHANGED
|
@@ -38,6 +38,14 @@
|
|
|
38
38
|
# Errno::ENOENT::Errno # => 2
|
|
39
39
|
# Errno::ENOTCAPABLE::Errno # => 0
|
|
40
40
|
#
|
|
41
|
+
# Each class in Errno can be created with optional messages:
|
|
42
|
+
#
|
|
43
|
+
# Errno::EPIPE.new # => #<Errno::EPIPE: Broken pipe>
|
|
44
|
+
# Errno::EPIPE.new("foo") # => #<Errno::EPIPE: Broken pipe - foo>
|
|
45
|
+
# Errno::EPIPE.new("foo", "here") # => #<Errno::EPIPE: Broken pipe @ here - foo>
|
|
46
|
+
#
|
|
47
|
+
# See SystemCallError.new.
|
|
48
|
+
#
|
|
41
49
|
module Errno
|
|
42
50
|
class NOERROR < SystemCallError
|
|
43
51
|
Errno: 0
|
data/core/errors.rbs
CHANGED
|
@@ -302,9 +302,15 @@ class NameError[T] < StandardError
|
|
|
302
302
|
def receiver: () -> T?
|
|
303
303
|
end
|
|
304
304
|
|
|
305
|
+
# <!-- rdoc-file=error.c -->
|
|
306
|
+
# Raised when matching pattern not found.
|
|
307
|
+
#
|
|
305
308
|
class NoMatchingPatternError < StandardError
|
|
306
309
|
end
|
|
307
310
|
|
|
311
|
+
# <!-- rdoc-file=error.c -->
|
|
312
|
+
# Raised when matching key not found.
|
|
313
|
+
#
|
|
308
314
|
class NoMatchingPatternKeyError[M, K] < NoMatchingPatternError
|
|
309
315
|
# <!--
|
|
310
316
|
# rdoc-file=error.c
|
|
@@ -588,13 +594,34 @@ end
|
|
|
588
594
|
class SystemCallError < StandardError
|
|
589
595
|
# <!--
|
|
590
596
|
# rdoc-file=error.c
|
|
591
|
-
# - SystemCallError.new(msg, errno) -> system_call_error_subclass
|
|
597
|
+
# - SystemCallError.new(msg, errno = nil, func = nil) -> system_call_error_subclass
|
|
592
598
|
# -->
|
|
593
599
|
# If *errno* corresponds to a known system error code, constructs the
|
|
594
600
|
# appropriate Errno class for that error, otherwise constructs a generic
|
|
595
601
|
# SystemCallError object. The error number is subsequently available via the
|
|
596
602
|
# #errno method.
|
|
597
603
|
#
|
|
604
|
+
# If only numeric object is given, it is treated as an Integer *errno*, and
|
|
605
|
+
# *msg* is omitted, otherwise the first argument *msg* is used as the additional
|
|
606
|
+
# error message.
|
|
607
|
+
#
|
|
608
|
+
# SystemCallError.new(Errno::EPIPE::Errno)
|
|
609
|
+
# #=> #<Errno::EPIPE: Broken pipe>
|
|
610
|
+
#
|
|
611
|
+
# SystemCallError.new("foo")
|
|
612
|
+
# #=> #<SystemCallError: unknown error - foo>
|
|
613
|
+
#
|
|
614
|
+
# SystemCallError.new("foo", Errno::EPIPE::Errno)
|
|
615
|
+
# #=> #<Errno::EPIPE: Broken pipe - foo>
|
|
616
|
+
#
|
|
617
|
+
# If *func* is not `nil`, it is appended to the message with "` @ `".
|
|
618
|
+
#
|
|
619
|
+
# SystemCallError.new("foo", Errno::EPIPE::Errno, "here")
|
|
620
|
+
# #=> #<Errno::EPIPE: Broken pipe @ here - foo>
|
|
621
|
+
#
|
|
622
|
+
# A subclass of SystemCallError can also be instantiated via the `new` method of
|
|
623
|
+
# the subclass. See Errno.
|
|
624
|
+
#
|
|
598
625
|
def initialize: (string msg, Integer errno) -> void
|
|
599
626
|
|
|
600
627
|
# <!--
|
data/core/exception.rbs
CHANGED
|
@@ -118,7 +118,7 @@ class Exception
|
|
|
118
118
|
# # String
|
|
119
119
|
# end
|
|
120
120
|
#
|
|
121
|
-
# The value returned by this method
|
|
121
|
+
# The value returned by this method might be adjusted when raising (see
|
|
122
122
|
# Kernel#raise), or during intermediate handling by #set_backtrace.
|
|
123
123
|
#
|
|
124
124
|
# See also #backtrace_locations that provide the same value, as structured
|
|
@@ -447,7 +447,7 @@ class Exception
|
|
|
447
447
|
# * If the value of keyword `order` is `:top` (the default), lists the
|
|
448
448
|
# error message and the innermost backtrace entry first.
|
|
449
449
|
# * If the value of keyword `order` is `:bottom`, lists the error message
|
|
450
|
-
# the
|
|
450
|
+
# the innermost entry last.
|
|
451
451
|
#
|
|
452
452
|
# Example:
|
|
453
453
|
#
|
data/core/fiber.rbs
CHANGED
|
@@ -58,7 +58,7 @@
|
|
|
58
58
|
# ## Non-blocking Fibers
|
|
59
59
|
#
|
|
60
60
|
# The concept of *non-blocking fiber* was introduced in Ruby 3.0. A non-blocking
|
|
61
|
-
# fiber, when reaching
|
|
61
|
+
# fiber, when reaching an operation that would normally block the fiber (like
|
|
62
62
|
# `sleep`, or wait for another process or I/O) will yield control to other
|
|
63
63
|
# fibers and allow the *scheduler* to handle blocking and waking up (resuming)
|
|
64
64
|
# this fiber when it can proceed.
|
|
@@ -82,7 +82,8 @@ class Fiber < Object
|
|
|
82
82
|
# -->
|
|
83
83
|
# Returns the value of the fiber storage variable identified by `key`.
|
|
84
84
|
#
|
|
85
|
-
# The `key` must be a symbol, and the value is set by Fiber#[]= or
|
|
85
|
+
# The `key` must be a symbol, and the value is set by Fiber#[]= or
|
|
86
|
+
# Fiber#storage.
|
|
86
87
|
#
|
|
87
88
|
# See also Fiber::[]=.
|
|
88
89
|
#
|
|
@@ -414,8 +415,8 @@ class Fiber < Object
|
|
|
414
415
|
#
|
|
415
416
|
# See Kernel#raise for more information.
|
|
416
417
|
#
|
|
417
|
-
def raise: (?string msg) -> untyped
|
|
418
|
-
| (_Exception, ?string msg, ?Array[string] | Array[Thread::Backtrace::Location] | nil backtrace) -> untyped
|
|
418
|
+
def raise: (?string msg, ?cause: Exception?) -> untyped
|
|
419
|
+
| (_Exception, ?string msg, ?Array[string] | Array[Thread::Backtrace::Location] | nil backtrace, ?cause: Exception?) -> untyped
|
|
419
420
|
|
|
420
421
|
# <!--
|
|
421
422
|
# rdoc-file=cont.c
|
data/core/file.rbs
CHANGED
|
@@ -1414,7 +1414,7 @@ class File < IO
|
|
|
1414
1414
|
# rdoc-file=file.c
|
|
1415
1415
|
# - File.owned?(file_name) -> true or false
|
|
1416
1416
|
# -->
|
|
1417
|
-
# Returns `true` if the named file exists and the effective
|
|
1417
|
+
# Returns `true` if the named file exists and the effective user id of the
|
|
1418
1418
|
# calling process is the owner of the file.
|
|
1419
1419
|
#
|
|
1420
1420
|
# *file_name* can be an IO object.
|
|
@@ -1427,8 +1427,25 @@ class File < IO
|
|
|
1427
1427
|
# -->
|
|
1428
1428
|
# Returns the string representation of the path
|
|
1429
1429
|
#
|
|
1430
|
-
#
|
|
1431
|
-
#
|
|
1430
|
+
# File.path(File::NULL) #=> "/dev/null"
|
|
1431
|
+
# File.path(Pathname.new("/tmp")) #=> "/tmp"
|
|
1432
|
+
#
|
|
1433
|
+
# If `path` is not a String:
|
|
1434
|
+
#
|
|
1435
|
+
# 1. If it has the `to_path` method, that method will be called to coerce to a
|
|
1436
|
+
# String.
|
|
1437
|
+
#
|
|
1438
|
+
# 2. Otherwise, or if the coerced result is not a String too, the standard
|
|
1439
|
+
# coersion using `to_str` method will take place on that object. (See also
|
|
1440
|
+
# String.try_convert)
|
|
1441
|
+
#
|
|
1442
|
+
# The coerced string must satisfy the following conditions:
|
|
1443
|
+
#
|
|
1444
|
+
# 1. It must be in an ASCII-compatible encoding; otherwise, an
|
|
1445
|
+
# Encoding::CompatibilityError is raised.
|
|
1446
|
+
#
|
|
1447
|
+
# 2. It must not contain the NUL character (`\0`); otherwise, an ArgumentError
|
|
1448
|
+
# is raised.
|
|
1432
1449
|
#
|
|
1433
1450
|
def self.path: (string | _ToPath path) -> String
|
|
1434
1451
|
|
|
@@ -1822,12 +1839,12 @@ class File < IO
|
|
|
1822
1839
|
# Returns `false` if `File::LOCK_NB` is specified and the operation would have
|
|
1823
1840
|
# blocked;
|
|
1824
1841
|
# otherwise returns `0`.
|
|
1825
|
-
# Constant | Lock |
|
|
1826
|
-
#
|
|
1827
|
-
#
|
|
1828
|
-
#
|
|
1829
|
-
#
|
|
1830
|
-
#
|
|
1842
|
+
# Constant | Lock | Effect
|
|
1843
|
+
# ---------------|------------|-------------------------------------------------------------------------------------------------------
|
|
1844
|
+
# `File::LOCK_EX`| Exclusive | Only one process may hold an exclusive lock for `self` at a time.
|
|
1845
|
+
# `File::LOCK_NB`|Non-blocking|No blocking; may be combined with `File::LOCK_SH` or `File::LOCK_EX` using the bitwise OR operator `|`.
|
|
1846
|
+
# `File::LOCK_SH`| Shared | Multiple processes may each hold a shared lock for `self` at the same time.
|
|
1847
|
+
# `File::LOCK_UN`| Unlock | Remove an existing lock held by this process.
|
|
1831
1848
|
# Example:
|
|
1832
1849
|
# # Update a counter using an exclusive lock.
|
|
1833
1850
|
# # Don't use File::WRONLY because it truncates the file.
|
|
@@ -2429,10 +2446,8 @@ class File::Stat < Object
|
|
|
2429
2446
|
|
|
2430
2447
|
# <!--
|
|
2431
2448
|
# rdoc-file=file.c
|
|
2432
|
-
# - new(
|
|
2449
|
+
# - File::Stat.new(file_name) -> stat
|
|
2433
2450
|
# -->
|
|
2434
|
-
# File::Stat.new(file_name) -> stat
|
|
2435
|
-
#
|
|
2436
2451
|
# Create a File::Stat object for the given file name (raising an exception if
|
|
2437
2452
|
# the file doesn't exist).
|
|
2438
2453
|
#
|
data/core/file_test.rbs
CHANGED
|
@@ -145,7 +145,7 @@ module FileTest
|
|
|
145
145
|
# rdoc-file=file.c
|
|
146
146
|
# - File.owned?(file_name) -> true or false
|
|
147
147
|
# -->
|
|
148
|
-
# Returns `true` if the named file exists and the effective
|
|
148
|
+
# Returns `true` if the named file exists and the effective user id of the
|
|
149
149
|
# calling process is the owner of the file.
|
|
150
150
|
#
|
|
151
151
|
# *file_name* can be an IO object.
|
data/core/float.rbs
CHANGED
|
@@ -1,15 +1,193 @@
|
|
|
1
|
-
# <!-- rdoc-file=
|
|
2
|
-
# A Float object
|
|
3
|
-
#
|
|
1
|
+
# <!-- rdoc-file=float.rb -->
|
|
2
|
+
# A Float object stores a real number using the native architecture's
|
|
3
|
+
# double-precision floating-point representation.
|
|
4
|
+
#
|
|
5
|
+
# ## Float Imprecisions
|
|
6
|
+
#
|
|
7
|
+
# Some real numbers can be represented precisely as Float objects:
|
|
8
|
+
#
|
|
9
|
+
# 37.5 # => 37.5
|
|
10
|
+
# 98.75 # => 98.75
|
|
11
|
+
# 12.3125 # => 12.3125
|
|
12
|
+
#
|
|
13
|
+
# Others cannot; among these are the transcendental numbers, including:
|
|
14
|
+
#
|
|
15
|
+
# * Pi, *π*: in mathematics, a number of infinite precision:
|
|
16
|
+
# 3.1415926535897932384626433... (to 25 places); in Ruby, it is of limited
|
|
17
|
+
# precision (in this case, to 16 decimal places):
|
|
18
|
+
#
|
|
19
|
+
# Math::PI # => 3.141592653589793
|
|
20
|
+
#
|
|
21
|
+
# * Euler's number, *e*: in mathematics, a number of infinite precision:
|
|
22
|
+
# 2.7182818284590452353602874... (to 25 places); in Ruby, it is of limited
|
|
23
|
+
# precision (in this case, to 15 decimal places):
|
|
24
|
+
#
|
|
25
|
+
# Math::E # => 2.718281828459045
|
|
26
|
+
#
|
|
27
|
+
# Some floating-point computations in Ruby give precise results:
|
|
28
|
+
#
|
|
29
|
+
# 1.0/2 # => 0.5
|
|
30
|
+
# 100.0/8 # => 12.5
|
|
31
|
+
#
|
|
32
|
+
# Others do not:
|
|
33
|
+
#
|
|
34
|
+
# * In mathematics, 2/3 as a decimal number is an infinitely-repeating
|
|
35
|
+
# decimal: 0.666... (forever); in Ruby, `2.0/3` is of limited precision (in
|
|
36
|
+
# this case, to 16 decimal places):
|
|
37
|
+
#
|
|
38
|
+
# 2.0/3 # => 0.6666666666666666
|
|
39
|
+
#
|
|
40
|
+
# * In mathematics, the square root of 2 is an irrational number of infinite
|
|
41
|
+
# precision: 1.4142135623730950488016887... (to 25 decimal places); in Ruby,
|
|
42
|
+
# it is of limited precision (in this case, to 16 decimal places):
|
|
43
|
+
#
|
|
44
|
+
# Math.sqrt(2.0) # => 1.4142135623730951
|
|
45
|
+
#
|
|
46
|
+
# * Even a simple computation can introduce imprecision:
|
|
47
|
+
#
|
|
48
|
+
# x = 0.1 + 0.2 # => 0.30000000000000004
|
|
49
|
+
# y = 0.3 # => 0.3
|
|
50
|
+
# x == y # => false
|
|
51
|
+
#
|
|
52
|
+
# See:
|
|
53
|
+
#
|
|
54
|
+
# * https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
|
|
55
|
+
# * https://github.com/rdp/ruby_tutorials_core/wiki/Ruby-Talk-FAQ#-why-are-rub
|
|
56
|
+
# ys-floats-imprecise
|
|
57
|
+
# * https://en.wikipedia.org/wiki/Floating_point#Accuracy_problems
|
|
58
|
+
#
|
|
59
|
+
# Note that precise storage and computation of rational numbers is possible
|
|
60
|
+
# using Rational objects.
|
|
61
|
+
#
|
|
62
|
+
# ## Creating a Float
|
|
63
|
+
#
|
|
64
|
+
# You can create a Float object explicitly with:
|
|
65
|
+
#
|
|
66
|
+
# * A [floating-point literal](rdoc-ref:syntax/literals.rdoc@Float+Literals).
|
|
67
|
+
#
|
|
68
|
+
# You can convert certain objects to Floats with:
|
|
69
|
+
#
|
|
70
|
+
# * Method #Float.
|
|
71
|
+
#
|
|
72
|
+
# ## What's Here
|
|
73
|
+
#
|
|
74
|
+
# First, what's elsewhere. Class Float:
|
|
75
|
+
#
|
|
76
|
+
# * Inherits from [class Numeric](rdoc-ref:Numeric@What-27s+Here) and [class
|
|
77
|
+
# Object](rdoc-ref:Object@What-27s+Here).
|
|
78
|
+
# * Includes [module Comparable](rdoc-ref:Comparable@What-27s+Here).
|
|
79
|
+
#
|
|
80
|
+
# Here, class Float provides methods for:
|
|
81
|
+
#
|
|
82
|
+
# * [Querying](rdoc-ref:Float@Querying)
|
|
83
|
+
# * [Comparing](rdoc-ref:Float@Comparing)
|
|
84
|
+
# * [Converting](rdoc-ref:Float@Converting)
|
|
85
|
+
#
|
|
86
|
+
# ### Querying
|
|
87
|
+
#
|
|
88
|
+
# * #finite?: Returns whether `self` is finite.
|
|
89
|
+
# * #hash: Returns the integer hash code for `self`.
|
|
90
|
+
# * #infinite?: Returns whether `self` is infinite.
|
|
91
|
+
# * #nan?: Returns whether `self` is a NaN (not-a-number).
|
|
92
|
+
#
|
|
93
|
+
# ### Comparing
|
|
4
94
|
#
|
|
5
|
-
#
|
|
6
|
-
#
|
|
95
|
+
# * #<: Returns whether `self` is less than the given value.
|
|
96
|
+
# * #<=: Returns whether `self` is less than or equal to the given value.
|
|
97
|
+
# * #<=>: Returns a number indicating whether `self` is less than, equal to,
|
|
98
|
+
# or greater than the given value.
|
|
99
|
+
# * #== (aliased as #=== and #eql?): Returns whether `self` is equal to the
|
|
100
|
+
# given value.
|
|
101
|
+
# * #>: Returns whether `self` is greater than the given value.
|
|
102
|
+
# * #>=: Returns whether `self` is greater than or equal to the given value.
|
|
103
|
+
#
|
|
104
|
+
# ### Converting
|
|
105
|
+
#
|
|
106
|
+
# * #% (aliased as #modulo): Returns `self` modulo the given value.
|
|
107
|
+
# * #*: Returns the product of `self` and the given value.
|
|
108
|
+
# * #**: Returns the value of `self` raised to the power of the given value.
|
|
109
|
+
# * #+: Returns the sum of `self` and the given value.
|
|
110
|
+
# * #-: Returns the difference of `self` and the given value.
|
|
111
|
+
# * #/: Returns the quotient of `self` and the given value.
|
|
112
|
+
# * #ceil: Returns the smallest number greater than or equal to `self`.
|
|
113
|
+
# * #coerce: Returns a 2-element array containing the given value converted to
|
|
114
|
+
# a Float and `self`
|
|
115
|
+
# * #divmod: Returns a 2-element array containing the quotient and remainder
|
|
116
|
+
# results of dividing `self` by the given value.
|
|
117
|
+
# * #fdiv: Returns the Float result of dividing `self` by the given value.
|
|
118
|
+
# * #floor: Returns the greatest number smaller than or equal to `self`.
|
|
119
|
+
# * #next_float: Returns the next-larger representable Float.
|
|
120
|
+
# * #prev_float: Returns the next-smaller representable Float.
|
|
121
|
+
# * #quo: Returns the quotient from dividing `self` by the given value.
|
|
122
|
+
# * #round: Returns `self` rounded to the nearest value, to a given precision.
|
|
123
|
+
# * #to_i (aliased as #to_int): Returns `self` truncated to an Integer.
|
|
124
|
+
# * #to_s (aliased as #inspect): Returns a string containing the place-value
|
|
125
|
+
# representation of `self` in the given radix.
|
|
126
|
+
# * #truncate: Returns `self` truncated to a given precision.
|
|
127
|
+
#
|
|
128
|
+
# <!-- rdoc-file=float.rb -->
|
|
129
|
+
# A Float object stores a real number using the native architecture's
|
|
130
|
+
# double-precision floating-point representation.
|
|
131
|
+
#
|
|
132
|
+
# ## Float Imprecisions
|
|
133
|
+
#
|
|
134
|
+
# Some real numbers can be represented precisely as Float objects:
|
|
135
|
+
#
|
|
136
|
+
# 37.5 # => 37.5
|
|
137
|
+
# 98.75 # => 98.75
|
|
138
|
+
# 12.3125 # => 12.3125
|
|
139
|
+
#
|
|
140
|
+
# Others cannot; among these are the transcendental numbers, including:
|
|
141
|
+
#
|
|
142
|
+
# * Pi, *π*: in mathematics, a number of infinite precision:
|
|
143
|
+
# 3.1415926535897932384626433... (to 25 places); in Ruby, it is of limited
|
|
144
|
+
# precision (in this case, to 16 decimal places):
|
|
145
|
+
#
|
|
146
|
+
# Math::PI # => 3.141592653589793
|
|
147
|
+
#
|
|
148
|
+
# * Euler's number, *e*: in mathematics, a number of infinite precision:
|
|
149
|
+
# 2.7182818284590452353602874... (to 25 places); in Ruby, it is of limited
|
|
150
|
+
# precision (in this case, to 15 decimal places):
|
|
151
|
+
#
|
|
152
|
+
# Math::E # => 2.718281828459045
|
|
153
|
+
#
|
|
154
|
+
# Some floating-point computations in Ruby give precise results:
|
|
155
|
+
#
|
|
156
|
+
# 1.0/2 # => 0.5
|
|
157
|
+
# 100.0/8 # => 12.5
|
|
158
|
+
#
|
|
159
|
+
# Others do not:
|
|
160
|
+
#
|
|
161
|
+
# * In mathematics, 2/3 as a decimal number is an infinitely-repeating
|
|
162
|
+
# decimal: 0.666... (forever); in Ruby, `2.0/3` is of limited precision (in
|
|
163
|
+
# this case, to 16 decimal places):
|
|
164
|
+
#
|
|
165
|
+
# 2.0/3 # => 0.6666666666666666
|
|
166
|
+
#
|
|
167
|
+
# * In mathematics, the square root of 2 is an irrational number of infinite
|
|
168
|
+
# precision: 1.4142135623730950488016887... (to 25 decimal places); in Ruby,
|
|
169
|
+
# it is of limited precision (in this case, to 16 decimal places):
|
|
170
|
+
#
|
|
171
|
+
# Math.sqrt(2.0) # => 1.4142135623730951
|
|
172
|
+
#
|
|
173
|
+
# * Even a simple computation can introduce imprecision:
|
|
174
|
+
#
|
|
175
|
+
# x = 0.1 + 0.2 # => 0.30000000000000004
|
|
176
|
+
# y = 0.3 # => 0.3
|
|
177
|
+
# x == y # => false
|
|
178
|
+
#
|
|
179
|
+
# See:
|
|
7
180
|
#
|
|
8
181
|
# * https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
|
|
9
182
|
# * https://github.com/rdp/ruby_tutorials_core/wiki/Ruby-Talk-FAQ#-why-are-rub
|
|
10
183
|
# ys-floats-imprecise
|
|
11
184
|
# * https://en.wikipedia.org/wiki/Floating_point#Accuracy_problems
|
|
12
185
|
#
|
|
186
|
+
# Note that precise storage and computation of rational numbers is possible
|
|
187
|
+
# using Rational objects.
|
|
188
|
+
#
|
|
189
|
+
# ## Creating a Float
|
|
190
|
+
#
|
|
13
191
|
# You can create a Float object explicitly with:
|
|
14
192
|
#
|
|
15
193
|
# * A [floating-point literal](rdoc-ref:syntax/literals.rdoc@Float+Literals).
|
|
@@ -79,7 +257,7 @@ class Float < Numeric
|
|
|
79
257
|
# rdoc-file=numeric.c
|
|
80
258
|
# - self % other -> float
|
|
81
259
|
# -->
|
|
82
|
-
# Returns `self` modulo `other` as a
|
|
260
|
+
# Returns `self` modulo `other` as a Float.
|
|
83
261
|
#
|
|
84
262
|
# For float `f` and real number `r`, these expressions are equivalent:
|
|
85
263
|
#
|
|
@@ -111,7 +289,7 @@ class Float < Numeric
|
|
|
111
289
|
# rdoc-file=numeric.c
|
|
112
290
|
# - self * other -> numeric
|
|
113
291
|
# -->
|
|
114
|
-
# Returns
|
|
292
|
+
# Returns the numeric product of `self` and `other`:
|
|
115
293
|
#
|
|
116
294
|
# f = 3.14
|
|
117
295
|
# f * 2 # => 6.28
|
|
@@ -124,9 +302,9 @@ class Float < Numeric
|
|
|
124
302
|
|
|
125
303
|
# <!--
|
|
126
304
|
# rdoc-file=numeric.c
|
|
127
|
-
# - self **
|
|
305
|
+
# - self ** exponent -> numeric
|
|
128
306
|
# -->
|
|
129
|
-
#
|
|
307
|
+
# Returns `self` raised to the power `exponent`:
|
|
130
308
|
#
|
|
131
309
|
# f = 3.14
|
|
132
310
|
# f ** 2 # => 9.8596
|
|
@@ -140,15 +318,20 @@ class Float < Numeric
|
|
|
140
318
|
|
|
141
319
|
# <!--
|
|
142
320
|
# rdoc-file=numeric.c
|
|
143
|
-
# - self + other ->
|
|
321
|
+
# - self + other -> float or complex
|
|
144
322
|
# -->
|
|
145
|
-
# Returns
|
|
323
|
+
# Returns the sum of `self` and `other`; the result may be inexact (see Float):
|
|
146
324
|
#
|
|
147
|
-
#
|
|
148
|
-
#
|
|
149
|
-
#
|
|
150
|
-
#
|
|
151
|
-
#
|
|
325
|
+
# 3.14 + 0 # => 3.14
|
|
326
|
+
# 3.14 + 1 # => 4.140000000000001
|
|
327
|
+
# -3.14 + 0 # => -3.14
|
|
328
|
+
# -3.14 + 1 # => -2.14
|
|
329
|
+
#
|
|
330
|
+
# 3.14 + -3.14 # => 0.0
|
|
331
|
+
# -3.14 + -3.14 # => -6.28
|
|
332
|
+
#
|
|
333
|
+
# 3.14 + Complex(1, 0) # => (4.140000000000001+0i)
|
|
334
|
+
# 3.14 + Rational(1, 1) # => 4.140000000000001
|
|
152
335
|
#
|
|
153
336
|
def +: (Complex) -> Complex
|
|
154
337
|
| (Numeric) -> Float
|
|
@@ -159,7 +342,7 @@ class Float < Numeric
|
|
|
159
342
|
# rdoc-file=numeric.c
|
|
160
343
|
# - self - other -> numeric
|
|
161
344
|
# -->
|
|
162
|
-
# Returns
|
|
345
|
+
# Returns the difference of `self` and `other`:
|
|
163
346
|
#
|
|
164
347
|
# f = 3.14
|
|
165
348
|
# f - 1 # => 2.14
|
|
@@ -172,9 +355,13 @@ class Float < Numeric
|
|
|
172
355
|
|
|
173
356
|
# <!--
|
|
174
357
|
# rdoc-file=numeric.rb
|
|
175
|
-
# - -
|
|
358
|
+
# - -self -> float
|
|
176
359
|
# -->
|
|
177
|
-
# Returns `self`, negated
|
|
360
|
+
# Returns `self`, negated:
|
|
361
|
+
#
|
|
362
|
+
# -3.14 # => -3.14
|
|
363
|
+
# -(-3.14) # => 3.14
|
|
364
|
+
# -0.0 # => -0.0
|
|
178
365
|
#
|
|
179
366
|
def -@: () -> Float
|
|
180
367
|
|
|
@@ -182,7 +369,7 @@ class Float < Numeric
|
|
|
182
369
|
# rdoc-file=numeric.c
|
|
183
370
|
# - self / other -> numeric
|
|
184
371
|
# -->
|
|
185
|
-
# Returns
|
|
372
|
+
# Returns the quotient of `self` and `other`:
|
|
186
373
|
#
|
|
187
374
|
# f = 3.14
|
|
188
375
|
# f / 2 # => 1.57
|
|
@@ -646,7 +833,7 @@ class Float < Numeric
|
|
|
646
833
|
alias magnitude abs
|
|
647
834
|
|
|
648
835
|
# <!-- rdoc-file=numeric.c -->
|
|
649
|
-
# Returns `self` modulo `other` as a
|
|
836
|
+
# Returns `self` modulo `other` as a Float.
|
|
650
837
|
#
|
|
651
838
|
# For float `f` and real number `r`, these expressions are equivalent:
|
|
652
839
|
#
|
|
@@ -1094,7 +1281,7 @@ Float::MAX_EXP: Integer
|
|
|
1094
1281
|
# Usually defaults to 2.2250738585072014e-308.
|
|
1095
1282
|
#
|
|
1096
1283
|
# If the platform supports denormalized numbers, there are numbers between zero
|
|
1097
|
-
# and Float::MIN. 0.0.next_float returns the smallest positive floating point
|
|
1284
|
+
# and Float::MIN. `0.0.next_float` returns the smallest positive floating point
|
|
1098
1285
|
# number including denormalized numbers.
|
|
1099
1286
|
#
|
|
1100
1287
|
Float::MIN: Float
|