On Mon, 10 Jul 2006 dblack / wobblini.net wrote:

> Dir["photos/*"].sort_by { rand }
>
> If the renaming is really necessary, then something like:
>
>  Dir.chdir("photos") do
>    files = Dir["*"]
>    new_names = [*0...files.size].sort_by { rand }
>    files.zip(new_names).each do |f,n|
>      File.rename(f, n.to_s)
>    end
>  end

this loop loses data

if

   files = %w( a b c )

and

   new_names = %w( c b a )

then the renaming order is

   a -->> c
   b -->> b
   c -->> a

so

   File.rename 'a', 'c'  # c is clobbered
   File.rename 'b', 'b'  # ok
   File.rename 'c', 'a'  # a is clobbered, new c is lost

at the end of the loop you'll have only files 'a' and 'b' and no error will
have been thrown.

regards.

-a
-- 
suffering increases your inner strength.  also, the wishing for suffering
makes the suffering disappear.
- h.h. the 14th dali lama