What is num.abs() in Ruby?

The abs() function returns the absolute value of a number.

Figure 1 shows a mathematical representation of the abs() function.

Figure 1: Mathematical representation of abs() function

Syntax

num.abs()
# where num is the number whose absolute value is required

Parameter

The abs() function does not take any parameter.

Return value

The abs() function returns the absolute value of a number passed in as a parameter.

Code

#positive number
print "The value of abs(10) : ", (10).abs(), "\n"
#zero
print "The value of abs(0) : ", (0).abs(), "\n"
#negative number
print "The value of abs(-10) : ", (-10).abs(), "\n"

Free Resources