📜  Ruby 整数 downto()函数与示例

📅  最后修改于: 2022-05-13 01:55:02.010000             🧑  作者: Mango

Ruby 整数 downto()函数与示例

Ruby 中的downto()函数返回所有小于等于 number 且大于等于 limit 的数字。它迭代给定的块,传递从int到并包括limit的递减值。如果没有给出块,则返回一个枚举器。

示例 #1:

# Initializing the number
num1 = 8
   
# Prints the number down to 0
puts num1.downto(0){| i | print i, " "}
   
# Initializing the number
num2 = -8
   
# Prints the number down to - 8 only since - 6 > -8
puts num2.downto(-6){| i | print i, " "}

输出

8 7 6 5 4 3 2 1 0 8
-8

示例 #2:

# Ruby program of Integer downto() function
  
# Initializing the number
num1 = 5
   
# Prints the number down to - 3
puts num1.downto(-3){| i | print i, " "}
   
# Initializing the number
num2 = 19
   
# Prints the number down to 17
puts num2.downto(17){| i | print i, " "}

输出

5 4 3 2 1 0 -1 -2 -3 5
19 18 17 19

示例#3:

# Initializing the number
num1 = 5
   
# Prints the number down to - 3
puts num1.downto(-3)

输出

#