End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
instance method
Integer#modulo
modulo(p1)
numeric.c
View on GitHub ↗
VALUE
rb_int_modulo(VALUE x, VALUE y)
{
if (FIXNUM_P(x)) {
return fix_mod(x, y);
}
else if (RB_BIGNUM_TYPE_P(x)) {
return rb_big_modulo(x, y);
}
return num_modulo(x, y);
}
Alias of #%
Returns self modulo other as a real number.
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)
Integer#modulo is an alias for Integer#%.