Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"
like image 674
Ahmed Salah Avatar asked Apr 19 '26 20:04

Ahmed Salah


1 Answers

If you're creating the Tempfile yourself you can do

>> Tempfile.new([ 'foobar', '.xlsx' ]).path
=> "/tmp/foobar20130115-19153-1xhbncb-0.xlsx"
like image 98
xxjjnn Avatar answered Apr 22 '26 09:04

xxjjnn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!