Ruby Integer times function with example
Last Updated :
17 Mar, 2020
The times function in Ruby returns all the numbers from 0 to one less than the 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 one less than the number itself.
Example #1:
num1 = 8
puts num1.times { |i| print i, " " }
num2 = 5
puts num2.times { |i| print i, " " }
|
Output :
0 1 2 3 4 5 6 7
0 1 2 3 4
Example #2:
num1 = 4
puts num1.times { |i| print i, " " }
num2 = - 2
puts num2.times { |i| print i, " " }
|
Output:
0 1 2 3
-2
Example #3:
Output:
#
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...