From: dblack@... Date: 2006-07-10T00:50:18+09:00 Subject: Re: renaming files randomly Hi -- On Mon, 10 Jul 2006, ara.t.howard@noaa.gov wrote: > On Mon, 10 Jul 2006 Ara.T.Howard@noaa.gov wrote: > >> 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. > > sorry - replace 'a b c' with '1 2 3' and 'c b a' with '3 2 1' - basically you > lose data when the slides have numbered names to begin with, which i noticed > because this is exactly how my slideshow software names it's slides! ;-) OK, then: new_names = [*0...files.size].map {|n| "MUNGED#{n}" }.sort_by { rand } :-) David -- http://www.rubypowerandlight.com => Ruby/Rails training & consultancy http://www.manning.com/black => RUBY FOR RAILS, the Ruby book for Rails developers http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log dblack@wobblini.net => me