End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
instance method
Rational#magnitude
rat.magnitude → rational
rational.c
View on GitHub ↗
VALUE
rb_rational_abs(VALUE self)
{
get_dat1(self);
if (INT_NEGATIVE_P(dat->num)) {
VALUE num = rb_int_abs(dat->num);
return nurat_s_canonicalize_internal_no_reduce(CLASS_OF(self), num, dat->den);
}
return self;
}
Alias of #abs
Returns the absolute value of rat.
Example
(1/2r).abs #=> (1/2) (-1/2r).abs #=> (1/2)
Rational#magnitude is an alias for Rational#abs.