randomly each : Enumerable « Collections « Ruby






randomly each


module Enumerable
  def each_randomly
    (sort_by { rand }).each { |e| yield e }
  end
end
%w{A b c }.each_randomly { |x| puts x }

 








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.Call each a number of times
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