带有示例的 Ruby 整数下一个函数
Ruby 中的next函数返回数字的直接后继,即,它返回数字 + 1 。如果使用浮点值,则会引发错误消息。
Syntax: number.next
Parameter: The function takes the integer whose next is to be returned.
Return Value: The function returns the immediate successor of the number, i.e., it returns number + 1
示例 #1:
# Ruby program of next function
# Initializing the numbers
num1 = 100
num2 = 17
num3 = -90
num4 = -29
# Printing the modulo value
puts num1.next
puts num2.next
puts num3.next
puts num4.next
输出:
101
18
-89
-28
示例 #2:
# Ruby program of next function
# Initializing the numbers
num1 = 19
num2 = -17
num3 = -18
num4 = 16
# Printing the modulo value
puts num1.next
puts num2.next
puts num3.next
puts num4.next
输出:
20
-16
-17
17