Skip to content
instance method

Integer#divmod

(::Integer) -> [ ::Integer, ::Integer ]
(::Float) -> [ ::Integer, ::Float ]
(::Rational) -> [ ::Integer, ::Rational ]
(::Numeric) -> [ ::Numeric, ::Numeric ]
divmod(other) → array

Returns a 2-element array [q, r], where

Example
q = (self/other).floor    # Quotient
r = self % other          # Remainder

Examples:

Example
11.divmod(4)              # => [2, 3]
11.divmod(-4)             # => [-3, -1]
-11.divmod(4)             # => [-3, 1]
-11.divmod(-4)            # => [2, -3]

12.divmod(4)              # => [3, 0]
12.divmod(-4)             # => [-3, 0]
-12.divmod(4)             # => [-3, 0]
-12.divmod(-4)            # => [3, 0]

13.divmod(4.0)            # => [3, 1.0]
13.divmod(Rational(4, 1)) # => [3, (1/1)]
See also #div #downto #digits #even? also in 4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0