From: get.codetriage@gmail.com
To: ruby-core@ruby-lang.org
Subject: [ruby-core:100671] [Ruby master Feature#17297] Feature: Introduce Pathname.mktmpdir
Date: Fri, 30 Oct 2020 15:09:41 +0000 (UTC) [thread overview]
Message-ID: <redmine.issue-17297.20201030150941.6346@ruby-lang.org> (raw)
In-Reply-To: <redmine.issue-17297.20201030150941.6346@ruby-lang.org>
Issue #17297 has been reported by schneems (Richard Schneeman).
----------------------------------------
Feature #17297: Feature: Introduce Pathname.mktmpdir
https://bugs.ruby-lang.org/issues/17297
* Author: schneems (Richard Schneeman)
* Status: Open
* Priority: Normal
----------------------------------------
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:
```ruby
Dir.mktmpdir do |dir|
dir = Pathname(dir)
# ... code
end
```
I would like to be able to instead:
```ruby
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
--
https://bugs.ruby-lang.org/
next parent reply other threads:[~2020-10-30 15:09 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-10-30 15:09 get.codetriage [this message]
2021-08-30 6:51 ` [ruby-core:105087] " hsbt (Hiroshi SHIBATA)
2024-10-04 2:16 ` [ruby-core:119439] " hsbt (Hiroshi SHIBATA) via ruby-core
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=redmine.issue-17297.20201030150941.6346@ruby-lang.org \
--to=get.codetriage@gmail.com \
--cc=ruby-core@ruby-lang.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).