Ruby ceil syntax
number.ceil
In Ruby, number.ceil rounds toward positive infinity. The ceiling of 2.3 is 3, the ceiling of −2.3 is −2, and 2 stays 2.
number.ceil
puts((2.3).ceil)
3
number.ceil
| Input | A finite Float receiver in these examples. |
|---|---|
| Result | An Integer when called without a precision argument. Infinity and NaN raise FloatDomainError. |
A test bench increases a reading by one tenth of a unit at a time. Watch where the rounded display changes as the sample passes from one integer toward the next.
class MathExample
def getCeilValue
num1 = (1.0).ceil;
num2 = (1.1).ceil;
num3 = (1.2).ceil;
num4 = (1.3).ceil;
num5 = (1.4).ceil;
num6 = (1.5).ceil;
num7 = (1.6).ceil;
num8 = (1.7).ceil;
num9 = (1.8).ceil;
num10 = (1.9).ceil;
num11 = (2.0).ceil;
puts("The ceiling value of 1.0 is " + num1.to_s);
puts("The ceiling value of 1.1 is " + num2.to_s);
puts("The ceiling value of 1.2 is " + num3.to_s);
puts("The ceiling value of 1.3 is " + num4.to_s);
puts("The ceiling value of 1.4 is " + num5.to_s);
puts("The ceiling value of 1.5 is " + num6.to_s);
puts("The ceiling value of 1.6 is " + num7.to_s);
puts("The ceiling value of 1.7 is " + num8.to_s);
puts("The ceiling value of 1.8 is " + num9.to_s);
puts("The ceiling value of 1.9 is " + num10.to_s);
puts("The ceiling value of 2.0 is " + num11.to_s);
end
end
obj = MathExample.new
obj.getCeilValue
The ceiling value of 1.0 is 1 The ceiling value of 1.1 is 2 The ceiling value of 1.2 is 2 The ceiling value of 1.3 is 2 The ceiling value of 1.4 is 2 The ceiling value of 1.5 is 2 The ceiling value of 1.6 is 2 The ceiling value of 1.7 is 2 The ceiling value of 1.8 is 2 The ceiling value of 1.9 is 2 The ceiling value of 2.0 is 2
A scheduling display groups positive measurements into whole-unit intervals. Compare readings just on either side of an integer to see which interval the display would select.
class MathExample
def getCeilValue
num1 = (1.5698).ceil;
num2 = (1.3093).ceil;
num3 = (1.0087).ceil;
num4 = (1.1038).ceil;
num5 = (1.9573).ceil;
num6 = (1.7462).ceil;
num7 = (1.6427).ceil;
num8 = (1.4128).ceil;
num9 = (1.8099).ceil;
num10 = (1.2743).ceil;
num11 = (2.0001).ceil;
puts("The ceiling value of 1.5698 is " + num1.to_s);
puts("The ceiling value of 1.3093 is " + num2.to_s);
puts("The ceiling value of 1.0087 is " + num3.to_s);
puts("The ceiling value of 1.1038 is " + num4.to_s);
puts("The ceiling value of 1.9573 is " + num5.to_s);
puts("The ceiling value of 1.7462 is " + num6.to_s);
puts("The ceiling value of 1.6427 is " + num7.to_s);
puts("The ceiling value of 1.4128 is " + num8.to_s);
puts("The ceiling value of 1.8099 is " + num9.to_s);
puts("The ceiling value of 1.2743 is " + num10.to_s);
puts("The ceiling value of 2.0001 is " + num11.to_s);
end
end
obj = MathExample.new
obj.getCeilValue
The ceiling value of 1.5698 is 2 The ceiling value of 1.3093 is 2 The ceiling value of 1.0087 is 2 The ceiling value of 1.1038 is 2 The ceiling value of 1.9573 is 2 The ceiling value of 1.7462 is 2 The ceiling value of 1.6427 is 2 The ceiling value of 1.4128 is 2 The ceiling value of 1.8099 is 2 The ceiling value of 1.2743 is 2 The ceiling value of 2.0001 is 3
A temperature logger records values just below zero or another integer boundary. Follow the rounding direction on the negative side before using the result as the label of a chart interval.
class MathExample
def getCeilValue
num1 = (-1.5698).ceil;
num2 = (-1.3093).ceil;
num3 = (-1.0087).ceil;
num4 = (-1.1038).ceil;
num5 = (-1.9573).ceil;
num6 = (-1.7462).ceil;
num7 = (-1.6427).ceil;
num8 = (-1.4128).ceil;
num9 = (-1.8099).ceil;
num10 = (-1.2743).ceil;
num11 = (-2.0001).ceil;
puts("The ceiling value of -1.5698 is " + num1.to_s);
puts("The ceiling value of -1.3093 is " + num2.to_s);
puts("The ceiling value of -1.0087 is " + num3.to_s);
puts("The ceiling value of -1.1038 is " + num4.to_s);
puts("The ceiling value of -1.9573 is " + num5.to_s);
puts("The ceiling value of -1.7462 is " + num6.to_s);
puts("The ceiling value of -1.6427 is " + num7.to_s);
puts("The ceiling value of -1.4128 is " + num8.to_s);
puts("The ceiling value of -1.8099 is " + num9.to_s);
puts("The ceiling value of -1.2743 is " + num10.to_s);
puts("The ceiling value of -2.0001 is " + num11.to_s);
end
end
obj = MathExample.new
obj.getCeilValue
The ceiling value of -1.5698 is -1 The ceiling value of -1.3093 is -1 The ceiling value of -1.0087 is -1 The ceiling value of -1.1038 is -1 The ceiling value of -1.9573 is -1 The ceiling value of -1.7462 is -1 The ceiling value of -1.6427 is -1 The ceiling value of -1.4128 is -1 The ceiling value of -1.8099 is -1 The ceiling value of -1.2743 is -1 The ceiling value of -2.0001 is -2
A sensor can use infinity to mark a reading outside its measuring range. Pass both signs through this operation so the display can handle the result or the reported error deliberately.
class MathExample
def getCeilValue
# Positive ∞ infinite value
n = Float::INFINITY.ceil
puts("The ceiling value of Infinity is " + n.to_s)
# Negative ∞ infinite value
x = (-Float::INFINITY).ceil
puts("The ceiling value of -Infinity is " + x.to_s)
end
end
obj = MathExample.new
obj.getCeilValue
`ceil': Infinity (FloatDomainError)
A teacher has 73 passengers on the museum trip, including the adults. Each minibus seats 24. She needs enough vehicles for the last passenger as well as the first 72.
passengers, seats = 73, 24
buses = passengers.fdiv(seats).ceil
puts "#{buses} minibuses"
4 minibuses
Three minibuses seat 72 people. Dividing before rounding up reserves the fourth vehicle for the remaining passenger.
Reference: Ruby numeric operations