Ruby
Get absolute value
in Ruby
Get absolute value of target.Use abs method of Numeric class for both of integer and float.
get_absolute_value.rb
# Get absolute value of integer print("Absolute value of 5 is ", 5.abs, "\n") print("Absolute value of -5 is ", (-5).abs, "\n") # Get absolute value of float print("Absolute value of 5.5 is ", 5.5.abs, "\n") print("Absolute value of -5.5 is ", (-5.5).abs, "\n")
Result
$ ruby get_absolute_value.rb Absolute value of 5 is 5 Absolute value of -5 is 5 Absolute value of 5.5 is 5.5 Absolute value of -5.5 is 5.5