1

I want to create a temp file:

def create_file
  FileUtils.mkdir_p('/var/log/my_app')
  tmp_file = '/var/log/my_app/tmp_file'
  File.open(tmp_file, 'w') do |file|
    file.write 'test'
  end
end

Here I am sure that the /var/log/my_app path exists. But after I run this method, I can't find a file named tmp_file under that path. And there wasn't any error, too.

sawa
  • 165,429
  • 45
  • 277
  • 381
s-cho-m
  • 967
  • 2
  • 13
  • 28
  • @saadq Thank you for your reply. I have read that question. I don't know what's the reason why my source doesn't work. – s-cho-m Aug 12 '15 at 02:06

2 Answers2

2

I think you would do better using Ruby's TempFile class and perhaps even Ruby's temp dir as suggested in this article: Quick tips for doing IO with Ruby.

I think you will find the article helpful. I believe it will make your approach easier - especially regarding deleting the file once you're done with it.

Myst
  • 18,516
  • 2
  • 45
  • 67
0

I don't see any error in your code. If you don't get any exception, the file must have been created, if this function has been executed.

I suggest that you make a test at the end of create_file:

if File.file?
    puts "File has been created"
else
    fail "File is not there!"
end

If you see "File has been created", but the file is still missing, something must have erased it before you had time to check its presence. If you see "File is not there!", something weird is going on and I would call an exorcist. If you don't see any message, it means that your function has not been executed.

user1934428
  • 19,864
  • 7
  • 42
  • 87