Ruby Integer times function with example
The times function in Ruby returns all the numbers from 0 to number itself. It iterates the given block, passing in increasing values from 0 up to the limit. If no block is given, an Enumerator is returned instead.
Syntax: (number).times
Parameter: The function takes the integer till which the numbers are returned. It also takes an block.
Return Value: The function returns all the numbers from 0 to number itself.
Example #1:
# Ruby program for times function # Initializing the number num1 = 8 # Prints the number from 0 to num1 puts num1.times { |i| print i, " " } # Initializing the number num2 = 5 # Prints the number from 0 to num2 puts num2.times { |i| print i, " " } |
Output :
0 1 2 3 4 5 6 7 8 0 1 2 3 4 5
Example #2:
# Ruby program for times function # Initializing the number num1 = 4 # Prints the number from 0 to num1 puts num1.times { |i| print i, " " } # Initializing the number num2 = -2 # Prints the number from 0 to num2 puts num2.times { |i| print i, " " } |
Output:
0 1 2 3 4 -2
Example #3:
# Ruby program for times function # Initializing the number num1 = 5 # Prints enumerator as no block is given puts num1.times |
Output:
#
Recommended Posts:
- Ruby Integer integer? function with example
- Ruby Integer odd? function with example
- Ruby Integer div() function with example
- Ruby | Integer >= function
- Ruby | Integer - function
- Ruby | Integer << function
- Ruby Integer gcd() function with example
- Ruby Integer next function with example
- Ruby Integer even? function with example
- Ruby | Integer <=> function
- Ruby Integer abs() function with example
- Ruby Integer lcm() function with example
- Ruby Integer chr function with example
- Ruby Integer pred() function with example
- Ruby Integer floor() function with example
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.

