How to create temp dir in Ruby?

后端 未结 2 915
醉酒成梦
醉酒成梦 2021-02-03 22:42

How do I create a temporary directory in Ruby in a nice way? I would also like to delete it automatically on process exit. Thanks!

2条回答
  •  旧巷少年郎
    2021-02-03 23:13

    Use the Dir.mktmpdir method, from the stdlib:

    require 'tmpdir'
    Dir.mktmpdir do |d|
      File.open("#{d}/1.txt", 'w') do |f|
        f.write('1.txt') 
      end
    end
    # at this point 1.txt and the dir no longer exist
    

提交回复
热议问题