Skip to content

End of Life Ruby 3.2 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

Float#<=>

self <=> other → -1, 0, +1, or nil

Returns a value that depends on the numeric relation between self and other:

  • -1, if self is less than other.

  • 0, if self is equal to other.

  • 1, if self is greater than other.

  • nil, if the two values are incommensurate.

Examples:

Example
2.0 <=> 2              # => 0
2.0 <=> 2.0            # => 0
2.0 <=> Rational(2, 1) # => 0
2.0 <=> Complex(2, 0)  # => 0
2.0 <=> 1.9            # => 1
2.0 <=> 2.1            # => -1
2.0 <=> 'foo'          # => nil

This is the basis for the tests in the Comparable module.

Float::NAN <=> Float::NAN returns an implementation-dependent value.

See also #<= #== #< #=== also in 4.1 · 4.0 · 3.4 · 3.3 · 3.1 · 3.0