From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS4713 221.184.0.0/13 X-Spam-Status: No, score=-4.1 required=3.0 tests=AWL,BAYES_00, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_PASS, UNPARSEABLE_RELAY shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from neon.ruby-lang.org (neon.ruby-lang.org [221.186.184.75]) by dcvr.yhbt.net (Postfix) with ESMTP id 8BAE31F8C8 for ; Thu, 16 Sep 2021 01:17:20 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id A1190120BF4; Thu, 16 Sep 2021 10:15:54 +0900 (JST) Received: from o1678948x4.outbound-mail.sendgrid.net (o1678948x4.outbound-mail.sendgrid.net [167.89.48.4]) by neon.ruby-lang.org (Postfix) with ESMTPS id 059B3120BF4 for ; Thu, 16 Sep 2021 10:15:52 +0900 (JST) Received: by filterdrecv-7bf5c69d5-fb76v with SMTP id filterdrecv-7bf5c69d5-fb76v-1-61429B14-3B 2021-09-16 01:17:08.851218163 +0000 UTC m=+1220164.651915541 Received: from herokuapp.com (unknown) by ismtpd0148p1mdw1.sendgrid.net (SG) with ESMTP id Wsa_YIceRqq8N6EOC6dLKg for ; Thu, 16 Sep 2021 01:17:08.731 +0000 (UTC) Date: Thu, 16 Sep 2021 01:17:08 +0000 (UTC) From: "Dan0042 (Daniel DeLorme)" Message-ID: References: Mime-Version: 1.0 X-Redmine-Project: ruby-master X-Redmine-Issue-Tracker: Feature X-Redmine-Issue-Id: 17295 X-Redmine-Issue-Author: schneems X-Redmine-Issue-Assignee: akr X-Redmine-Sender: Dan0042 X-Mailer: Redmine X-Redmine-Host: bugs.ruby-lang.org X-Redmine-Site: Ruby Issue Tracking System X-Auto-Response-Suppress: All Auto-Submitted: auto-generated X-Redmine-MailingListIntegration-Message-Ids: 81449 X-SG-EID: =?us-ascii?Q?9vnO9kNFlf1pwhty1clU3mt9eNWYniufdXqocdsQQtaDTWZk4+b5g5js0Akvaj?= =?us-ascii?Q?32LW=2F+fGSAom3tt8Jo+=2FGW3uDD0ktbmu7qAbln0?= =?us-ascii?Q?uJrjICJtWjOfWkBg++4bWQkAsWCnzGCOHt8PTF8?= =?us-ascii?Q?H59RTmGXueJ2YC3Q56G9Y+s3GnvM7DZidF1VTpR?= =?us-ascii?Q?C1amlTi1D5ROb7B=2Fb7Z17MGHQn3ClZudVJS4Njx?= =?us-ascii?Q?SoUkbLrbH4cZLZlcne70oiL68vFvyTEYedy27SP?= =?us-ascii?Q?wOHIF2QvB5vJG+0pW3TvQ=3D=3D?= To: ruby-core@ruby-lang.org X-Entity-ID: b/2+PoftWZ6GuOu3b0IycA== X-ML-Name: ruby-core X-Mail-Count: 105273 Subject: [ruby-core:105273] [Ruby master Feature#17295] Feature: Create a directory and file with Pathname#touch X-BeenThere: ruby-core@ruby-lang.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: Ruby developers List-Id: Ruby developers List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ruby-core-bounces@ruby-lang.org Sender: "ruby-core" 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/