Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →
instance method
Complex#<=>
(untyped) -> ::Integer?
complex <=> object → -1, 0, 1, or nil
complex.c
View on GitHub ↗
static VALUE
nucomp_cmp(VALUE self, VALUE other)
{
if (!k_numeric_p(other)) {
return rb_num_coerce_cmp(self, other, idCmp);
}
if (!nucomp_real_p(self)) {
return Qnil;
}
if (RB_TYPE_P(other, T_COMPLEX)) {
if (nucomp_real_p(other)) {
get_dat2(self, other);
return rb_funcall(adat->real, idCmp, 1, bdat->real);
}
}
else {
get_dat1(self);
if (f_real_p(other)) {
return rb_funcall(dat->real, idCmp, 1, other);
}
else {
return rb_num_coerce_cmp(dat->real, other, idCmp);
}
}
return Qnil;
}
Returns:
-
self.real <=> object.realif both of the following are true:-
self.imag == 0. -
object.imag == 0. # Always true if object is numeric but not complex.
-
-
nilotherwise.
Examples:
Example
Complex.rect(2) <=> 3 # => -1 Complex.rect(2) <=> 2 # => 0 Complex.rect(2) <=> 1 # => 1 Complex.rect(2, 1) <=> 1 # => nil # self.imag not zero. Complex.rect(1) <=> Complex.rect(1, 1) # => nil # object.imag not zero. Complex.rect(1) <=> 'Foo' # => nil # object.imag not defined.