Search code examples
rubyfiletemporary-files

How to add extension to ruby Tempfile object?


How to add extension for Tempfile object?

image_path = "https://api.tinify.com/output/g85retpckb5fz2x8zpjrvtj0jcv1txm0"
image = open(image_path)
image.path # "/tmp/open-uri20191225-21585-oo95rb"

Now I want to make this file has jpg extension how can I do that?

I have also tried to convert it to File class but couldn't change extension too.

new_image = File.new(image)
new_image.path # "/tmp/open-uri20191225-21585-oo95rb"

Solution

  • If you're creating the Tempfile yourself you can do

    >> Tempfile.new([ 'foobar', '.xlsx' ]).path
    => "/tmp/foobar20130115-19153-1xhbncb-0.xlsx"