Ruby Newbie homepage Ruby Newbie homepage

How to use

Quick guide

Official content
Returns self modulo other as a real number.
For integer n and real number r, these expressions are equivalent:
n % r
n-r*(n/r).floor
n.divmod(r)[1]
Examples:
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#%.
 
               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);
}
            

Was this page useful?