instance method
Complex#<=>
(untyped) -> ::Integer?
self <=> other → -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;
}
Compares self and other.
Returns:
-
self.real <=> other.realif both of the following are true:-
self.imag == 0. -
other.imag == 0(always true ifotheris 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.
Class Complex includes module Comparable, each of whose methods uses Complex#<=> for comparison.