Skip to content

Instantly share code, notes, and snippets.

@stefanpenner
Created May 22, 2009 16:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save stefanpenner/116221 to your computer and use it in GitHub Desktop.
Save stefanpenner/116221 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
@timestamp ||= Time.now.to_i
dir = "/tmp/backup+#{@timestamp}"
mkdir_p dir
begin
cd dir do
yield
end
ensure
rm_rf dir
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment