Skip to content

Instantly share code, notes, and snippets.

@cassio-maletich
Forked from rpearce/tmp.rb
Created June 17, 2022 16:12
Show Gist options
  • Save cassio-maletich/581eb0660272126f2b3e829adabfc989 to your computer and use it in GitHub Desktop.
Save cassio-maletich/581eb0660272126f2b3e829adabfc989 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