Because blocks!
This code creates a temporary directory and a file foo.txt
therein:
require 'tmpdir'
Dir.mktmpdir do |dir|
open("#{dir}/foo.txt", 'w') do |file|
# Do something with the file.
end
end
When the block exits, the directory and its contents are deleted automatically.
Easy peasy!