Skip to content

Instantly share code, notes, and snippets.

@avescodes
Forked from stefanpenner/gist:116221
Created May 22, 2009 16:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save avescodes/116229 to your computer and use it in GitHub Desktop.
Save avescodes/116229 to your computer and use it in GitHub Desktop.
# helpfull for performing actions that require temp directories
# 1. handles temp file creation
# 2. lets you do your business in da 'block'
# 3. cleans up after itself
# usage
require 'fileutils'
include FileUtils
temp do
mkdir_p 'some/long/path'
touch 'some compile stuff etc.etc.'
end
# now its all cleaned up
# implementation
def temp(opts = {})
@timestamp ||= Time.now.to_i
dir = "/tmp/backup+#{@timestamp}"
mkdir_p dir
begin
cd dir do
yield
end
ensure
#not tested, only theorized
if opts[:save_to]
mv dir, opts[:save_to]
end
rm_rf dir
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment