Skip to content

Maintenance Ruby 3.3 gets fixes, but no new features. Switch to Ruby 4.0 →

instance method

Complex#<=>

complex <=> object → -1, 0, 1, or nil

Returns:

  • self.real <=> object.real if both of the following are true:

    • self.imag == 0.

    • object.imag == 0. # Always true if object is numeric but not complex.

  • nil otherwise.

Examples:

Example
Complex(2) <=> 3             # => -1
Complex(2) <=> 2             # => 0
Complex(2) <=> 1             # => 1
Complex(2, 1) <=> 1          # => nil # self.imag not zero.
Complex(1) <=> Complex(1, 1) # => nil # object.imag not zero.
Complex(1) <=> 'Foo'         # => nil # object.imag not defined.
See also #/ #== #-@ #abs also in 4.1 · 4.0 · 3.4 · 3.2 · 3.1 · 3.0