End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
instance method
Complex#to_r
cmp.to_r → rational
complex.c
View on GitHub ↗
static VALUE
nucomp_to_r(VALUE self)
{
get_dat1(self);
if (!k_exact_zero_p(dat->imag)) {
rb_raise(rb_eRangeError, "can't convert %"PRIsVALUE" into Rational",
self);
}
return f_to_r(dat->real);
}
Returns the value as a rational if possible (the imaginary part should be exactly zero).
Example
Complex(1, 0).to_r #=> (1/1) Complex(1, 0.0).to_r # RangeError Complex(1, 2).to_r # RangeError
See rationalize.