# Ruby Integer downto() function with example

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:
 # 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, " "}

Output:
8 7 6 5 4 3 2 1 0 8
-8
Example #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, " "}

Output:
5 4 3 2 1 0 -1 -2 -3 5
19 18 17 19
Example #3:
 # Initializing the number num1 = 5   # Prints the number down to - 3 puts num1.downto(-3)

Output:
#

Previous
Next