This course will be retired on June 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
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 upRelated 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