# How to improve iteration

**URL:** <https://www.ruby-forum.com/t/how-to-improve-iteration/105313>\
**Category:** Ruby\
**Created:** [May 28, 2007, 7:20pm UTC](https://www.ruby-forum.com/t/how-to-improve-iteration/105313 "2007-05-28T19:20:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![chasconquest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/bb73d2/32.png) [@chasconquest](https://www.ruby-forum.com/u/chasconquest)\
**Post date:** [May 28, 2007, 7:20pm UTC](https://www.ruby-forum.com/t/how-to-improve-iteration/105313/1 "2007-05-28T19:20:11Z")

</div>

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----\>

---

<div class="post-metadata">

**Author:** ![chasconquest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/bb73d2/32.png) [@chasconquest](https://www.ruby-forum.com/u/chasconquest)\
**Post date:** [May 28, 2007, 7:37pm UTC](https://www.ruby-forum.com/t/how-to-improve-iteration/105313/2 "2007-05-28T19:37:53Z")

</div>

On 5/28/07, Chas C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> File.rename(letters[i], ordinals[i])
> 
> ```
> 
> 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

---

<div class="post-metadata">

**Author:** ![chasconquest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/bb73d2/32.png) [@chasconquest](https://www.ruby-forum.com/u/chasconquest)\
**Post date:** [May 28, 2007, 7:48pm UTC](https://www.ruby-forum.com/t/how-to-improve-iteration/105313/3 "2007-05-28T19:48:54Z")

</div>

Chas C. 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

HTH,  
Sebastian H.

---

<div class="post-metadata">

**Author:** ![chasconquest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/bb73d2/32.png) [@chasconquest](https://www.ruby-forum.com/u/chasconquest)\
**Post date:** [May 28, 2007, 8:47pm UTC](https://www.ruby-forum.com/t/how-to-improve-iteration/105313/4 "2007-05-28T20:47:52Z")

</div>

Fantastic guys! Thanks very much.

---

<div class="post-metadata">

**Author:** ![chasconquest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/bb73d2/32.png) [@chasconquest](https://www.ruby-forum.com/u/chasconquest)\
**Post date:** [May 28, 2007, 7:50pm UTC](https://www.ruby-forum.com/t/how-to-improve-iteration/105313/5 "2007-05-28T19:50:55Z")

</div>

Instead of doing for…in and use i as an index, you could do  
letters.each…do…|letter| and there’s no index. letter is the  
extracted index.

The only problem would be the ordinals[i] which could stay as it is  
and note your own iterations (not recommended) or do, instead of  
letters.each, do letters.each\_index do |i| and little would change.

Both suggestions would rid you of needing the length argument, but to  
be honest that’s the only real disadvantage to your algorithm. It’s  
pretty much fine how it is. You should only change it if it’s really  
hard to find length in my opinion.

---

<div class="post-metadata">

**Author:** ![chasconquest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/bb73d2/32.png) [@chasconquest](https://www.ruby-forum.com/u/chasconquest)\
**Post date:** [May 29, 2007, 9:57am UTC](https://www.ruby-forum.com/t/how-to-improve-iteration/105313/6 "2007-05-29T09:57:12Z")

</div>

On 28.05.2007 19:47, Sebastian H. wrote:

> # I don’t know why you chose to make a class for this, but since you did I’ll
> 
> ```
> 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. 😉
