Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll

- 2x 2x
- 1.75x 1.75x
- 1.5x 1.5x
- 1.25x 1.25x
- 1.1x 1.1x
- 1x 1x
- 0.75x 0.75x
- 0.5x 0.5x
The times iterator will take a number and execute the statements in the block that number of times. The times iterator optionally takes an argument and, just like array indices, starts at 0.
Code Samples
Using a number, we can iterate that amount of times:
5.times do
puts "Hello!"
end
This will print "Hello!" to the screen 5 times.
The times
method also can take a single argument:
5.times do |item|
puts "Hello! #{item}"
end
Just like an array index, the times
index starts at 0.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up-
Mark Weinberg
3,002 PointsHow does Ruby know from the word "item" in the pipe to print out "Hello!" and then the numbers 0 to 5?
Posted by Mark WeinbergMark Weinberg
3,002 Points2 Answers
-
Hyunsoo Choi
1,255 PointsHow did the variable |item| printed out number from 0 to 4?
Posted by Hyunsoo ChoiHyunsoo Choi
1,255 Points3 Answers
View all discussions for this video
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up