take the absolute value of a number in ruby

To take the absolute value of a number in Ruby, you can use the abs method that is available on Numeric objects. Here's an example:

main.rb
num = -10
absolute_value = num.abs
puts absolute_value  # prints 10
68 chars
4 lines

In this example, we're creating a variable num with a value of -10. We then call the abs method on num to get its absolute value, which is assigned to the variable absolute_value. Finally, we use puts to print out the value of absolute_value, which is 10.

related categories

gistlibby LogSnag