Ruby abs - Syntax
mixed abs(mixed n);
The abs() function in Ruby is used to return the absolute value or modulus |x| of a real number x is the non-negative value of x without regard to its sign.
mixed abs(mixed n);
mixed n |
Specifies the real number (int, long, float, double) / infinity (*Required) |
---|---|
Return mixed |
Returns a number / infinity respective to an input argument. |
Convert the positive and negative integer to an absolute value.
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
Convert the positive and negative float to an absolute value.
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
Convert the positive and negative Infinity to an absolute value.
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