randomly each : Enumerable « Collections « Ruby

randomly each

module Enumerable
  def each_randomly
    (sort_by { rand }).each { |e| yield e }
%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
9.Sort on two arrays