Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

instance method

Integer#modulo

(::Float) -> ::Float
(::Rational) -> ::Rational
(::Integer) -> ::Integer
(::Numeric) -> ::Numeric
modulo(p1)

Alias of #%

Returns self modulo other as a real numeric (Integer, Float, or Rational).

For integer n and real number r, these expressions are equivalent:

Example
n % r
n-r*(n/r).floor
n.divmod(r)[1]

See Numeric#divmod.

Examples:

Example
10 % 2              # => 0
10 % 3              # => 1
10 % 4              # => 2

10 % -2             # => 0
10 % -3             # => -2
10 % -4             # => -2

10 % 3.0            # => 1.0
10 % Rational(3, 1) # => (1/1)
See also #% #magnitude #next #lcm also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0