Google

Programming Ruby

The Pragmatic Programmer's Guide

Previous < Contents ^
Next >
class Numeric
Parent: Object
Version: 1.6

Index:

+@ --@ abs coerce divmod eql? integer? modulo nonzero? remainder zero?



Subclasses: Float, Integer

Numeric is the fundamental base type for the concrete number classes Float, Fixnum, and Bignum.
mixins
Comparable: <, <=, ==, >=, >, between?

Difference between modulo and remainder. The modulo operator (``%'') always has the sign of the divisor, whereas remainder has the sign of the dividend.

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

instance methods
+@ +num -> num

Unary Plus---Returns the receiver's value.

--@ --num -> aNumeric

Unary Minus---Returns the receiver's value, negated.

abs num.abs -> aNumeric

Returns the absolute value of num.

12.abs 12
(-34.56).abs 34.56
-34.56.abs 34.56

coerce num.coerce( aNumeric ) -> anArray

If aNumeric is the same type as num, returns an array containing aNumeric and num. Otherwise, returns an array with both aNumeric and num represented as Float objects.

1.coerce(2.5) [2.5, 1.0]
1.2.coerce(3) [3.0, 1.2]
1.coerce(2) [2, 1]

divmod num.divmod( aNumeric ) -> anArray

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 on page 350.

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]

eql? num.eql?( aNumeric ) -> true or false

Returns 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

integer? num.integer? -> true or false

Returns true if num is an Integer (including Fixnum and Bignum).

modulo num.modulo( aNumeric ) -> aNumeric

Equivalent to num.divmod( aNumeric )[1].

nonzero? num.nonzero? -> num or nil

Returns num if num is not zero, 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"]

remainder num.remainder( aNumeric ) -> aNumeric

If num and aNumeric have different signs, returns mod-aNumeric; otherwise, returns mod. In both cases mod is the value num.modulo( aNumeric ). The differences between remainder and modulo (%) are shown in Table 22.6 on page 350.

zero? num.zero? -> true or false

Returns true if num has a zero value.


Previous < Contents ^
Next >

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.