>>565 class Array
def each_permutation(&block)
_permutate(self, 0, &block)
end
private
def _permutate(a, k, &block)
n = a.size-1
if n == k
yield(a)
else
(k..n).each do |i|
b = a.dup ; b[k],b[i] = b[i],b[k] ; _permutate(b, k+1, &block)
end
end
end
end