Ruby: How to ensure a Tempfile's extension

If you use Tempfile and pass your own filename containing an extension, it will just be consumed by the Tempfile's filename:

>> Tempfile.new('foobar.xlsx').path
=> "/tmp/foobar.xlsx20130115-19153-4ykpwm-0"

If you want to keep the file extension, pass filename and extension as an array:

>> Tempfile.new([ 'foobar', '.xlsx' ]).path
=> "/tmp/foobar20130115-19153-1xhbncb-0.xlsx"
Arne Hartherz Over 11 years ago