This blog is part of our Ruby 2.4 series.
Tempfile is used for managing temporary files in Ruby. A Tempfile object creates a temporary file with a unique filename. It behaves just like a File object, and therefore we can perform all the usual file operations on it.
Why Tempfile when we can use File
These days it is common
to store file on services like S3.
Let’s say that we have a
file on S3.
Working with this file remotely is
In such cases it is desirable
to download the file on local machine
After the work is done then
file should be deleted.
Tempfile is ideal for such cases.
Basename for tempfile
If we want to create a temporary file then we needed to pass parameter to it prior to Ruby 2.3.
As we can see above the generated file name begins with “bigbinary” word.
Since Tempfile ensures that the generate filename will always be unique the point of passing the argument is meaningless. Ruby doc calls this passing “basename”.
So in Ruby 2.3.0 it was
that the basename parameter
was meaningless for
and an empty string
will be the default value.
But the same was not implemented for
in Ruby 2.4.
So now the basename parameter
is set to
empty string by default,
to keep it
consistent with the