ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: "Dan0042 (Daniel DeLorme)" <noreply@ruby-lang.org>
To: ruby-core@ruby-lang.org
Subject: [ruby-core:105273] [Ruby master Feature#17295] Feature: Create a directory and file with Pathname#touch
Date: Thu, 16 Sep 2021 01:17:08 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-93684.20210916011708.6346@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-17295.20201030150639.6346@ruby-lang.org

Issue #17295 has been updated by Dan0042 (Daniel DeLorme).


In the end I agree that `touch` is enough and `ensure_parent_dir_exists` is unnecessary (even with a shorter name). Even though creating the file via "touch" is kinda  redundant before `open('a')` it's not really a problem either.

schneems (Richard Schneeman) wrote in #note-5:
> I explored what it would look like to support all kwargs and wrote it up. It ended up being a little involved: https://gist.github.com/schneems/681a42ee54aa91a2185f49556469b319.

The `nocreate` option is intended to update the timestamp on an existing file. It's like "noop if file doesn't exist". So in the case the file doesn't exist, IMHO it shouldn't create the directories either.


----------------------------------------
Feature #17295: Feature: Create a directory and file with Pathname#touch
https://bugs.ruby-lang.org/issues/17295#change-93684

* Author: schneems (Richard Schneeman)
* Status: Assigned
* Priority: Normal
* Assignee: akr (Akira Tanaka)
----------------------------------------


Right now if a developer wants to create a file and is not sure if the path exists yet or not they must:

```ruby
Pathname.new("/a/b/c/d.txt").tap {|p| p.dirname.mkpath; FileUtils.touch(p)}
```

After this patch a developer can instead call:

```ruby
Pathname.new("/a/b/c/d.txt").touch
```

An alternative name for this behavior could be `mkfile` but I think it is confusing to have a `mkfile` and a `mkpath` where one creates a directory and one creates a file.

Diff:

```
$ git diff master
diff --git a/ext/pathname/lib/pathname.rb b/ext/pathname/lib/pathname.rb
index e6fb90277d..2ed02a6633 100644
--- a/ext/pathname/lib/pathname.rb
+++ b/ext/pathname/lib/pathname.rb
@@ -585,6 +585,27 @@ def mkpath
     nil
   end

+  # Creates a file and the full path to the file including any intermediate directories that don't yet
+  # exist.
+  #
+  # Example:
+  #
+  #   Dir.exist?("/a/b/c") # => false
+  #
+  #   p = Pathname.new("/a/b/c/d.txt")
+  #   p.file? => false
+  #   p.touch
+  #   p.file? => true
+  #
+  #   Dir.exist?("/a/b/c") # => true
+  def touch
+    require 'fileutils'
+    dirname.mkpath
+
+    FileUtils.touch(self)
+    self
+  end
+
   # Recursively deletes a directory, including all directories beneath it.
   #
   # See FileUtils.rm_r
diff --git a/test/pathname/test_pathname.rb b/test/pathname/test_pathname.rb
index 43cef4849f..3c518cc3da 100644
--- a/test/pathname/test_pathname.rb
+++ b/test/pathname/test_pathname.rb
@@ -1394,6 +1394,14 @@ def test_mkpath
     }
   end

+  def test_touch
+    with_tmpchdir('rubytest-pathname') {|dir|
+      Pathname("a/b/c/d.txt").touch
+      assert_file.directory?("a/b/c")
+      assert_file.file?("a/b/c/d.txt")
+    }
+  end
+
   def test_rmtree
     with_tmpchdir('rubytest-pathname') {|dir|
       Pathname("a/b/c/d").mkpath
```


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





-- 
https://bugs.ruby-lang.org/

  parent reply	other threads:[~2021-09-16  1:17 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-30 15:06 [ruby-core:100669] [Ruby master Feature#17295] Feature: Create a directory and file with Pathname#touch get.codetriage
2021-08-30  6:51 ` [ruby-core:105088] " hsbt (Hiroshi SHIBATA)
2021-09-01 13:23 ` [ruby-core:105105] " Dan0042 (Daniel DeLorme)
2021-09-02 16:06 ` [ruby-core:105122] " schneems (Richard Schneeman)
2021-09-14  6:24 ` [ruby-core:105245] " knu (Akinori MUSHA)
2021-09-15 19:24 ` [ruby-core:105268] " schneems (Richard Schneeman)
2021-09-16  1:17 ` Dan0042 (Daniel DeLorme) [this message]
2021-09-28  1:20 ` [ruby-core:105458] " schneems (Richard Schneeman)

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-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-93684.20210916011708.6346@ruby-lang.org \
    --to=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 read-only IMAP folder(s) and NNTP newsgroup(s).