---
title: "Temp Dir With Ruby"
description: "Played around with few programming languages to handle temp dirs, but amongst the lot /me really liked ruby’s paradigm in JS."
url: "https://h3manth.com/scribe/temp-dir-ruby/"
canonical_url: "https://h3manth.com/scribe/temp-dir-ruby/"
markdown_url: "https://h3manth.com/scribe/blog/temp-dir-ruby.md"
publishDate: "2012-09-06T00:00:00.000Z"
author: "Hemanth HM"
category: "web"
tags: ["ruby"]
word_count: 117
reading_time_minutes: 1
x_markdown_tokens: 268
content_signal: "ai-train=yes, search=yes, ai-input=yes"
---

# Temp Dir With Ruby

> Played around with few programming languages to handle temp dirs, but amongst the lot /me really liked ruby’s paradigm in JS.

Played around with few programming languages to handle temp dirs, but amongst the lot /me really liked ruby's paradigm.

**Mainly liked :**

  * yield for the win!

  * Easy maintenance of code.

  * Temp dir removal is assured.

**Code to manage temp dir :**

def in_tmp path = File.expand_path "#{Dir.tmpdir}/#{Time.now.to_i}#{rand(1000)}/" FileUtils.mkdir_p( path ) yield( path ) ensure # This ensures temp dir removal. FileUtils.rm_rf( path ) if File.exists?( path ) end

The above method can be used as :

in_tmp do |tmpdir| puts "In tmp dir: #{tmpdir}" end 

This code was improved with the help of a few hackers at #ruby, anyway there might be better ways to do this, do suggest your way below! Happy Hacking.
