What is the best way to get an empty temporary directory in Ruby on Rails?

后端 未结 9 605

What is the best way to get a temporary directory with nothing in it using Ruby on Rails? I need the API to be cross-platform compatible. The stdlib tmpdir won\'t work.

9条回答
  •  长发绾君心
    2020-12-28 13:12

    Check out the Ruby STemp library: http://ruby-stemp.rubyforge.org/rdoc/

    If you do something like this:

    dirname = STemp.mkdtemp("#{Dir.tmpdir}/directory-name-template-XXXXXXXX")
    

    dirname will be a string that points to a directory that's guaranteed not to exist previously. You get to define what you want the directory name to start with. The X's get replaced with random characters.

    EDIT: someone mentioned this didn't work for them on 1.9, so YMMV.

提交回复
热议问题