temporary Files in Ruby

Working with Ruby on Rails applications, many a timesĀ  such as in case of file upload services, generating/processing csv data, uploading data to external services like amazon there is a need to create temporary files.
A very common solution is to create an usual file object and delete it later. Imagine a scenario where you had created a large data file (say a 2GB) for temporary usage and forgot to delete it.

The Solution… Ruby Tempfile Class

Tempfile is a ruby utility class for managing temporary files. The class can be used to create temporary files. The file is generated with a unique name each time and is garbage collected when it goes out of scope. This save you the trouble to have to remove them explicitly.
Since explicitly temporary deleting files is good idea you can still do it with Tempfile object, Tempfile#unlink .
All actions on a File object are also valid on a Tempfile object, hence no loss of functionality.

Creating a using a temporary file with Tempfile class

For complete documentation of class ref Ruby Tempfile .