Skip to content

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

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
See also #/ #== #-@ #abs also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.1