Project

General

Profile

Actions

Feature #17297

closed

Feature: Introduce Pathname.mktmpdir

Added by schneems (Richard Schneeman) about 4 years ago. Updated 3 months ago.

Status:
Closed
Target version:
-
[ruby-core:100671]

Description

When I want to create a tmpdir I often want to manipulate it as a pathname. By introducing Pathname.mktmpdir I can get this behavior.

Currently I must:

Dir.mktmpdir do |dir|
  dir = Pathname(dir)
  # ... code
end

I would like to be able to instead:

Pathname.mktmpdir do |dir|
  # ... code
end

Diff:

$ git diff master
diff --git a/ext/pathname/lib/pathname.rb b/ext/pathname/lib/pathname.rb
index e6fb90277d..ec32e7d611 100644
--- a/ext/pathname/lib/pathname.rb
+++ b/ext/pathname/lib/pathname.rb
@@ -597,3 +597,20 @@ def rmtree
   end
 end

+class Pathname    # * tmpdir *
+  # Creates a tmp directory and wraps the returned path in a Pathname object.
+  #
+  # See Dir.mktmpdir
+  def self.mktmpdir
+    require 'tmpdir' unless defined?(Dir.mktmpdir)
+    if block_given?
+      Dir.mktmpdir do |dir|
+        dir = self.new(dir)
+        yield dir
+      end
+    else
+      self.new(Dir.mktmpdir)
+    end
+  end
+end
+
diff --git a/test/pathname/test_pathname.rb b/test/pathname/test_pathname.rb
index 43cef4849f..8edcccf666 100644
--- a/test/pathname/test_pathname.rb
+++ b/test/pathname/test_pathname.rb
@@ -1272,6 +1272,14 @@ def test_s_glob_3args
     }
   end

+  def test_mktmpdir
+    Pathname.mktmpdir do |dir|
+      assert_equal Pathname(dir), dir
+      assert dir.directory?
+      assert dir.exist?
+    end
+  end
+
   def test_s_getwd
     wd = Pathname.getwd
     assert_kind_of(Pathname, wd)

Github link: https://github.com/ruby/ruby/pull/3709

Updated by hsbt (Hiroshi SHIBATA) over 3 years ago

  • Status changed from Open to Assigned
  • Assignee set to akr (Akira Tanaka)

Updated by hsbt (Hiroshi SHIBATA) 3 months ago

  • Status changed from Assigned to Closed

This feature request has been approved by @akr (Akira Tanaka). I merged https://github.com/ruby/ruby/pull/3709 now.

Actions

Also available in: Atom PDF

Like0
Like0Like0