Project

path-temp

0.0
No release in over a year
Utility for defining a path that, if the file exists, is deleted at the end of the block.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies
 Project Readme

path-temp

File.path_temp defines a temporary file path. It does not create the file. If, after the do block, a file exists at that path then the file is deleted.

#!/usr/bin/ruby -w
require 'file/path-temp'

# call File.path_temp
File.path_temp() do |path|
  # defines a path in current directory with a bunch of random numbers as
  # the name e.g. ./18168068873296828
  puts path 
  
  # file is *not* automatically created
  puts File.exist?(path) # => false
  
  # write to the path
  File.write path, 'whatever'
  
  # file now exists
  puts File.exist?(path) # => true
end

# at this point, the file no longer exists

root

To put the path somewhere besides the current directory, use the root option:

File.path_temp('root'=>'/tmp') do |path|
  puts path # => e,g, /tmp/458577463617317
end

extension

To give the path an extension, use the ext option:

File.path_temp('ext'=>'txt') do |path|
  puts path # => e,g, ./5314921893963471.txt
end

Installation

The usual:

sudo gem install path-temp

Or however you like to install gems. It's just a single file.

License

Open source, MIT license.

Author

Mike O'Sullivan mike@idocs.com

History

version date notes
1.0 Jun 29, 2023 Initial upload.