Reputation: 30671
I'm trying to do repeat a code block, but have something happen every 5th time.
In English:
Do this 30 times, every 5th time take an additional step
My Ruby so far:
6.times do
5.times do
#standard step
end
#perform additional step
end
but I wondered if there was a clever way to do it?
Upvotes: 2
Views: 107
Reputation: 5651
mostly you do "every nth time" problems with a modulo like this:
30.times do |n|
# standard step
if n % 5 == 0
puts n # extra step
end
end
Upvotes: 6