Why I Love Coding in Ruby: Dir.mktmpdir

Burdette Lamar - Nov 3 '18 - - Dev Community

Because blocks!

This code creates a temporary directory and a file foo.txt therein:

require 'tmpdir'

Dir.mktmpdir do |dir|
  open("#{dir}/foo.txt", 'w') do |file|
    # Do something with the file.
  end
end

When the block exits, the directory and its contents are deleted automatically.

Easy peasy!

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player