From: Robert Klemme Date: 2007-05-29T16:55:04+09:00 Subject: Re: How to improve iteration On 28.05.2007 19:47, Sebastian Hungerecker wrote: > Chas Conquest wrote: >> Could you please point out how I can make this code more >> compact/cleaner/smarter??? > > I'll try: > > ar = ["a.txt", "b.txt", "c.txt", "d.txt", "e.txt"] > br = ["first.txt", "second.txt", "third.txt", "fourth.txt", "fifth.txt"] > > class FileNameSwap > # I don't know why you chose to make a class for this, but since you did I'll > # at least store the arrays as instance variables > > def initialize(letters,ordinals) > @letters=letters > @ordinals=ordinals > end > > def switch > @letters.zip(@ordinals).each do |letter, ordinal| > if File.file? letter > File.rename(letter, ordinal) > else > File.rename(ordinal, letter) > end > end > end > end > > switcher = FileNameSwap.new(ar,br) > switcher.switch And a strange variant of this: def switch(letters, ordinals) letters.zip(ordinals).each do |*a| File.rename(*(File.file? a[0] ? a : a.reverse)) end end :-) robert PS: Sorry to all who expected an #inject solution. ;-)