title: How to duplicate a File as a TempFile?
body: <p>I am currently developing a script that look for files in a specific folder.
  I want to duplicate those files. Then I want to remove some useless lines from those duplicate files, parse them in YAML (this is the format of content in it). And finally make a diff of the files.</p>
  
  <p>I was firstly thinking to copy them in a <code>tmp</code> folder but I just discovered the existence of <a href="http://www.ruby-doc.org/stdlib-1.9.3/libdoc/tempfile/rdoc/Tempfile.html" rel="nofollow noreferrer"><code>Tempfile</code></a> which seems to be exactly what I need since the temporary files have to be deleted after the script has done its job.</p>
  
  <p>What I have for now:</p>
  
  <pre><code>require 'Tempfile'
  require 'YAML'
  
  PWD = File.dirname(__FILE__)
  LOCALES_FOLDER_PATH = "#{PWD}/../app/assets/javascripts/ayl/bo/config/locales"
  LOCALES = ['en', 'fr']
  
  files_data = {}
  locale_file_names = LOCALES.map{|locale| "#{locale}.js.coffee"}
  files = locale_file_names.map do |locale_file_name|
    files_data[locale_file_name] = "#{LOCALES_FOLDER_PATH}/#{locale_file_name}"
  end
  
  locale_files = []
  files.each do |file|
  
    content = File.open(file).read
    locale_file = Tempfile.open file.gsub("/", "_") do |f|
      f.write content
      f.flush
      puts f.read
    end
    locale_files &lt;&lt; locale_file
  
  end
  </code></pre>
  
  <p>Results in:</p>
  
  <pre><code>± ruby diff_locales.rb
  /Users/adrien/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/YAML.rb:48: warning: already initialized constant ENGINE
  /Users/adrien/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/YAML.rb:69: warning: already initialized constant ENGINE
  /Users/adrien/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/YAML.rb:73: warning: already initialized constant ENGINE
  
  
  ±
  </code></pre>
  
  <p>The content of my temporary files is empty.</p>
  
  <p>This seems weird to me. But I assume their is something I just do not know/understand.</p>
  
  <hr>
  
  <p><strong>OK!</strong> Figured it out! Inside the block, I had to move the cursor back to the beginning of the file before reading the content.</p>