Ruby Integer pred()函数与示例
Ruby 中的pred函数返回数字的直接前任,即它返回数字 - 1 。如果使用浮点值,则会引发错误消息。
Syntax: number.pred
Parameter: The function takes the integer whose predecessor is to be returned.
Return Value: The function returns the immediate predecessor of the number, i.e., it returns number – 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