title: ZipRuby - Zipping files with duplicate names (clean code)
body: <p>I have altered my code to prevent an error when zipping a number of files that have the same file name. I want to change it so that duplicate file names are incremented. </p>
  
  <p>For example if you had three files called file.txt, then I want them to be <code>file.txt</code>, <code>file_2.txt</code> and <code>file_3.txt</code></p>
  
  <pre><code>def zip(file_name)
    files_added = []
    ZipRuby::Archive.open(file_name, ZipRuby::CREATE) do |archive|
      file_associations.sort_by { |fa| fa.created_at }.each do |fa|
        attachment_file_name = fa.attachment_file_name
        binding.pry
  
        if files_added.include?(attachment_file_name)
          n = 2
          ext = File.extname(attachment_file_name)
          base = File.basename(attachment_file_name, ext)
          new_name = "#{base}_#{n}#{ext}"
          while files_added.include? new_name
            n += 1
            new_name = "#{base}_#{n}#{ext}"
          end
          attachment_file_name = new_name
        end
  
        archive.add_buffer(attachment_file_name, fa.attachment.read)
        files_added &lt;&lt; attachment_file_name
      end
    end
  end
  </code></pre>
  
  <p>The code above works but I am new to Ruby/Rails and it doesn't feel very Rubyist. Can anyone give any pointers for a refactor.</p>