Subclasses: Float, Integer
Numeric is the fundamental base type for the concrete number classes Float
, Fixnum
, and Bignum
.
Comparable
<
, <=
, ==
, >=
, >
, between?
12.abs → 12
(-34.56).abs → 34.56
-34.56.abs → 34.56
Float
objects.
1.coerce(2.5) → [2.5, 1.0]
1.2.coerce(3) → [3.0, 1.2]
1.coerce(2) → [2, 1]
Returns an array containing the quotient and modulus obtained by dividing num by aNumeric.
If q, r = x.divmod(y)
,
q = floor(float(x) / float(y))
x = q * y + r
The quotient is rounded toward -infinity. See /Table 22.6.
11.divmod(3) → [3, 2]
11.divmod(-3) → [-4, -1]
11.divmod(3.5) → [3.0, 0.5]
(-11).divmod(3.5) → [-4.0, 3.0]
(11.5).divmod(3.5) → [3.0, 1.0]
a | b | a.divmod(b) | a / b | a.modulo(b) | a.remainder(b) |
---|---|---|---|---|---|
13 | 4 | 3, 1 | 3 | 1 | 1 |
13 | -4 | -4, -3 | -4 | -3 | 1 |
-13 | 4 | -4, 3 | -4 | 3 | -1 |
-13 | -4 | 3, -1 | 3 | -1 | -1 |
11.5 | 4 | 2.0, 3.5 | 2.875 | 3.5 | 3.5 |
11.5 | -4 | -3.0, -0.5 | -2.875 | -0.5 | 3.5 |
-11.5 | 4 | -3.0, 0.5 | -2.875 | 0.5 | -3.5 |
-11.5 | -4 | 2.0, -3.5 | 2.875 | -3.5 | -3.5 |
true
or false
true
if num and aNumeric are the same type and have equal values.
1 == 1.0 → true
1.eql?(1.0) → false
(1.0).eql?(1.0) → true
true
or false
true
if num is an Integer
(including Fixnum
and Bignum
). divmod(
aNumeric)[1]
. nil
nil
otherwise. This behavior is useful when chaining comparisons:
a = %w( z Bb bB bb BB a aA Aa AA A )
b = a.sort {|a,b| (a.downcase <=> b.downcase).nonzero? || a <=> b }
b → ["A", "a", "AA", "Aa", "aA", "BB", "Bb", "bB", "bb", "z"]
modulo(
aNumeric)
. The differences between remainder
and modulo (%
) are shown in Table 22.6. true
or false
true
if num has a zero value.Extracted from the book "Programming Ruby - The Pragmatic Programmer's Guide"
Copyright © 2001 by Addison Wesley Longman, Inc. This material may be distributed only subject to the terms and conditions set forth in the Open Publication License, v1.0 or later (the latest version is presently available at http://www.opencontent.org/openpub/).
Distribution of substantively modified versions of this document is prohibited without the explicit permission of the copyright holder.
Distribution of the work or derivative of the work in any standard (paper) book form is prohibited unless prior permission is obtained from the copyright holder.