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 4C0941F8C6 for ; Thu, 2 Sep 2021 16:06:57 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 97B451209F2; Fri, 3 Sep 2021 01:05:29 +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 35CD71209E3 for ; Fri, 3 Sep 2021 01:05:28 +0900 (JST) Received: by filterdrecv-7bc86b958d-g5ggb with SMTP id filterdrecv-7bc86b958d-g5ggb-1-6130F695-C0 2021-09-02 16:06:45.628664734 +0000 UTC m=+64009.168903164 Received: from herokuapp.com (unknown) by geopod-ismtpd-3-1 (SG) with ESMTP id FZxXUmbuTS2eFrFj4AEc1Q for ; Thu, 02 Sep 2021 16:06:45.565 +0000 (UTC) Date: Thu, 02 Sep 2021 16:06:45 +0000 (UTC) From: "schneems (Richard Schneeman)" 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: schneems 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: 81300 X-SG-EID: =?us-ascii?Q?EnEEJb7=2F=2FojRm3A6TBYrv4b78cm4+wob4l6BNl9Cx7=2FPMwJjcqeKg4U6oO19FK?= =?us-ascii?Q?Dwj1NGFmJkes6=2FzjTAfxKfO=2F3qwu+U61=2FXo+SXs?= =?us-ascii?Q?YMDILQfGmLiXF13fiXLGW=2F74+2jBhvORIauHsKg?= =?us-ascii?Q?ditY2YFGcne7gYRCmxcD8a1=2Fz0W5Tf6Zx4Rwd+5?= =?us-ascii?Q?TsC21gp1EtFvtBKEipybCyWeWvVlD129SEC23DR?= =?us-ascii?Q?kVuq104zgwy+4uM6ZWkFInqzKGFft3u65Uf5YsJ?= =?us-ascii?Q?Dyn96R4=2FqcRYmAwjEnqVw=3D=3D?= To: ruby-core@ruby-lang.org X-Entity-ID: b/2+PoftWZ6GuOu3b0IycA== X-ML-Name: ruby-core X-Mail-Count: 105122 Subject: [ruby-core:105122] [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 schneems (Richard Schneeman). For the example you gave: ``` path.tap{ |p| p.dirname.mkpath }.open("a"){ ... } ``` It looks like you want to ensure a file is created in a directory that exists. I actually think that would be a good use case for the proposed `touch`. It could be shorter as: ``` path.touch.open("a") { ... } ``` I see two cases: - Want to `mkdir -p` the parent and the path points to a file: This proposed touch interface would accommodate that. - Want to `mkdir -p` the parent and the path points to a dir: Then the dev can use `mkpath`. I can't think of a situation you would want to have the parent dir created, but not the full path. I'm not opposed to adding a specialized method that creates the parent dir, but I think that should be a separate proposal. I also think the name would need to be both specific and short-ish: ``` path.ensure_parent_dir_exists.open('w'){...} path.tap{|p|p.dirname.mkpath}.open('w'){...} # Same length if you remove whitespace ``` I think that adding a `touch` that also does `mkdir -p` of the parent dir buys us the same functionality (if there's some cases I've not considered, that would be good to put into the separate proposal. Back to this proposal, we could add a `touch` that only creates the file without a `mkdir -p`. But I don't know why someone would ever want to touch a file that doesn't exist. If they're wanting an error there are other ways to get it. We could also make it configurable `touch(skip_mkpath: true)`, however someone can still use the regular FileUtils.touch if they want: ``` touch(skip_mkpath: true) tap{|p|FileUtils.touch(p)} ``` ---------------------------------------- Feature #17295: Feature: Create a directory and file with Pathname#touch https://bugs.ruby-lang.org/issues/17295#change-93535 * 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/