Skip to content
instance method

Complex#<=>

(untyped) -> ::Integer?
self <=> other → -1, 0, 1, or nil

Compares self and other.

Returns:

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

    • self.imag == 0.

    • other.imag == 0 (always true if other 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.

Class Complex includes module Comparable, each of whose methods uses Complex#<=> for comparison.

See also #/ #== #-@ #abs also in 4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0