Skip to content

Instantly share code, notes, and snippets.

@rpearce
Created April 10, 2017 22:55
Show Gist options
  • Save rpearce/48db5e41d7dde123fc86b81b3437f064 to your computer and use it in GitHub Desktop.
Save rpearce/48db5e41d7dde123fc86b81b3437f064 to your computer and use it in GitHub Desktop.
Rails – open a file, read it and write it to a temp file
module Tmp
def self.build(src:, name:, ext:, binmode: false)
file = Tempfile.new([name, ext], Rails.root.join('tmp'))
file.binmode if binmode
open(src) { |f| file.write(f.read) }
file.rewind
file.close
file
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment