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:



filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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

Example #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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

Example #3:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

#



My Personal Notes arrow_drop_up

The function of education is to teach one to think intensively and to think critically

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.