Maintenance Ruby 3.3 gets fixes, but no new features. Switch to Ruby 4.0 →
instance method
Integer#gcd
int.gcd(other_int) → integer
rational.c
View on GitHub ↗
VALUE
rb_gcd(VALUE self, VALUE other)
{
other = nurat_int_value(other);
return f_gcd(self, other);
}
Returns the greatest common divisor of the two integers. The result is always positive. 0.gcd(x) and x.gcd(0) return x.abs.
Example
36.gcd(60) #=> 12 2.gcd(2) #=> 2 3.gcd(-7) #=> 1 ((1<<31)-1).gcd((1<<61)-1) #=> 1