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

Rational#ceil

rat.ceil([ndigits]) → integer or rational

Returns the smallest number greater than or equal to rat with a precision of ndigits decimal digits (default: 0).

When the precision is negative, the returned value is an integer with at least ndigits.abs trailing zeros.

Returns a rational when ndigits is positive, otherwise returns an integer.

Example
Rational(3).ceil      #=> 3
Rational(2, 3).ceil   #=> 1
Rational(-3, 2).ceil  #=> -1

  #    decimal      -  1  2  3 . 4  5  6
  #                   ^  ^  ^  ^   ^  ^
  #   precision      -3 -2 -1  0  +1 +2

Rational('-123.456').ceil(+1).to_f  #=> -123.4
Rational('-123.456').ceil(-1)       #=> -120
See also #as_json #denominator #abs #fdiv also in 4.1 · 4.0 · 3.4 · 3.3 · 3.1 · 3.0