Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →
instance method
Integer#remainder
(::Integer) -> ::Integer
(::Float) -> ::Float
(::Rational) -> ::Rational
(::Numeric) -> ::Numeric
remainder(other) → real_number
numeric.c
View on GitHub ↗
static VALUE
int_remainder(VALUE x, VALUE y)
{
if (FIXNUM_P(x)) {
if (FIXNUM_P(y)) {
VALUE z = fix_mod(x, y);
RUBY_ASSERT(FIXNUM_P(z));
if (z != INT2FIX(0) && (SIGNED_VALUE)(x ^ y) < 0)
z = fix_minus(z, y);
return z;
}
else if (!RB_BIGNUM_TYPE_P(y)) {
return num_remainder(x, y);
}
x = rb_int2big(FIX2LONG(x));
}
else if (!RB_BIGNUM_TYPE_P(x)) {
return Qnil;
}
return rb_big_remainder(x, y);
}
Returns the remainder after dividing self by other.
Examples:
Example
11.remainder(4) # => 3 11.remainder(-4) # => 3 -11.remainder(4) # => -3 -11.remainder(-4) # => -3 12.remainder(4) # => 0 12.remainder(-4) # => 0 -12.remainder(4) # => 0 -12.remainder(-4) # => 0 13.remainder(4.0) # => 1.0 13.remainder(Rational(4, 1)) # => (1/1)