rbs 2.0.0 → 2.2.2
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/comments.yml +34 -0
- data/.github/workflows/ruby.yml +5 -0
- data/CHANGELOG.md +82 -0
- data/README.md +6 -1
- data/Rakefile +56 -21
- data/core/array.rbs +2866 -1086
- data/core/basic_object.rbs +150 -30
- data/core/binding.rbs +33 -0
- data/core/builtin.rbs +4 -4
- data/core/class.rbs +43 -5
- data/core/comparable.rbs +57 -0
- data/core/complex.rbs +170 -4
- data/core/constants.rbs +51 -0
- data/core/deprecated.rbs +7 -0
- data/core/dir.rbs +305 -20
- data/core/encoding.rbs +1214 -77
- data/core/enumerable.rbs +2173 -234
- data/core/enumerator.rbs +448 -182
- data/core/env.rbs +448 -1
- data/core/errno.rbs +1 -10
- data/core/errors.rbs +152 -2
- data/core/exception.rbs +201 -127
- data/core/false_class.rbs +27 -0
- data/core/fiber.rbs +118 -37
- data/core/fiber_error.rbs +8 -9
- data/core/file.rbs +1060 -142
- data/core/file_test.rbs +287 -32
- data/core/float.rbs +776 -300
- data/core/gc.rbs +185 -34
- data/core/global_variables.rbs +5 -1
- data/core/hash.rbs +1582 -649
- data/core/integer.rbs +974 -204
- data/core/io/buffer.rbs +710 -0
- data/core/io/wait.rbs +29 -8
- data/core/io.rbs +2438 -417
- data/core/kernel.rbs +2319 -318
- data/core/marshal.rbs +37 -2
- data/core/match_data.rbs +123 -6
- data/core/math.rbs +126 -6
- data/core/method.rbs +226 -102
- data/core/module.rbs +421 -45
- data/core/nil_class.rbs +64 -0
- data/core/numeric.rbs +620 -142
- data/core/object.rbs +453 -81
- data/core/object_space.rbs +92 -2
- data/core/proc.rbs +482 -285
- data/core/process.rbs +443 -34
- data/core/ractor.rbs +232 -9
- data/core/random.rbs +151 -52
- data/core/range.rbs +885 -160
- data/core/rational.rbs +122 -6
- data/core/rb_config.rbs +14 -4
- data/core/refinement.rbs +44 -0
- data/core/regexp.rbs +156 -14
- data/core/ruby_vm.rbs +42 -3
- data/core/signal.rbs +78 -39
- data/core/string.rbs +2123 -567
- data/core/string_io.rbs +204 -0
- data/core/struct.rbs +283 -28
- data/core/symbol.rbs +304 -30
- data/core/thread.rbs +1288 -688
- data/core/thread_group.rbs +66 -10
- data/core/time.rbs +643 -217
- data/core/trace_point.rbs +100 -12
- data/core/true_class.rbs +24 -0
- data/core/unbound_method.rbs +73 -7
- data/core/warning.rbs +37 -12
- data/docs/CONTRIBUTING.md +40 -34
- data/docs/stdlib.md +3 -102
- data/docs/syntax.md +54 -11
- data/ext/rbs_extension/extconf.rb +1 -0
- data/ext/rbs_extension/lexer.h +5 -0
- data/ext/rbs_extension/lexstate.c +6 -0
- data/ext/rbs_extension/parser.c +85 -10
- data/ext/rbs_extension/ruby_objs.c +4 -2
- data/ext/rbs_extension/ruby_objs.h +2 -2
- data/goodcheck.yml +0 -11
- data/lib/rbs/annotate/annotations.rb +197 -0
- data/lib/rbs/annotate/formatter.rb +80 -0
- data/lib/rbs/annotate/rdoc_annotator.rb +398 -0
- data/lib/rbs/annotate/rdoc_source.rb +120 -0
- data/lib/rbs/annotate.rb +6 -0
- data/lib/rbs/ast/members.rb +21 -13
- data/lib/rbs/buffer.rb +17 -11
- data/lib/rbs/cli.rb +48 -1
- data/lib/rbs/definition_builder/method_builder.rb +28 -16
- data/lib/rbs/definition_builder.rb +6 -2
- data/lib/rbs/environment.rb +8 -4
- data/lib/rbs/location_aux.rb +12 -0
- data/lib/rbs/namespace.rb +1 -1
- data/lib/rbs/prototype/rb.rb +12 -0
- data/lib/rbs/type_alias_regularity.rb +6 -4
- data/lib/rbs/type_name.rb +1 -1
- data/lib/rbs/types.rb +1 -1
- data/lib/rbs/validator.rb +6 -0
- data/lib/rbs/version.rb +1 -1
- data/lib/rbs/writer.rb +54 -4
- data/lib/rbs.rb +0 -2
- data/schema/typeParam.json +3 -3
- data/sig/annotate/annotations.rbs +102 -0
- data/sig/annotate/formatter.rbs +24 -0
- data/sig/annotate/rdoc_annotater.rbs +82 -0
- data/sig/annotate/rdoc_source.rbs +30 -0
- data/sig/buffer.rbs +6 -2
- data/sig/cli.rbs +2 -0
- data/sig/collection/{collections.rbs → sources.rbs} +0 -0
- data/sig/location.rbs +6 -0
- data/sig/members.rbs +24 -18
- data/sig/method_builder.rbs +5 -4
- data/sig/method_types.rbs +5 -1
- data/sig/polyfill.rbs +78 -0
- data/sig/validator.rbs +3 -1
- data/sig/writer.rbs +79 -2
- data/stdlib/abbrev/0/abbrev.rbs +6 -0
- data/stdlib/abbrev/0/array.rbs +26 -0
- data/stdlib/base64/0/base64.rbs +31 -0
- data/stdlib/benchmark/0/benchmark.rbs +74 -3
- data/stdlib/bigdecimal/0/big_decimal.rbs +614 -165
- data/stdlib/bigdecimal-math/0/big_math.rbs +41 -64
- data/stdlib/cgi/0/core.rbs +649 -21
- data/stdlib/coverage/0/coverage.rbs +164 -2
- data/stdlib/csv/0/csv.rbs +2862 -398
- data/stdlib/date/0/date.rbs +483 -25
- data/stdlib/date/0/date_time.rbs +187 -12
- data/stdlib/dbm/0/dbm.rbs +152 -17
- data/stdlib/digest/0/digest.rbs +146 -0
- data/stdlib/erb/0/erb.rbs +65 -245
- data/stdlib/fiber/0/fiber.rbs +73 -91
- data/stdlib/fileutils/0/fileutils.rbs +301 -1
- data/stdlib/find/0/find.rbs +9 -0
- data/stdlib/forwardable/0/forwardable.rbs +65 -1
- data/stdlib/io-console/0/io-console.rbs +227 -15
- data/stdlib/ipaddr/0/ipaddr.rbs +161 -0
- data/stdlib/json/0/json.rbs +1147 -145
- data/stdlib/logger/0/formatter.rbs +24 -0
- data/stdlib/logger/0/log_device.rbs +64 -0
- data/stdlib/logger/0/logger.rbs +165 -13
- data/stdlib/logger/0/period.rbs +10 -0
- data/stdlib/logger/0/severity.rbs +26 -0
- data/stdlib/monitor/0/monitor.rbs +163 -0
- data/stdlib/mutex_m/0/mutex_m.rbs +35 -6
- data/stdlib/net-http/0/manifest.yaml +1 -0
- data/stdlib/net-http/0/net-http.rbs +1513 -683
- data/stdlib/nkf/0/nkf.rbs +372 -0
- data/stdlib/objspace/0/objspace.rbs +149 -90
- data/stdlib/openssl/0/openssl.rbs +8108 -71
- data/stdlib/optparse/0/optparse.rbs +487 -19
- data/stdlib/pathname/0/pathname.rbs +425 -124
- data/stdlib/prettyprint/0/prettyprint.rbs +120 -99
- data/stdlib/prime/0/integer-extension.rbs +20 -2
- data/stdlib/prime/0/prime.rbs +88 -21
- data/stdlib/pstore/0/pstore.rbs +102 -0
- data/stdlib/pty/0/pty.rbs +64 -14
- data/stdlib/resolv/0/resolv.rbs +420 -31
- data/stdlib/rubygems/0/basic_specification.rbs +4 -1
- data/stdlib/rubygems/0/config_file.rbs +33 -1
- data/stdlib/rubygems/0/dependency_installer.rbs +4 -3
- data/stdlib/rubygems/0/installer.rbs +13 -1
- data/stdlib/rubygems/0/path_support.rbs +4 -1
- data/stdlib/rubygems/0/platform.rbs +5 -1
- data/stdlib/rubygems/0/request_set.rbs +44 -2
- data/stdlib/rubygems/0/requirement.rbs +65 -2
- data/stdlib/rubygems/0/rubygems.rbs +407 -0
- data/stdlib/rubygems/0/source_list.rbs +13 -0
- data/stdlib/rubygems/0/specification.rbs +21 -1
- data/stdlib/rubygems/0/stream_ui.rbs +3 -1
- data/stdlib/rubygems/0/uninstaller.rbs +8 -1
- data/stdlib/rubygems/0/version.rbs +60 -157
- data/stdlib/securerandom/0/securerandom.rbs +44 -0
- data/stdlib/set/0/set.rbs +423 -109
- data/stdlib/shellwords/0/shellwords.rbs +55 -77
- data/stdlib/singleton/0/singleton.rbs +20 -0
- data/stdlib/socket/0/addrinfo.rbs +210 -9
- data/stdlib/socket/0/basic_socket.rbs +103 -11
- data/stdlib/socket/0/ip_socket.rbs +31 -9
- data/stdlib/socket/0/socket.rbs +586 -38
- data/stdlib/socket/0/tcp_server.rbs +22 -2
- data/stdlib/socket/0/tcp_socket.rbs +12 -1
- data/stdlib/socket/0/udp_socket.rbs +25 -2
- data/stdlib/socket/0/unix_server.rbs +22 -2
- data/stdlib/socket/0/unix_socket.rbs +45 -5
- data/stdlib/strscan/0/string_scanner.rbs +210 -9
- data/stdlib/tempfile/0/tempfile.rbs +58 -10
- data/stdlib/time/0/time.rbs +208 -116
- data/stdlib/timeout/0/timeout.rbs +10 -0
- data/stdlib/tmpdir/0/tmpdir.rbs +13 -4
- data/stdlib/tsort/0/cyclic.rbs +1 -0
- data/stdlib/tsort/0/interfaces.rbs +1 -0
- data/stdlib/tsort/0/tsort.rbs +42 -0
- data/stdlib/uri/0/common.rbs +57 -8
- data/stdlib/uri/0/file.rbs +55 -109
- data/stdlib/uri/0/ftp.rbs +6 -3
- data/stdlib/uri/0/generic.rbs +558 -329
- data/stdlib/uri/0/http.rbs +60 -114
- data/stdlib/uri/0/https.rbs +8 -102
- data/stdlib/uri/0/ldap.rbs +143 -137
- data/stdlib/uri/0/ldaps.rbs +8 -102
- data/stdlib/uri/0/mailto.rbs +3 -0
- data/stdlib/uri/0/rfc2396_parser.rbs +66 -26
- data/stdlib/uri/0/ws.rbs +6 -3
- data/stdlib/uri/0/wss.rbs +5 -3
- data/stdlib/yaml/0/dbm.rbs +151 -87
- data/stdlib/yaml/0/store.rbs +6 -0
- data/stdlib/zlib/0/zlib.rbs +90 -31
- metadata +18 -6
- data/lib/rbs/location.rb +0 -221
- data/sig/char_scanner.rbs +0 -9
data/core/numeric.rbs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
# <!-- rdoc-file=numeric.c -->
|
|
1
2
|
# Numeric is the class from which all higher-level numeric classes should
|
|
2
3
|
# inherit.
|
|
3
4
|
#
|
|
@@ -69,17 +70,206 @@
|
|
|
69
70
|
# puts tally * 2 #=> "||||"
|
|
70
71
|
# puts tally > 1 #=> true
|
|
71
72
|
#
|
|
73
|
+
# ## What's Here
|
|
74
|
+
#
|
|
75
|
+
# First, what's elsewhere. Class Numeric:
|
|
76
|
+
#
|
|
77
|
+
# * Inherits from [class
|
|
78
|
+
# Object](Object.html#class-Object-label-What-27s+Here).
|
|
79
|
+
# * Includes [module
|
|
80
|
+
# Comparable](Comparable.html#module-Comparable-label-What-27s+Here).
|
|
81
|
+
#
|
|
82
|
+
#
|
|
83
|
+
# Here, class Numeric provides methods for:
|
|
84
|
+
#
|
|
85
|
+
# * [Querying](#class-Numeric-label-Querying)
|
|
86
|
+
# * [Comparing](#class-Numeric-label-Comparing)
|
|
87
|
+
# * [Converting](#class-Numeric-label-Converting)
|
|
88
|
+
# * [Other](#class-Numeric-label-Other)
|
|
89
|
+
#
|
|
90
|
+
#
|
|
91
|
+
# ### Querying
|
|
92
|
+
#
|
|
93
|
+
# #finite?
|
|
94
|
+
# : Returns true unless `self` is infinite or not a number.
|
|
95
|
+
#
|
|
96
|
+
# #infinite?
|
|
97
|
+
# : Returns -1, `nil` or +1, depending on whether `self` is
|
|
98
|
+
# `-Infinity<tt>, finite, or <tt>+Infinity`.
|
|
99
|
+
#
|
|
100
|
+
# #integer?
|
|
101
|
+
# : Returns whether `self` is an integer.
|
|
102
|
+
#
|
|
103
|
+
# #negative?
|
|
104
|
+
# : Returns whether `self` is negative.
|
|
105
|
+
#
|
|
106
|
+
# #nonzero?
|
|
107
|
+
# : Returns whether `self` is not zero.
|
|
108
|
+
#
|
|
109
|
+
# #positive?
|
|
110
|
+
# : Returns whether `self` is positive.
|
|
111
|
+
#
|
|
112
|
+
# #real?
|
|
113
|
+
# : Returns whether `self` is a real value.
|
|
114
|
+
#
|
|
115
|
+
# #zero?
|
|
116
|
+
# : Returns whether `self` is zero.
|
|
117
|
+
#
|
|
118
|
+
#
|
|
119
|
+
#
|
|
120
|
+
# ### Comparing
|
|
121
|
+
#
|
|
122
|
+
# [<=>](#method-i-3C-3D-3E)
|
|
123
|
+
# : Returns:
|
|
124
|
+
#
|
|
125
|
+
# * -1 if `self` is less than the given value.
|
|
126
|
+
# * 0 if `self` is equal to the given value.
|
|
127
|
+
# * 1 if `self` is greater than the given value.
|
|
128
|
+
# * `nil` if `self` and the given value are not comparable.
|
|
129
|
+
#
|
|
130
|
+
# #eql?
|
|
131
|
+
# : Returns whether `self` and the given value have the same value and
|
|
132
|
+
# type.
|
|
133
|
+
#
|
|
134
|
+
#
|
|
135
|
+
#
|
|
136
|
+
# ### Converting
|
|
137
|
+
#
|
|
138
|
+
# #% (aliased as #modulo)
|
|
139
|
+
# : Returns the remainder of `self` divided by the given value.
|
|
140
|
+
#
|
|
141
|
+
# #-@
|
|
142
|
+
# : Returns the value of `self`, negated.
|
|
143
|
+
#
|
|
144
|
+
# #abs (aliased as #magnitude)
|
|
145
|
+
# : Returns the absolute value of `self`.
|
|
146
|
+
#
|
|
147
|
+
# #abs2
|
|
148
|
+
# : Returns the square of `self`.
|
|
149
|
+
#
|
|
150
|
+
# #angle (aliased as #arg and #phase)
|
|
151
|
+
# : Returns 0 if `self` is positive, Math::PI otherwise.
|
|
152
|
+
#
|
|
153
|
+
# #ceil
|
|
154
|
+
# : Returns the smallest number greater than or equal to `self`, to a
|
|
155
|
+
# given precision.
|
|
156
|
+
#
|
|
157
|
+
# #coerce
|
|
158
|
+
# : Returns array `[coerced_self, coerced_other]` for the given other
|
|
159
|
+
# value.
|
|
160
|
+
#
|
|
161
|
+
# #conj (aliased as #conjugate)
|
|
162
|
+
# : Returns the complex conjugate of `self`.
|
|
163
|
+
#
|
|
164
|
+
# #denominator
|
|
165
|
+
# : Returns the denominator (always positive) of the Rational
|
|
166
|
+
# representation of `self`.
|
|
167
|
+
#
|
|
168
|
+
# #div
|
|
169
|
+
# : Returns the value of `self` divided by the given value and converted
|
|
170
|
+
# to an integer.
|
|
171
|
+
#
|
|
172
|
+
# #divmod
|
|
173
|
+
# : Returns array `[quotient, modulus]` resulting from dividing `self` the
|
|
174
|
+
# given divisor.
|
|
175
|
+
#
|
|
176
|
+
# #fdiv
|
|
177
|
+
# : Returns the Float result of dividing `self` by the given divisor.
|
|
178
|
+
#
|
|
179
|
+
# #floor
|
|
180
|
+
# : Returns the largest number less than or equal to `self`, to a given
|
|
181
|
+
# precision.
|
|
182
|
+
#
|
|
183
|
+
# #i
|
|
184
|
+
# : Returns the Complex object `Complex(0, self)`. the given value.
|
|
185
|
+
#
|
|
186
|
+
# #imaginary (aliased as #imag)
|
|
187
|
+
# : Returns the imaginary part of the `self`.
|
|
188
|
+
#
|
|
189
|
+
# #numerator
|
|
190
|
+
# : Returns the numerator of the Rational representation of `self`; has
|
|
191
|
+
# the same sign as `self`.
|
|
192
|
+
#
|
|
193
|
+
# #polar
|
|
194
|
+
# : Returns the array `[self.abs, self.arg]`.
|
|
195
|
+
#
|
|
196
|
+
# #quo
|
|
197
|
+
# : Returns the value of `self` divided by the given value.
|
|
198
|
+
#
|
|
199
|
+
# #real
|
|
200
|
+
# : Returns the real part of `self`.
|
|
201
|
+
#
|
|
202
|
+
# #rect (aliased as #rectangular)
|
|
203
|
+
# : Returns the array `[self, 0]`.
|
|
204
|
+
#
|
|
205
|
+
# #remainder
|
|
206
|
+
# : Returns `self-arg*(self/arg).truncate` for the given `arg`.
|
|
207
|
+
#
|
|
208
|
+
# #round
|
|
209
|
+
# : Returns the value of `self` rounded to the nearest value for the given
|
|
210
|
+
# a precision.
|
|
211
|
+
#
|
|
212
|
+
# #to_c
|
|
213
|
+
# : Returns the Complex representation of `self`.
|
|
214
|
+
#
|
|
215
|
+
# #to_int
|
|
216
|
+
# : Returns the Integer representation of `self`, truncating if necessary.
|
|
217
|
+
#
|
|
218
|
+
# #truncate
|
|
219
|
+
# : Returns `self` truncated (toward zero) to a given precision.
|
|
220
|
+
#
|
|
221
|
+
#
|
|
222
|
+
#
|
|
223
|
+
# ### Other
|
|
224
|
+
#
|
|
225
|
+
# #clone
|
|
226
|
+
# : Returns `self`; does not allow freezing.
|
|
227
|
+
#
|
|
228
|
+
# #dup (aliased as #+@)
|
|
229
|
+
# : Returns `self`.
|
|
230
|
+
#
|
|
231
|
+
# #step
|
|
232
|
+
# : Invokes the given block with the sequence of specified numbers.
|
|
233
|
+
#
|
|
72
234
|
class Numeric
|
|
73
235
|
include Comparable
|
|
74
236
|
|
|
75
237
|
public
|
|
76
238
|
|
|
77
|
-
#
|
|
239
|
+
# <!--
|
|
240
|
+
# rdoc-file=numeric.c
|
|
241
|
+
# - self % other -> real_numeric
|
|
242
|
+
# -->
|
|
243
|
+
# Returns `self` modulo `other` as a real number.
|
|
244
|
+
#
|
|
245
|
+
# Of the Core and Standard Library classes, only Rational uses this
|
|
246
|
+
# implementation.
|
|
78
247
|
#
|
|
79
|
-
#
|
|
248
|
+
# For Rational `r` and real number `n`, these expressions are equivalent:
|
|
249
|
+
#
|
|
250
|
+
# c % n
|
|
251
|
+
# c-n*(c/n).floor
|
|
252
|
+
# c.divmod(n)[1]
|
|
80
253
|
#
|
|
81
254
|
# See Numeric#divmod.
|
|
82
255
|
#
|
|
256
|
+
# Examples:
|
|
257
|
+
#
|
|
258
|
+
# r = Rational(1, 2) # => (1/2)
|
|
259
|
+
# r2 = Rational(2, 3) # => (2/3)
|
|
260
|
+
# r % r2 # => (1/2)
|
|
261
|
+
# r % 2 # => (1/2)
|
|
262
|
+
# r % 2.0 # => 0.5
|
|
263
|
+
#
|
|
264
|
+
# r = Rational(301,100) # => (301/100)
|
|
265
|
+
# r2 = Rational(7,5) # => (7/5)
|
|
266
|
+
# r % r2 # => (21/100)
|
|
267
|
+
# r % -r2 # => (-119/100)
|
|
268
|
+
# (-r) % r2 # => (119/100)
|
|
269
|
+
# (-r) %-r2 # => (-21/100)
|
|
270
|
+
#
|
|
271
|
+
# Numeric#modulo is an alias for Numeric#%.
|
|
272
|
+
#
|
|
83
273
|
def %: (Numeric) -> Numeric
|
|
84
274
|
|
|
85
275
|
# Performs addition: the class of the resulting object depends on the class of
|
|
@@ -87,7 +277,11 @@ class Numeric
|
|
|
87
277
|
#
|
|
88
278
|
def +: (Numeric) -> Numeric
|
|
89
279
|
|
|
90
|
-
#
|
|
280
|
+
# <!--
|
|
281
|
+
# rdoc-file=numeric.c
|
|
282
|
+
# - +self -> self
|
|
283
|
+
# -->
|
|
284
|
+
# Returns `self`.
|
|
91
285
|
#
|
|
92
286
|
def +@: () -> Numeric
|
|
93
287
|
|
|
@@ -96,165 +290,267 @@ class Numeric
|
|
|
96
290
|
#
|
|
97
291
|
def -: (Numeric) -> Numeric
|
|
98
292
|
|
|
293
|
+
# <!--
|
|
294
|
+
# rdoc-file=numeric.c
|
|
295
|
+
# - -self -> numeric
|
|
296
|
+
# -->
|
|
99
297
|
# Unary Minus---Returns the receiver, negated.
|
|
100
298
|
#
|
|
101
299
|
def -@: () -> Numeric
|
|
102
300
|
|
|
103
|
-
#
|
|
301
|
+
# <!--
|
|
302
|
+
# rdoc-file=numeric.c
|
|
303
|
+
# - self <=> other -> zero or nil
|
|
304
|
+
# -->
|
|
305
|
+
# Returns zero if `self` is the same as `other`, `nil` otherwise.
|
|
306
|
+
#
|
|
307
|
+
# No subclass in the Ruby Core or Standard Library uses this implementation.
|
|
104
308
|
#
|
|
105
309
|
def <=>: (Numeric other) -> Integer
|
|
106
310
|
|
|
107
|
-
#
|
|
311
|
+
# <!--
|
|
312
|
+
# rdoc-file=numeric.c
|
|
313
|
+
# - abs -> numeric
|
|
314
|
+
# -->
|
|
315
|
+
# Returns the absolute value of `self`.
|
|
108
316
|
#
|
|
109
|
-
# 12.abs
|
|
110
|
-
# (-34.56).abs
|
|
111
|
-
# -34.56.abs
|
|
317
|
+
# 12.abs #=> 12
|
|
318
|
+
# (-34.56).abs #=> 34.56
|
|
319
|
+
# -34.56.abs #=> 34.56
|
|
112
320
|
#
|
|
113
321
|
# Numeric#magnitude is an alias for Numeric#abs.
|
|
114
322
|
#
|
|
115
323
|
def abs: () -> Numeric
|
|
116
324
|
|
|
325
|
+
# <!--
|
|
326
|
+
# rdoc-file=complex.c
|
|
327
|
+
# - num.abs2 -> real
|
|
328
|
+
# -->
|
|
117
329
|
# Returns square of self.
|
|
118
330
|
#
|
|
119
331
|
def abs2: () -> Numeric
|
|
120
332
|
|
|
333
|
+
# <!-- rdoc-file=complex.c -->
|
|
121
334
|
# Returns 0 if the value is positive, pi otherwise.
|
|
122
335
|
#
|
|
123
336
|
def angle: () -> Numeric
|
|
124
337
|
|
|
338
|
+
# <!--
|
|
339
|
+
# rdoc-file=complex.c
|
|
340
|
+
# - num.arg -> 0 or float
|
|
341
|
+
# - num.angle -> 0 or float
|
|
342
|
+
# - num.phase -> 0 or float
|
|
343
|
+
# -->
|
|
125
344
|
# Returns 0 if the value is positive, pi otherwise.
|
|
126
345
|
#
|
|
127
346
|
alias arg angle
|
|
128
347
|
|
|
129
|
-
#
|
|
130
|
-
#
|
|
348
|
+
# <!--
|
|
349
|
+
# rdoc-file=numeric.c
|
|
350
|
+
# - ceil(digits = 0) -> integer or float
|
|
351
|
+
# -->
|
|
352
|
+
# Returns the smallest number that is greater than or equal to `self` with a
|
|
353
|
+
# precision of `digits` decimal digits.
|
|
131
354
|
#
|
|
132
|
-
# Numeric implements this by converting
|
|
355
|
+
# Numeric implements this by converting `self` to a Float and invoking
|
|
133
356
|
# Float#ceil.
|
|
134
357
|
#
|
|
135
358
|
def ceil: () -> Integer
|
|
136
359
|
| (Integer digits) -> (Integer | Numeric)
|
|
137
360
|
|
|
138
|
-
#
|
|
139
|
-
#
|
|
140
|
-
#
|
|
361
|
+
# <!--
|
|
362
|
+
# rdoc-file=numeric.c
|
|
363
|
+
# - coerce(other) -> array
|
|
364
|
+
# -->
|
|
365
|
+
# Returns a 2-element array containing two numeric elements, formed from the two
|
|
366
|
+
# operands `self` and `other`, of a common compatible type.
|
|
367
|
+
#
|
|
368
|
+
# Of the Core and Standard Library classes, Integer, Rational, and Complex use
|
|
369
|
+
# this implementation.
|
|
370
|
+
#
|
|
371
|
+
# Examples:
|
|
372
|
+
#
|
|
373
|
+
# i = 2 # => 2
|
|
374
|
+
# i.coerce(3) # => [3, 2]
|
|
375
|
+
# i.coerce(3.0) # => [3.0, 2.0]
|
|
376
|
+
# i.coerce(Rational(1, 2)) # => [0.5, 2.0]
|
|
377
|
+
# i.coerce(Complex(3, 4)) # Raises RangeError.
|
|
378
|
+
#
|
|
379
|
+
# r = Rational(5, 2) # => (5/2)
|
|
380
|
+
# r.coerce(2) # => [(2/1), (5/2)]
|
|
381
|
+
# r.coerce(2.0) # => [2.0, 2.5]
|
|
382
|
+
# r.coerce(Rational(2, 3)) # => [(2/3), (5/2)]
|
|
383
|
+
# r.coerce(Complex(3, 4)) # => [(3+4i), ((5/2)+0i)]
|
|
141
384
|
#
|
|
142
|
-
#
|
|
143
|
-
#
|
|
144
|
-
#
|
|
385
|
+
# c = Complex(2, 3) # => (2+3i)
|
|
386
|
+
# c.coerce(2) # => [(2+0i), (2+3i)]
|
|
387
|
+
# c.coerce(2.0) # => [(2.0+0i), (2+3i)]
|
|
388
|
+
# c.coerce(Rational(1, 2)) # => [((1/2)+0i), (2+3i)]
|
|
389
|
+
# c.coerce(Complex(3, 4)) # => [(3+4i), (2+3i)]
|
|
145
390
|
#
|
|
146
|
-
#
|
|
147
|
-
# 1.2.coerce(3) #=> [3.0, 1.2]
|
|
148
|
-
# 1.coerce(2) #=> [2, 1]
|
|
391
|
+
# Raises an exception if any type conversion fails.
|
|
149
392
|
#
|
|
150
393
|
def coerce: (Numeric) -> [ Numeric, Numeric ]
|
|
151
394
|
|
|
395
|
+
# <!-- rdoc-file=complex.c -->
|
|
152
396
|
# Returns self.
|
|
153
397
|
#
|
|
154
398
|
def conj: () -> Numeric
|
|
155
399
|
|
|
400
|
+
# <!--
|
|
401
|
+
# rdoc-file=complex.c
|
|
402
|
+
# - num.conj -> self
|
|
403
|
+
# - num.conjugate -> self
|
|
404
|
+
# -->
|
|
156
405
|
# Returns self.
|
|
157
406
|
#
|
|
158
407
|
def conjugate: () -> Numeric
|
|
159
408
|
|
|
409
|
+
# <!--
|
|
410
|
+
# rdoc-file=rational.c
|
|
411
|
+
# - num.denominator -> integer
|
|
412
|
+
# -->
|
|
160
413
|
# Returns the denominator (always positive).
|
|
161
414
|
#
|
|
162
415
|
def denominator: () -> Integer
|
|
163
416
|
|
|
164
|
-
#
|
|
165
|
-
#
|
|
417
|
+
# <!--
|
|
418
|
+
# rdoc-file=numeric.c
|
|
419
|
+
# - div(other) -> integer
|
|
420
|
+
# -->
|
|
421
|
+
# Returns the quotient `self/other` as an integer (via `floor`), using method
|
|
422
|
+
# `/` in the derived class of `self`. (Numeric itself does not define method
|
|
423
|
+
# `/`.)
|
|
166
424
|
#
|
|
167
|
-
#
|
|
168
|
-
#
|
|
169
|
-
# See Numeric#divmod.
|
|
425
|
+
# Of the Core and Standard Library classes, Float, Rational, and Complex use
|
|
426
|
+
# this implementation.
|
|
170
427
|
#
|
|
171
428
|
def div: (Numeric) -> Integer
|
|
172
429
|
|
|
173
|
-
#
|
|
174
|
-
#
|
|
175
|
-
#
|
|
176
|
-
#
|
|
177
|
-
#
|
|
178
|
-
#
|
|
179
|
-
#
|
|
180
|
-
#
|
|
181
|
-
#
|
|
182
|
-
#
|
|
183
|
-
#
|
|
184
|
-
#
|
|
185
|
-
#
|
|
186
|
-
#
|
|
187
|
-
#
|
|
188
|
-
#
|
|
189
|
-
#
|
|
190
|
-
# -
|
|
191
|
-
#
|
|
192
|
-
#
|
|
193
|
-
#
|
|
194
|
-
#
|
|
195
|
-
#
|
|
196
|
-
#
|
|
197
|
-
#
|
|
198
|
-
#
|
|
199
|
-
# ------+-----+---------------+---------+-------------+---------------
|
|
200
|
-
# -11.5 | -4 | 2, -3.5 | 2.875 | -3.5 | -3.5
|
|
201
|
-
#
|
|
202
|
-
# Examples
|
|
203
|
-
#
|
|
204
|
-
# 11.divmod(3) #=> [3, 2]
|
|
205
|
-
# 11.divmod(-3) #=> [-4, -1]
|
|
206
|
-
# 11.divmod(3.5) #=> [3, 0.5]
|
|
207
|
-
# (-11).divmod(3.5) #=> [-4, 3.0]
|
|
208
|
-
# 11.5.divmod(3.5) #=> [3, 1.0]
|
|
430
|
+
# <!--
|
|
431
|
+
# rdoc-file=numeric.c
|
|
432
|
+
# - divmod(other) -> array
|
|
433
|
+
# -->
|
|
434
|
+
# Returns a 2-element array `[q, r]`, where
|
|
435
|
+
#
|
|
436
|
+
# q = (self/other).floor # Quotient
|
|
437
|
+
# r = self % other # Remainder
|
|
438
|
+
#
|
|
439
|
+
# Of the Core and Standard Library classes, only Rational uses this
|
|
440
|
+
# implementation.
|
|
441
|
+
#
|
|
442
|
+
# Examples:
|
|
443
|
+
#
|
|
444
|
+
# Rational(11, 1).divmod(4) # => [2, (3/1)]
|
|
445
|
+
# Rational(11, 1).divmod(-4) # => [-3, (-1/1)]
|
|
446
|
+
# Rational(-11, 1).divmod(4) # => [-3, (1/1)]
|
|
447
|
+
# Rational(-11, 1).divmod(-4) # => [2, (-3/1)]
|
|
448
|
+
#
|
|
449
|
+
# Rational(12, 1).divmod(4) # => [3, (0/1)]
|
|
450
|
+
# Rational(12, 1).divmod(-4) # => [-3, (0/1)]
|
|
451
|
+
# Rational(-12, 1).divmod(4) # => [-3, (0/1)]
|
|
452
|
+
# Rational(-12, 1).divmod(-4) # => [3, (0/1)]
|
|
453
|
+
#
|
|
454
|
+
# Rational(13, 1).divmod(4.0) # => [3, 1.0]
|
|
455
|
+
# Rational(13, 1).divmod(Rational(4, 11)) # => [35, (3/11)]
|
|
209
456
|
#
|
|
210
457
|
def divmod: (Numeric) -> [ Numeric, Numeric ]
|
|
211
458
|
|
|
212
|
-
#
|
|
213
|
-
#
|
|
459
|
+
# <!--
|
|
460
|
+
# rdoc-file=numeric.c
|
|
461
|
+
# - eql?(other) -> true or false
|
|
462
|
+
# -->
|
|
463
|
+
# Returns `true` if `self` and `other` are the same type and have equal values.
|
|
464
|
+
#
|
|
465
|
+
# Of the Core and Standard Library classes, only Integer, Rational, and Complex
|
|
466
|
+
# use this implementation.
|
|
467
|
+
#
|
|
468
|
+
# Examples:
|
|
214
469
|
#
|
|
215
|
-
# 1
|
|
216
|
-
# 1.eql?(1.0)
|
|
217
|
-
# 1.
|
|
470
|
+
# 1.eql?(1) # => true
|
|
471
|
+
# 1.eql?(1.0) # => false
|
|
472
|
+
# 1.eql?(Rational(1, 1)) # => false
|
|
473
|
+
# 1.eql?(Complex(1, 0)) # => false
|
|
474
|
+
#
|
|
475
|
+
# Method `eql?` is different from +==+ in that `eql?` requires matching types,
|
|
476
|
+
# while +==+ does not.
|
|
218
477
|
#
|
|
219
478
|
def eql?: (untyped) -> bool
|
|
220
479
|
|
|
221
|
-
#
|
|
480
|
+
# <!--
|
|
481
|
+
# rdoc-file=numeric.c
|
|
482
|
+
# - fdiv(other) -> float
|
|
483
|
+
# -->
|
|
484
|
+
# Returns the quotient `self/other` as a float, using method `/` in the derived
|
|
485
|
+
# class of `self`. (Numeric itself does not define method `/`.)
|
|
486
|
+
#
|
|
487
|
+
# Of the Core and Standard Library classes, only BigDecimal uses this
|
|
488
|
+
# implementation.
|
|
222
489
|
#
|
|
223
490
|
def fdiv: (Numeric) -> Numeric
|
|
224
491
|
|
|
492
|
+
# <!--
|
|
493
|
+
# rdoc-file=numeric.rb
|
|
494
|
+
# - num.finite? -> true or false
|
|
495
|
+
# -->
|
|
225
496
|
# Returns `true` if `num` is a finite number, otherwise returns `false`.
|
|
226
497
|
#
|
|
227
498
|
def finite?: () -> bool
|
|
228
499
|
|
|
229
|
-
#
|
|
230
|
-
#
|
|
500
|
+
# <!--
|
|
501
|
+
# rdoc-file=numeric.c
|
|
502
|
+
# - floor(digits = 0) -> integer or float
|
|
503
|
+
# -->
|
|
504
|
+
# Returns the largest number that is less than or equal to `self` with a
|
|
505
|
+
# precision of `digits` decimal digits.
|
|
231
506
|
#
|
|
232
|
-
# Numeric implements this by converting
|
|
507
|
+
# Numeric implements this by converting `self` to a Float and invoking
|
|
233
508
|
# Float#floor.
|
|
234
509
|
#
|
|
235
510
|
def floor: () -> Integer
|
|
236
511
|
| (Integer digits) -> Numeric
|
|
237
512
|
|
|
238
|
-
#
|
|
513
|
+
# <!--
|
|
514
|
+
# rdoc-file=numeric.c
|
|
515
|
+
# - i -> complex
|
|
516
|
+
# -->
|
|
517
|
+
# Returns `Complex(0, self)`:
|
|
239
518
|
#
|
|
240
|
-
#
|
|
241
|
-
# 2.
|
|
519
|
+
# 2.i # => (0+2i)
|
|
520
|
+
# -2.i # => (0-2i)
|
|
521
|
+
# 2.0.i # => (0+2.0i)
|
|
522
|
+
# Rational(1, 2).i # => (0+(1/2)*i)
|
|
523
|
+
# Complex(3, 4).i # Raises NoMethodError.
|
|
242
524
|
#
|
|
243
525
|
def i: () -> Complex
|
|
244
526
|
|
|
527
|
+
# <!-- rdoc-file=complex.c -->
|
|
245
528
|
# Returns zero.
|
|
246
529
|
#
|
|
247
530
|
def imag: () -> Numeric
|
|
248
531
|
|
|
532
|
+
# <!--
|
|
533
|
+
# rdoc-file=complex.c
|
|
534
|
+
# - num.imag -> 0
|
|
535
|
+
# - num.imaginary -> 0
|
|
536
|
+
# -->
|
|
249
537
|
# Returns zero.
|
|
250
538
|
#
|
|
251
539
|
def imaginary: () -> Numeric
|
|
252
540
|
|
|
541
|
+
# <!--
|
|
542
|
+
# rdoc-file=numeric.rb
|
|
543
|
+
# - num.infinite? -> -1, 1, or nil
|
|
544
|
+
# -->
|
|
253
545
|
# Returns `nil`, -1, or 1 depending on whether the value is finite, `-Infinity`,
|
|
254
546
|
# or `+Infinity`.
|
|
255
547
|
#
|
|
256
548
|
def infinite?: () -> Integer?
|
|
257
549
|
|
|
550
|
+
# <!--
|
|
551
|
+
# rdoc-file=numeric.rb
|
|
552
|
+
# - num.integer? -> true or false
|
|
553
|
+
# -->
|
|
258
554
|
# Returns `true` if `num` is an Integer.
|
|
259
555
|
#
|
|
260
556
|
# 1.0.integer? #=> false
|
|
@@ -262,165 +558,347 @@ class Numeric
|
|
|
262
558
|
#
|
|
263
559
|
def integer?: () -> bool
|
|
264
560
|
|
|
265
|
-
#
|
|
561
|
+
# <!-- rdoc-file=numeric.c -->
|
|
562
|
+
# Returns the absolute value of `self`.
|
|
266
563
|
#
|
|
267
|
-
# 12.abs
|
|
268
|
-
# (-34.56).abs
|
|
269
|
-
# -34.56.abs
|
|
564
|
+
# 12.abs #=> 12
|
|
565
|
+
# (-34.56).abs #=> 34.56
|
|
566
|
+
# -34.56.abs #=> 34.56
|
|
270
567
|
#
|
|
271
568
|
# Numeric#magnitude is an alias for Numeric#abs.
|
|
272
569
|
#
|
|
273
570
|
alias magnitude abs
|
|
274
571
|
|
|
275
|
-
#
|
|
572
|
+
# <!-- rdoc-file=numeric.c -->
|
|
573
|
+
# Returns `self` modulo `other` as a real number.
|
|
574
|
+
#
|
|
575
|
+
# Of the Core and Standard Library classes, only Rational uses this
|
|
576
|
+
# implementation.
|
|
276
577
|
#
|
|
277
|
-
#
|
|
578
|
+
# For Rational `r` and real number `n`, these expressions are equivalent:
|
|
579
|
+
#
|
|
580
|
+
# c % n
|
|
581
|
+
# c-n*(c/n).floor
|
|
582
|
+
# c.divmod(n)[1]
|
|
278
583
|
#
|
|
279
584
|
# See Numeric#divmod.
|
|
280
585
|
#
|
|
586
|
+
# Examples:
|
|
587
|
+
#
|
|
588
|
+
# r = Rational(1, 2) # => (1/2)
|
|
589
|
+
# r2 = Rational(2, 3) # => (2/3)
|
|
590
|
+
# r % r2 # => (1/2)
|
|
591
|
+
# r % 2 # => (1/2)
|
|
592
|
+
# r % 2.0 # => 0.5
|
|
593
|
+
#
|
|
594
|
+
# r = Rational(301,100) # => (301/100)
|
|
595
|
+
# r2 = Rational(7,5) # => (7/5)
|
|
596
|
+
# r % r2 # => (21/100)
|
|
597
|
+
# r % -r2 # => (-119/100)
|
|
598
|
+
# (-r) % r2 # => (119/100)
|
|
599
|
+
# (-r) %-r2 # => (-21/100)
|
|
600
|
+
#
|
|
601
|
+
# Numeric#modulo is an alias for Numeric#%.
|
|
602
|
+
#
|
|
281
603
|
def modulo: (Numeric) -> Numeric
|
|
282
604
|
|
|
283
|
-
#
|
|
605
|
+
# <!--
|
|
606
|
+
# rdoc-file=numeric.c
|
|
607
|
+
# - negative? -> true or false
|
|
608
|
+
# -->
|
|
609
|
+
# Returns `true` if `self` is less than 0, `false` otherwise.
|
|
284
610
|
#
|
|
285
611
|
def negative?: () -> bool
|
|
286
612
|
|
|
287
|
-
#
|
|
613
|
+
# <!--
|
|
614
|
+
# rdoc-file=numeric.c
|
|
615
|
+
# - nonzero? -> self or nil
|
|
616
|
+
# -->
|
|
617
|
+
# Returns `self` if `self` is not a zero value, `nil` otherwise; uses method
|
|
618
|
+
# `zero?` for the evaluation.
|
|
619
|
+
#
|
|
620
|
+
# The returned `self` allows the method to be chained:
|
|
288
621
|
#
|
|
289
|
-
#
|
|
622
|
+
# a = %w[z Bb bB bb BB a aA Aa AA A]
|
|
623
|
+
# a.sort {|a, b| (a.downcase <=> b.downcase).nonzero? || a <=> b }
|
|
624
|
+
# # => ["A", "a", "AA", "Aa", "aA", "BB", "Bb", "bB", "bb", "z"]
|
|
290
625
|
#
|
|
291
|
-
#
|
|
292
|
-
#
|
|
293
|
-
# b #=> ["A", "a", "AA", "Aa", "aA", "BB", "Bb", "bB", "bb", "z"]
|
|
626
|
+
# Of the Core and Standard Library classes, Integer, Float, Rational, and
|
|
627
|
+
# Complex use this implementation.
|
|
294
628
|
#
|
|
295
629
|
def nonzero?: () -> self?
|
|
296
630
|
|
|
631
|
+
# <!--
|
|
632
|
+
# rdoc-file=rational.c
|
|
633
|
+
# - num.numerator -> integer
|
|
634
|
+
# -->
|
|
297
635
|
# Returns the numerator.
|
|
298
636
|
#
|
|
299
637
|
def numerator: () -> Numeric
|
|
300
638
|
|
|
639
|
+
# <!-- rdoc-file=complex.c -->
|
|
301
640
|
# Returns 0 if the value is positive, pi otherwise.
|
|
302
641
|
#
|
|
303
642
|
alias phase angle
|
|
304
643
|
|
|
644
|
+
# <!--
|
|
645
|
+
# rdoc-file=complex.c
|
|
646
|
+
# - num.polar -> array
|
|
647
|
+
# -->
|
|
305
648
|
# Returns an array; [num.abs, num.arg].
|
|
306
649
|
#
|
|
307
650
|
def polar: () -> [ Numeric, Numeric ]
|
|
308
651
|
|
|
309
|
-
#
|
|
652
|
+
# <!--
|
|
653
|
+
# rdoc-file=numeric.c
|
|
654
|
+
# - positive? -> true or false
|
|
655
|
+
# -->
|
|
656
|
+
# Returns `true` if `self` is greater than 0, `false` otherwise.
|
|
310
657
|
#
|
|
311
658
|
def positive?: () -> bool
|
|
312
659
|
|
|
660
|
+
# <!--
|
|
661
|
+
# rdoc-file=rational.c
|
|
662
|
+
# - num.quo(int_or_rat) -> rat
|
|
663
|
+
# - num.quo(flo) -> flo
|
|
664
|
+
# -->
|
|
313
665
|
# Returns the most exact division (rational for integers, float for floats).
|
|
314
666
|
#
|
|
315
667
|
def quo: (Numeric) -> Numeric
|
|
316
668
|
|
|
669
|
+
# <!--
|
|
670
|
+
# rdoc-file=complex.c
|
|
671
|
+
# - num.real -> self
|
|
672
|
+
# -->
|
|
317
673
|
# Returns self.
|
|
318
674
|
#
|
|
319
675
|
def real: () -> Numeric
|
|
320
676
|
|
|
677
|
+
# <!--
|
|
678
|
+
# rdoc-file=numeric.rb
|
|
679
|
+
# - num.real? -> true or false
|
|
680
|
+
# -->
|
|
321
681
|
# Returns `true` if `num` is a real number (i.e. not Complex).
|
|
322
682
|
#
|
|
323
683
|
def real?: () -> bool
|
|
324
684
|
|
|
685
|
+
# <!-- rdoc-file=complex.c -->
|
|
325
686
|
# Returns an array; [num, 0].
|
|
326
687
|
#
|
|
327
688
|
def rect: () -> [ Numeric, Numeric ]
|
|
328
689
|
|
|
690
|
+
# <!--
|
|
691
|
+
# rdoc-file=complex.c
|
|
692
|
+
# - num.rect -> array
|
|
693
|
+
# - num.rectangular -> array
|
|
694
|
+
# -->
|
|
329
695
|
# Returns an array; [num, 0].
|
|
330
696
|
#
|
|
331
697
|
alias rectangular rect
|
|
332
698
|
|
|
333
|
-
#
|
|
699
|
+
# <!--
|
|
700
|
+
# rdoc-file=numeric.c
|
|
701
|
+
# - remainder(other) -> real_number
|
|
702
|
+
# -->
|
|
703
|
+
# Returns the remainder after dividing `self` by `other`.
|
|
334
704
|
#
|
|
335
|
-
#
|
|
705
|
+
# Of the Core and Standard Library classes, only Float and Rational use this
|
|
706
|
+
# implementation.
|
|
707
|
+
#
|
|
708
|
+
# Examples:
|
|
709
|
+
#
|
|
710
|
+
# 11.0.remainder(4) # => 3.0
|
|
711
|
+
# 11.0.remainder(-4) # => 3.0
|
|
712
|
+
# -11.0.remainder(4) # => -3.0
|
|
713
|
+
# -11.0.remainder(-4) # => -3.0
|
|
714
|
+
#
|
|
715
|
+
# 12.0.remainder(4) # => 0.0
|
|
716
|
+
# 12.0.remainder(-4) # => 0.0
|
|
717
|
+
# -12.0.remainder(4) # => -0.0
|
|
718
|
+
# -12.0.remainder(-4) # => -0.0
|
|
719
|
+
#
|
|
720
|
+
# 13.0.remainder(4.0) # => 1.0
|
|
721
|
+
# 13.0.remainder(Rational(4, 1)) # => 1.0
|
|
722
|
+
#
|
|
723
|
+
# Rational(13, 1).remainder(4) # => (1/1)
|
|
724
|
+
# Rational(13, 1).remainder(-4) # => (1/1)
|
|
725
|
+
# Rational(-13, 1).remainder(4) # => (-1/1)
|
|
726
|
+
# Rational(-13, 1).remainder(-4) # => (-1/1)
|
|
336
727
|
#
|
|
337
728
|
def remainder: (Numeric) -> Numeric
|
|
338
729
|
|
|
339
|
-
#
|
|
340
|
-
#
|
|
730
|
+
# <!--
|
|
731
|
+
# rdoc-file=numeric.c
|
|
732
|
+
# - round(digits = 0) -> integer or float
|
|
733
|
+
# -->
|
|
734
|
+
# Returns `self` rounded to the nearest value with a precision of `digits`
|
|
735
|
+
# decimal digits.
|
|
341
736
|
#
|
|
342
|
-
# Numeric implements this by converting
|
|
737
|
+
# Numeric implements this by converting `self` to a Float and invoking
|
|
343
738
|
# Float#round.
|
|
344
739
|
#
|
|
345
740
|
def round: () -> Integer
|
|
346
741
|
| (Integer digits) -> Numeric
|
|
347
742
|
|
|
348
|
-
#
|
|
349
|
-
#
|
|
350
|
-
#
|
|
351
|
-
#
|
|
352
|
-
#
|
|
353
|
-
#
|
|
354
|
-
#
|
|
355
|
-
#
|
|
356
|
-
#
|
|
357
|
-
#
|
|
358
|
-
#
|
|
359
|
-
#
|
|
360
|
-
#
|
|
361
|
-
#
|
|
362
|
-
#
|
|
363
|
-
#
|
|
364
|
-
#
|
|
365
|
-
#
|
|
366
|
-
#
|
|
367
|
-
#
|
|
368
|
-
#
|
|
369
|
-
#
|
|
370
|
-
#
|
|
371
|
-
#
|
|
372
|
-
#
|
|
373
|
-
#
|
|
374
|
-
#
|
|
375
|
-
#
|
|
376
|
-
#
|
|
377
|
-
#
|
|
378
|
-
#
|
|
379
|
-
#
|
|
380
|
-
#
|
|
381
|
-
#
|
|
382
|
-
#
|
|
383
|
-
#
|
|
384
|
-
#
|
|
385
|
-
#
|
|
386
|
-
#
|
|
387
|
-
#
|
|
388
|
-
#
|
|
743
|
+
# <!--
|
|
744
|
+
# rdoc-file=numeric.c
|
|
745
|
+
# - step(to = nil, by = 1) {|n| ... } -> self
|
|
746
|
+
# - step(to = nil, by = 1) -> enumerator
|
|
747
|
+
# - step(to = nil, by: 1) {|n| ... } -> self
|
|
748
|
+
# - step(to = nil, by: 1) -> enumerator
|
|
749
|
+
# - step(by: 1, to: ) {|n| ... } -> self
|
|
750
|
+
# - step(by: 1, to: ) -> enumerator
|
|
751
|
+
# - step(by: , to: nil) {|n| ... } -> self
|
|
752
|
+
# - step(by: , to: nil) -> enumerator
|
|
753
|
+
# -->
|
|
754
|
+
# Generates a sequence of numbers; with a block given, traverses the sequence.
|
|
755
|
+
#
|
|
756
|
+
# Of the Core and Standard Library classes,
|
|
757
|
+
# Integer, Float, and Rational use this implementation.
|
|
758
|
+
#
|
|
759
|
+
# A quick example:
|
|
760
|
+
#
|
|
761
|
+
# squares = []
|
|
762
|
+
# 1.step(by: 2, to: 10) {|i| squares.push(i*i) }
|
|
763
|
+
# squares # => [1, 9, 25, 49, 81]
|
|
764
|
+
#
|
|
765
|
+
# The generated sequence:
|
|
766
|
+
#
|
|
767
|
+
# - Begins with +self+.
|
|
768
|
+
# - Continues at intervals of +step+ (which may not be zero).
|
|
769
|
+
# - Ends with the last number that is within or equal to +limit+;
|
|
770
|
+
# that is, less than or equal to +limit+ if +step+ is positive,
|
|
771
|
+
# greater than or equal to +limit+ if +step+ is negative.
|
|
772
|
+
# If +limit+ is not given, the sequence is of infinite length.
|
|
773
|
+
#
|
|
774
|
+
# If a block is given, calls the block with each number in the sequence;
|
|
775
|
+
# returns +self+. If no block is given, returns an Enumerator::ArithmeticSequence.
|
|
776
|
+
#
|
|
777
|
+
# <b>Keyword Arguments</b>
|
|
778
|
+
#
|
|
779
|
+
# With keyword arguments +by+ and +to+,
|
|
780
|
+
# their values (or defaults) determine the step and limit:
|
|
781
|
+
#
|
|
782
|
+
# # Both keywords given.
|
|
783
|
+
# squares = []
|
|
784
|
+
# 4.step(by: 2, to: 10) {|i| squares.push(i*i) } # => 4
|
|
785
|
+
# squares # => [16, 36, 64, 100]
|
|
786
|
+
# cubes = []
|
|
787
|
+
# 3.step(by: -1.5, to: -3) {|i| cubes.push(i*i*i) } # => 3
|
|
788
|
+
# cubes # => [27.0, 3.375, 0.0, -3.375, -27.0]
|
|
789
|
+
# squares = []
|
|
790
|
+
# 1.2.step(by: 0.2, to: 2.0) {|f| squares.push(f*f) }
|
|
791
|
+
# squares # => [1.44, 1.9599999999999997, 2.5600000000000005, 3.24, 4.0]
|
|
792
|
+
#
|
|
793
|
+
# squares = []
|
|
794
|
+
# Rational(6/5).step(by: 0.2, to: 2.0) {|r| squares.push(r*r) }
|
|
795
|
+
# squares # => [1.0, 1.44, 1.9599999999999997, 2.5600000000000005, 3.24, 4.0]
|
|
796
|
+
#
|
|
797
|
+
# # Only keyword to given.
|
|
798
|
+
# squares = []
|
|
799
|
+
# 4.step(to: 10) {|i| squares.push(i*i) } # => 4
|
|
800
|
+
# squares # => [16, 25, 36, 49, 64, 81, 100]
|
|
801
|
+
# # Only by given.
|
|
802
|
+
#
|
|
803
|
+
# # Only keyword by given
|
|
804
|
+
# squares = []
|
|
805
|
+
# 4.step(by:2) {|i| squares.push(i*i); break if i > 10 }
|
|
806
|
+
# squares # => [16, 36, 64, 100, 144]
|
|
807
|
+
#
|
|
808
|
+
# # No block given.
|
|
809
|
+
# e = 3.step(by: -1.5, to: -3) # => (3.step(by: -1.5, to: -3))
|
|
810
|
+
# e.class # => Enumerator::ArithmeticSequence
|
|
811
|
+
#
|
|
812
|
+
# <b>Positional Arguments</b>
|
|
813
|
+
#
|
|
814
|
+
# With optional positional arguments +limit+ and +step+,
|
|
815
|
+
# their values (or defaults) determine the step and limit:
|
|
816
|
+
#
|
|
817
|
+
# squares = []
|
|
818
|
+
# 4.step(10, 2) {|i| squares.push(i*i) } # => 4
|
|
819
|
+
# squares # => [16, 36, 64, 100]
|
|
820
|
+
# squares = []
|
|
821
|
+
# 4.step(10) {|i| squares.push(i*i) }
|
|
822
|
+
# squares # => [16, 25, 36, 49, 64, 81, 100]
|
|
823
|
+
# squares = []
|
|
824
|
+
# 4.step {|i| squares.push(i*i); break if i > 10 } # => nil
|
|
825
|
+
# squares # => [16, 25, 36, 49, 64, 81, 100, 121]
|
|
826
|
+
#
|
|
827
|
+
# **Implementation Notes**
|
|
828
|
+
#
|
|
829
|
+
# If all the arguments are integers, the loop operates using an integer
|
|
830
|
+
# counter.
|
|
831
|
+
#
|
|
832
|
+
# If any of the arguments are floating point numbers, all are converted
|
|
833
|
+
# to floats, and the loop is executed
|
|
834
|
+
# <i>floor(n + n*Float::EPSILON) + 1</i> times,
|
|
835
|
+
# where <i>n = (limit - self)/step</i>.
|
|
389
836
|
#
|
|
390
837
|
def step: (?Numeric limit, ?Numeric step) { (Numeric) -> void } -> self
|
|
391
838
|
| (?Numeric limit, ?Numeric step) -> Enumerator[Numeric, self]
|
|
392
839
|
| (?by: Numeric, ?to: Numeric) { (Numeric) -> void } -> self
|
|
393
840
|
| (?by: Numeric, ?to: Numeric) -> Enumerator[Numeric, self]
|
|
394
841
|
|
|
842
|
+
# <!--
|
|
843
|
+
# rdoc-file=complex.c
|
|
844
|
+
# - num.to_c -> complex
|
|
845
|
+
# -->
|
|
395
846
|
# Returns the value as a complex.
|
|
396
847
|
#
|
|
397
848
|
def to_c: () -> Complex
|
|
398
849
|
|
|
399
|
-
#
|
|
850
|
+
# <!--
|
|
851
|
+
# rdoc-file=numeric.c
|
|
852
|
+
# - to_int -> integer
|
|
853
|
+
# -->
|
|
854
|
+
# Returns `self` as an integer; converts using method `to_i` in the derived
|
|
855
|
+
# class.
|
|
856
|
+
#
|
|
857
|
+
# Of the Core and Standard Library classes, only Rational and Complex use this
|
|
858
|
+
# implementation.
|
|
400
859
|
#
|
|
401
|
-
#
|
|
402
|
-
#
|
|
403
|
-
# 1.0
|
|
860
|
+
# Examples:
|
|
861
|
+
#
|
|
862
|
+
# Rational(1, 2).to_int # => 0
|
|
863
|
+
# Rational(2, 1).to_int # => 2
|
|
864
|
+
# Complex(2, 0).to_int # => 2
|
|
865
|
+
# Complex(2, 1) # Raises RangeError (non-zero imaginary part)
|
|
404
866
|
#
|
|
405
867
|
def to_int: () -> Integer
|
|
406
868
|
|
|
407
|
-
#
|
|
408
|
-
#
|
|
869
|
+
# <!--
|
|
870
|
+
# rdoc-file=numeric.c
|
|
871
|
+
# - truncate(digits = 0) -> integer or float
|
|
872
|
+
# -->
|
|
873
|
+
# Returns `self` truncated (toward zero) to a precision of `digits` decimal
|
|
874
|
+
# digits.
|
|
409
875
|
#
|
|
410
|
-
# Numeric implements this by converting
|
|
876
|
+
# Numeric implements this by converting `self` to a Float and invoking
|
|
411
877
|
# Float#truncate.
|
|
412
878
|
#
|
|
413
879
|
def truncate: () -> Integer
|
|
414
880
|
| (Integer ndigits) -> (Integer | Numeric)
|
|
415
881
|
|
|
416
|
-
#
|
|
882
|
+
# <!--
|
|
883
|
+
# rdoc-file=numeric.c
|
|
884
|
+
# - zero? -> true or false
|
|
885
|
+
# -->
|
|
886
|
+
# Returns `true` if `zero` has a zero value, `false` otherwise.
|
|
887
|
+
#
|
|
888
|
+
# Of the Core and Standard Library classes, only Rational and Complex use this
|
|
889
|
+
# implementation.
|
|
417
890
|
#
|
|
418
891
|
def zero?: () -> bool
|
|
419
892
|
|
|
420
|
-
#
|
|
893
|
+
# <!--
|
|
894
|
+
# rdoc-file=numeric.c
|
|
895
|
+
# - clone(freeze: true) -> self
|
|
896
|
+
# -->
|
|
897
|
+
# Returns `self`.
|
|
421
898
|
#
|
|
422
|
-
# Raises an exception if the value for
|
|
899
|
+
# Raises an exception if the value for `freeze` is neither `true` nor `nil`.
|
|
423
900
|
#
|
|
424
901
|
# Related: Numeric#dup.
|
|
902
|
+
#
|
|
425
903
|
def clone: (?freeze: true?) -> self
|
|
426
904
|
end
|