Call each a number of times : Enumerable « Collections « Ruby






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

 








Related examples in the same category

1.get Enumerable involved
2.Sorting an Array by Frequency of Appearance
3.Add cartesian to Enumerable
4.Building a Histogram
5.randomly each
6.Writing Block Methods that Classify or Collect
7.Implementing Enumerable - Write One Method, Get 22 Free
8.Enumerable.instance_methods.sort
9.Sort on two arrays