Mega Code Archive

 
Categories / Ruby / Collections
 

Call each a number of times

module Enumerable   def each_n_times(n)     each { |e| n.times { yield e } }   end end %w{Hello Echo}.each_n_times(3) { |x| puts x } # Hello # Hello # Hello # Echo # Echo # Echo