How would I print “Hello” every nth of an iteration in ruby using something like:
50.times do
# every nth say hello
end
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I don’t think
timeswill work for that case. But you can iterate over range instead:Updated (thanks to d11wtq)
It turned out that
Integer#timesyields iteration number to the block too:Numeration is zero-based so we add 1 to iteration number (you can use
i % 5 == 4instead, but it looks less obvious).