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 E93581F8C6 for ; Tue, 14 Sep 2021 06:24:43 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id CB219120A4F; Tue, 14 Sep 2021 15:23:19 +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 7E8DE120A0F for ; Tue, 14 Sep 2021 15:23:17 +0900 (JST) Received: by filterdrecv-7bf5c69d5-fb76v with SMTP id filterdrecv-7bf5c69d5-fb76v-1-61404026-5 2021-09-14 06:24:38.171908969 +0000 UTC m=+1065813.972606358 Received: from herokuapp.com (unknown) by geopod-ismtpd-4-0 (SG) with ESMTP id 9cqO7swrRg6QnvwXoc2nDg for ; Tue, 14 Sep 2021 06:24:38.015 +0000 (UTC) Date: Tue, 14 Sep 2021 06:24:38 +0000 (UTC) From: "knu (Akinori MUSHA)" 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: knu 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: 81421 X-SG-EID: =?us-ascii?Q?OfuCcQLTUeSkgvg2zt7WGolyX0xr4xICUddkcqMptxz7TOYwO=2Fom5us72hswHt?= =?us-ascii?Q?SryUsdfD8mWRXeiIT8WvFoFehDj4dodp2emCqhA?= =?us-ascii?Q?E9pThqHk9VpfLcOgQ3uAXCVNws22H9QZtDqANbg?= =?us-ascii?Q?7FQGhVjhDzHjyl087UI+IhyCUktiNCUFIaUGQ7N?= =?us-ascii?Q?lLB+85eimrnGHdS84Lw8xqkOuwt4sSH3pFpiGv7?= =?us-ascii?Q?=2F=2FoEnMIJlsI1enOJ7vsrrbPr37JBkidlyxsG=2Fw1?= =?us-ascii?Q?Z=2FwXCG5+ho6e311Ml2+yg=3D=3D?= To: ruby-core@ruby-lang.org X-Entity-ID: b/2+PoftWZ6GuOu3b0IycA== X-ML-Name: ruby-core X-Mail-Count: 105245 Subject: [ruby-core:105245] [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 knu (Akinori MUSHA). Shouldn't this method take keyword arguments that FileUtils.touch accepts? ---------------------------------------- Feature #17295: Feature: Create a directory and file with Pathname#touch https://bugs.ruby-lang.org/issues/17295#change-93652 * 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/