Ajouter une extension dans l'objet Ruby Tempfile

To add extension to a Ruby tempfile, I figured that tempfile module needs to be modified a little.

Make a class like this:

require 'tempfile'

class CustomTempfle < Tempfile
  def initialize(filename, temp_dir = nil)
    temp_dir ||= Dir.tmpdir
    extension = File.extname(filename)
    basename = File.basename(filename, extension)
    super([basename, extension], temp_dir)
  end
end
And then you can call the class and provide the filename with the extensio and write to the file like so.

CustomTempfle.open('filename.pdf', nil) do |tmp|
  File.open(tmp, 'wb') { |f| f << 'content_of_the_file' }
end
Said HR