Skip to content
instance method

Rational#<=>

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

Compares self and other.

Returns:

  • -1, if self is less than other.

  • 0, if the two values are the same.

  • 1, if self is greater than other.

  • nil, if the two values are incomparable.

Examples:

Example
Rational(2, 3) <=> Rational(4, 3) # => -1
Rational(2, 1) <=> Rational(2, 1) # => 0
Rational(2, 1) <=> 2              # => 0
Rational(2, 1) <=> 2.0            # => 0
Rational(2, 1) <=> Complex(2, 0)  # => 0
Rational(4, 3) <=> Rational(2, 3) # => 1
Rational(4, 3) <=> :foo           # => nil

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

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