Skip to content

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

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