Is there a way to rename created temp files in ruby.
file = Tempfile.new("prefix", Dir.tmpdir, encoding: 'ascii-8bit')
file.write(content)
file.close
file
#File:/var/XXX/mz/37g0z42324fffnqy_27h0000gn/T/prefix20210223-44327-1mn74mk
I want a temp file to renamed with the given name.This is to retain the original name of the file. I want the file to be short lived only so creating a normal file wont work in this case.
But I tried FileUtils like this to see if copy works.
file = Tempfile.new("XXZZA", Dir.tmpdir, encoding: 'ascii-8bit')
=> #<File:/var/folders/mz/37g0zms52cg6bh0n5nqy_27h0000gn/T/XXZZA20210223-44327-10gvnou>
[78] pry(main)> file.write("Its a new text file")
=> 19
[79] pry(main)> nfile = File.join("public", "XXXXAAnew")
=> "public/XXXXAAnew"
[80] pry(main)> FileUtils.cp file.path, nfile
=> nil
[81] pry(main)> file.close
=> nil
[82] pry(main)> file
=> #<File:/var/folders/mz/37g0zms52cg6bh0n5nqy_27h0000gn/T/XXZZA20210223-44327-10gvnou (closed)>
[83] pry(main)> nfile
"public/XXXXAAnew"
but the file is empty
also, copied file is not cleaned up by garbage collector