Module: TmpFile

Defined in:
lib/rbbt/util/tmpfile.rb

Class Method Summary collapse

Class Method Details

.random_name(s = "", max = 10000000) ⇒ Object

Creates a random file name, with the given suffix and a random number up to max



9
10
11
12
13
# File 'lib/rbbt/util/tmpfile.rb', line 9

def self.random_name( s="",max=10000000)
  n = rand(max)
  s << n.to_s
  s
end

.tmp_file(s = "", max = 10000000) ⇒ Object Also known as: new

Creates a random filename in the temporary directory



16
17
18
# File 'lib/rbbt/util/tmpfile.rb', line 16

def self.tmp_file(s = "",max=10000000)
  File.join(Rbbt.tmpdir,random_name(s,max))
end

.with_file(content = nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rbbt/util/tmpfile.rb', line 20

def self.with_file(content = nil)
  tmpfile = tmp_file

  File.open(tmpfile, 'w') do |f| f.write content end if content != nil

  result = yield(tmpfile)

  FileUtils.rm tmpfile if File.exists? tmpfile

  result
end