Ruby Integer downto() function with example
Last Updated :
07 Jan, 2020
The downto() function in Ruby returns all the numbers less than equal to number and greater than equal to limit. It iterates the given block, passing in decreasing values from int down to and including limit. If no block is given, an Enumerator is returned instead.
Syntax: (number).downto(limit)
Parameter: The function takes the integer from which the number starts decreasing. It takes a parameter which is the limit till which the decreasing occurs. It also takes an enumerator.
Return Value: The function returns all the numbers less than equal to number and greater than equal to limit.
Example #1:
num1 = 8
puts num1.downto( 0 ){| i | print i, " " }
num2 = - 8
puts num2.downto(- 6 ){| i | print i, " " }
|
Output:
8 7 6 5 4 3 2 1 0 8
-8
Example #2:
num1 = 5
puts num1.downto(- 3 ){| i | print i, " " }
num2 = 19
puts num2.downto( 17 ){| i | print i, " " }
|
Output:
5 4 3 2 1 0 -1 -2 -3 5
19 18 17 19
Example #3:
num1 = 5
puts num1.downto(- 3 )
|
Output:
#
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...