Tempfile gives random extensions by default

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the ruby category.

Last Updated: 2021-05-16

Tempfile puts random crud AFTER the file extension by default in Ruby

Tempfile.new("thing.zip")
 # => .../thing.zip20191230-37984-1eqfs9q>

This leads to surprises, since you cannot open the file in the expected programs (e.g. an unzipper) or test for that file extension elsewhere.

The solution is to pass in an array where the second item is the file extension:

Tempfile.new(["thing", ".zip"] # note the inclusion of the dot in front of the
# "zip"
 # => .../thing20191230-37984-1eqfs9q.zip>