# If problem in loop causing weirdness

**URL:** <https://www.ruby-forum.com/t/if-problem-in-loop-causing-weirdness/96915>\
**Category:** Ruby\
**Created:** [July 3, 2006, 2:15pm UTC](https://www.ruby-forum.com/t/if-problem-in-loop-causing-weirdness/96915 "2006-07-03T14:15:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [July 3, 2006, 2:15pm UTC](https://www.ruby-forum.com/t/if-problem-in-loop-causing-weirdness/96915/1 "2006-07-03T14:15:26Z")

</div>

Having an issue here, specifically (5 lines down) puts x + ’ already  
exists’. It spits out the same number of times that the array loops  
are reiterating. Here is my concept, both arrays are looping [0]  
element to [0] element. When File.exist returns true it should ask  
user “once” for a yes or no, if no then it deletes it from the  
pics2bmoved array. However as I introduced above, it is asking x  
number of times , with x being the number of elements in the array.  
Sorry for asking yet another noob question, but the books and  
reference didn’t turn up anything specific to this situation.

destdir = Dir[‘C:/testfilesmoved/_._’]  
destdir.each do |name|  
pics2bmoved.each do |x|

```
 if File.exist?(name) && File.compare(x, name)
      puts x + ' already exists'
      puts 'Do you want to overwrite? "yes" or "no"?'
               ow = gets.chomp.downcase

               if ow == 'no'
               pics2bmoved.delete(x)

  end # for if file.exist?
end # for if ow statement

```

end # end for pics2bmoved loop  
end # end for destdir loop

puts  
if pics2bmoved.empty?  
puts ‘There are no files to be moved’  
else  
puts ‘The following files will be moved’  
puts pics2bmoved  
end # for pic2bmoved list

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [July 3, 2006, 2:21pm UTC](https://www.ruby-forum.com/t/if-problem-in-loop-causing-weirdness/96915/2 "2006-07-03T14:21:37Z")

</div>

On 7/3/06, Dark A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
