當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Ruby Integer pred()用法及代碼示例

Ruby中的pred函數返回數字的直接前任,即返回數字-1。如果使用浮點值,則拋出錯誤消息。

用法:number.pred  

參數:該函數采用要返回其前身的整數。


返回值:該函數返回數字的前身,即返回數字-1

例子1

# Ruby program for pred function 
   
# Initializing the numbers  
num1 = 100
num2 = 17
num3 = -90
num4 = -29
     
# Printing the pred value 
puts num1.pred 
puts num2.pred 
puts num3.pred  
puts num4.pred 

輸出

99
16
-91
-30

例子2

# Ruby program for pred function 
   
# Initializing the numbers  
num1 = 19
num2 = -17
num3 = -18
num4 = 16
       
# Printing the pred value 
puts num1.pred 
puts num2.pred 
puts num3.pred  
puts num4.pred

輸出

18
-18
-19
15


相關用法


注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 Ruby Integer pred() function with example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。