I found it may be a common case that we need randomly enumerate elements
in a array or some other collections.
Please take a look at the very simple snippet of code below:
class Array
def random_each
a = self.dup
n = len = self.length
len.times do
index = rand(n)
yield a[index]
a.delete_at(index)
n -= 1
end
end
end
a = [1,2,3,4,5,6,7]
a.random_each {|e| print e , ' '}
result:
7 1 6 2 4 3 5
I don't whether it is proper to put this kind of simple but useful
utility in the standard library of ruby.
Thanks.
uncutstone
--
Posted via http://www.ruby-forum.com/.