Ruby 整数abs()函数与实例
Ruby中的 abs() 函数返回整数的绝对值。
语法 :(number).abs
参数 :该函数接收要返回其绝对值的整数。
返回值 :该函数返回该整数的绝对值。
例子 #1 :
# Ruby program Integer abs() function
  
# Initializing the numbers 
num1 = -21
num2 = 21 
num3 = 0 
num4 = -100 
   
      
# Printing the absolute value of integers 
puts (num1).abs
puts (num2).abs
puts (num3).abs
puts (num4).abs
输出:
21
21
0
100
例子#2 。
# Ruby program of Integer abs() function
  
# Initializing the numbers 
num1 =29
num2 = -7
num3 = 90
num4 = -10
   
      
# Printing the absolute value of integers 
puts (num1).abs
puts (num2).abs
puts (num3).abs
puts (num4).abs
输出:
29
7
90
10
极客教程