End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
instance method
Complex#<=>
cmp <=> object → 0, 1, -1, or nil
complex.c
View on GitHub ↗
static VALUE
nucomp_cmp(VALUE self, VALUE other)
{
if (nucomp_real_p(self) && k_numeric_p(other)) {
if (RB_TYPE_P(other, T_COMPLEX) && nucomp_real_p(other)) {
get_dat2(self, other);
return rb_funcall(adat->real, idCmp, 1, bdat->real);
}
else if (f_real_p(other)) {
get_dat1(self);
return rb_funcall(dat->real, idCmp, 1, other);
}
}
return Qnil;
}
If cmp‘s imaginary part is zero, and object is also a real number (or a Complex number where the imaginary part is zero), compare the real part of cmp to object. Otherwise, return nil.
Example
Complex(2, 3) <=> Complex(2, 3) #=> nil Complex(2, 3) <=> 1 #=> nil Complex(2) <=> 1 #=> 1 Complex(2) <=> 2 #=> 0 Complex(2) <=> 3 #=> -1