From: Scott Barron Date: 2007-05-29T02:36:03+09:00 Subject: Re: How to improve iteration ------=_Part_12006_23186653.1180373759543 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline On 5/28/07, Chas Conquest wrote: > > Hi, > > Could you please point out how I can make this code more > compact/cleaner/smarter??? > Gotta be a block..I just don't see how to connect it. > > Gratefully, > > Chas > > <------Switch File Names---> > > ar = ["a.txt", "b.txt", "c.txt", "d.txt", "e.txt"] > br = ["first.txt", "second.txt", "third.txt", "fourth.txt", "fifth.txt"] > > length = ar.length > class FileNameSwap > def switch( letters, ordinals, length) > for i in 0..length -1 > if File.file?(letters[i]) > File.rename(letters[i], ordinals[i]) > puts "switched to ordinal" > else > File.rename(ordinals[i], letters[i]) > puts "switch to letter" > end > end > end > end > > switcher = FileNameSwap.new > switcher.switch(ar,br, length) > > <---end----> Sure, assuming ar and br from above, and that they're the same length, you can: ar.zip(br).each do |letter, ordinal| File.rename(letter, ordinal) rescue File.rename(ordinal, letter) end Or you can use the same construct (zip/each) with the if File.file? conditional block, whichever you find more understandable. -Scott ------=_Part_12006_23186653.1180373759543--