📜  红宝石 |整数 upto()函数

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

红宝石 |整数 upto()函数

Ruby 中的upto函数将给定的所有数字返回到数字本身。它迭代给定的块,传入从 number1 到 number2 的递增值。如果没有给出块,则返回一个枚举器。

示例 1

Ruby
#Ruby program for upto() function
 
#Initializing the number
num1 = 8 num2 = 12
 
#Prints the number from num1 to num2
                puts num1.upto(num2)
{
    | i | print i, " "
}
 
#Initializing the number
num3 = 5 num4 = 15
 
#Prints the number from num3 to num4
                puts num3.upto(num4)
{
    | i | print i, " "
}


Ruby
#Ruby program for upto() function
 
#Initializing the number
num1 = 1 num2 = 3
 
#Prints the number from num1 to num2
                puts num1.upto(num2)
{
    | i | print i, " "
}
 
#Initializing the number
num3 = -7 num4 = -2
 
#Prints the number from num3 to num4
                  puts num3.upto(num4)
{
    | i | print i, " "
}


Ruby
#Ruby program for upto() function
 
#Initializing the number
num1 = 1 num2 = 3
 
#Returns an enumerator
#since no block is passed
                puts num1.upto(num2)


输出

8 9 10 11 12 8
5 6 7 8 9 10 11 12 13 14 15 5

示例 2

红宝石

#Ruby program for upto() function
 
#Initializing the number
num1 = 1 num2 = 3
 
#Prints the number from num1 to num2
                puts num1.upto(num2)
{
    | i | print i, " "
}
 
#Initializing the number
num3 = -7 num4 = -2
 
#Prints the number from num3 to num4
                  puts num3.upto(num4)
{
    | i | print i, " "
}

输出

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

示例 3

红宝石

#Ruby program for upto() function
 
#Initializing the number
num1 = 1 num2 = 3
 
#Returns an enumerator
#since no block is passed
                puts num1.upto(num2)

输出

#

参考:https://devdocs.io/ruby~2.5/integer#method-i-upto