Skip to content

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

instance method

Rational#<=>

rational <=> numeric → -1, 0, +1, or nil

Returns -1, 0, or +1 depending on whether rational is less than, equal to, or greater than numeric.

nil is returned if the two values are incomparable.

Example
Rational(2, 3) <=> Rational(2, 3)  #=> 0
Rational(5)    <=> 5               #=> 0
Rational(2, 3) <=> Rational(1, 3)  #=> 1
Rational(1, 3) <=> 1               #=> -1
Rational(1, 3) <=> 0.3             #=> 1

Rational(1, 3) <=> "0.3"           #=> nil
See also #/ #== #-@ #abs also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.0