Ruby abs syntax
number.abs
In Ruby, number.abs returns the magnitude of a number. For example, the absolute value of −6 is 6, and the absolute value of 0 is 0.
number.abs
puts((-3).abs)
3
number.abs
| Input | An Integer or Float receiver in the examples below. |
|---|---|
| Result | The magnitude of the receiver. A negative literal needs parentheses before the method call, as in (-6).abs. |
A shop records a surplus as a positive count and a shortage as a negative count. The reconciliation report asks how many items differ in each case, regardless of direction.
class MathExample
def getAbsoluteValue
# Positive integer value
n = 9.abs
puts("The absolute value of 9 is " + n.to_s)
# Negative integer value
x = (-6).abs
puts("The absolute value of -6 is " + x.to_s)
end
end
obj = MathExample.new
obj.getAbsoluteValue
The absolute value of 9 is 9 The absolute value of -6 is 6
A scale reports deviations on either side of its reference weight. Find the size of each deviation while keeping the fractional part so small differences remain visible.
class MathExample
def getAbsoluteValue
# Positive floating point value
n = (9.3).abs
puts("The absolute value of 9.3 is " + n.to_s)
# Negative floating point value
x = (-6.3).abs
puts("The absolute value of -6.3 is " + x.to_s)
end
end
obj = MathExample.new
obj.getAbsoluteValue
The absolute value of 9.3 is 9.3 The absolute value of -6.3 is 6.3
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 getAbsoluteValue
# Positive ∞ infinite value
n = Float::INFINITY.abs
puts("The absolute value of Infinity is " + n.to_s)
# Negative ∞ infinite value
x = (-Float::INFINITY).abs
puts("The absolute value of -Infinity is " + x.to_s)
end
end
obj = MathExample.new
obj.getAbsoluteValue
The absolute value of Infinity is Infinity The absolute value of -Infinity is Infinity
A stocktake compares what the scanner found with what the inventory system expected. The report needs to distinguish a shortage, a surplus and an exact match before deciding whether to investigate.
scanned = 47
expected = 52
puts (scanned - expected).abs
5
The scanner found 47 items against 52 on the manifest. The discrepancy is five items. Keep the signed difference separately to distinguish a shortage from a surplus.
A school library expects 48 loan chargers on its shelf after class. A student counts 45. The handover form has a separate field for the size of the discrepancy, whether the shelf is short or over.
expected, counted = 48, 45
puts "#{(counted - expected).abs} chargers"
3 chargers
The signed difference is minus three. Its magnitude gives the three chargers the next shift needs to account for.
A class logs the courtyard temperature at sunrise and again before lunch. The report asks how far apart the readings are, even when one is below freezing.
The two readings sit seven one-degree intervals apart on the same scale.
sunrise, midday = -3, 4
puts "#{(midday - sunrise).abs} °C"
7 °C
Reference: Ruby numeric operations