https://redmine.ruby-lang.org/https://redmine.ruby-lang.org/favicon.ico?17113305112021-08-30T06:51:59ZRuby Issue Tracking SystemRuby master - Feature #17295: Feature: Create a directory and file with Pathname#touchhttps://redmine.ruby-lang.org/issues/17295?journal_id=934972021-08-30T06:51:59Zhsbt (Hiroshi SHIBATA)hsbt@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Assigned</i></li><li><strong>Assignee</strong> set to <i>akr (Akira Tanaka)</i></li></ul> Ruby master - Feature #17295: Feature: Create a directory and file with Pathname#touchhttps://redmine.ruby-lang.org/issues/17295?journal_id=935142021-09-01T13:23:15ZDan0042 (Daniel DeLorme)
<ul></ul><p>I agree having <code>Pathname#touch</code> would be nice, but the issue of making sure the parent dir exists is not limited to <code>touch</code>.<br>
I often have code such as <code>path.tap{ |p| p.dirname.mkpath }.open("a"){ ... }</code><br>
So I think here it would be nice to have something like <code>Pathname#ensure_parent_dir_exists</code> (but with a shorter name) that can be used in various situations:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">path</span><span class="p">.</span><span class="nf">ensure_parent_dir_exists</span><span class="p">.</span><span class="nf">touch</span>
<span class="n">path</span><span class="p">.</span><span class="nf">ensure_parent_dir_exists</span><span class="p">.</span><span class="nf">open</span><span class="p">(</span><span class="s1">'w'</span><span class="p">){</span><span class="o">...</span><span class="p">}</span>
<span class="n">path</span><span class="p">.</span><span class="nf">ensure_parent_dir_exists</span><span class="p">.</span><span class="nf">write</span><span class="p">(</span><span class="s1">'w'</span><span class="p">,</span> <span class="n">str</span><span class="p">)</span>
<span class="n">source</span><span class="p">.</span><span class="nf">rename</span><span class="p">(</span><span class="n">dest</span><span class="p">.</span><span class="nf">ensure_parent_dir_exists</span><span class="p">)</span>
</code></pre> Ruby master - Feature #17295: Feature: Create a directory and file with Pathname#touchhttps://redmine.ruby-lang.org/issues/17295?journal_id=935352021-09-02T16:06:45Zschneems (Richard Schneeman)
<ul></ul><p>For the example you gave:</p>
<pre><code>path.tap{ |p| p.dirname.mkpath }.open("a"){ ... }
</code></pre>
<p>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 <code>touch</code>. It could be shorter as:</p>
<pre><code>path.touch.open("a") { ... }
</code></pre>
<p>I see two cases:</p>
<ul>
<li>Want to <code>mkdir -p</code> the parent and the path points to a file: This proposed touch interface would accommodate that.</li>
<li>Want to <code>mkdir -p</code> the parent and the path points to a dir: Then the dev can use <code>mkpath</code>. I can't think of a situation you would want to have the parent dir created, but not the full path.</li>
</ul>
<p>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:</p>
<pre><code>path.ensure_parent_dir_exists.open('w'){...}
path.tap{|p|p.dirname.mkpath}.open('w'){...} # Same length if you remove whitespace
</code></pre>
<p>I think that adding a <code>touch</code> that also does <code>mkdir -p</code> 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.</p>
<p>Back to this proposal, we could add a <code>touch</code> that only creates the file without a <code>mkdir -p</code>. 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 <code>touch(skip_mkpath: true)</code>, however someone can still use the regular FileUtils.touch if they want:</p>
<pre><code>touch(skip_mkpath: true)
tap{|p|FileUtils.touch(p)}
</code></pre> Ruby master - Feature #17295: Feature: Create a directory and file with Pathname#touchhttps://redmine.ruby-lang.org/issues/17295?journal_id=936522021-09-14T06:24:37Zknu (Akinori MUSHA)knu@ruby-lang.org
<ul></ul><p>Shouldn't this method take keyword arguments that FileUtils.touch accepts?</p> Ruby master - Feature #17295: Feature: Create a directory and file with Pathname#touchhttps://redmine.ruby-lang.org/issues/17295?journal_id=936792021-09-15T19:24:51Zschneems (Richard Schneeman)
<ul></ul><blockquote>
<p>Shouldn't this method take keyword arguments that FileUtils.touch accepts?</p>
</blockquote>
<p>I looked into it. Of the existing pathnames that delegate to FileUtils, only one supports kwargs and it does not support all of them, just one:</p>
<pre><code> def mkpath(mode: nil)
</code></pre>
<p>This was added by nobu 16 days ago <a href="https://github.com/ruby/ruby/commit/2dd26bed86f721ed1982d00c3a0bd5ed37568e96" class="external">https://github.com/ruby/ruby/commit/2dd26bed86f721ed1982d00c3a0bd5ed37568e96</a>.</p>
<p>I explored what it would look like to support all kwargs and wrote it up. It ended up being a little involved: <a href="https://gist.github.com/schneems/681a42ee54aa91a2185f49556469b319" class="external">https://gist.github.com/schneems/681a42ee54aa91a2185f49556469b319</a>.</p>
<p>I am fine merging this and adding kwarg support as people see fit. Or if the rest of core wants it in I can add support for all the kwargs that I've described. I want to get some feedback before implementing such a change.</p>
<p>Pending an agreeable implementation what do you think of the opportunity to add such an interface?</p> Ruby master - Feature #17295: Feature: Create a directory and file with Pathname#touchhttps://redmine.ruby-lang.org/issues/17295?journal_id=936842021-09-16T01:17:08ZDan0042 (Daniel DeLorme)
<ul></ul><p>In the end I agree that <code>touch</code> is enough and <code>ensure_parent_dir_exists</code> is unnecessary (even with a shorter name). Even though creating the file via "touch" is kinda redundant before <code>open('a')</code> it's not really a problem either.</p>
<p>schneems (Richard Schneeman) wrote in <a href="#note-5">#note-5</a>:</p>
<blockquote>
<p>I explored what it would look like to support all kwargs and wrote it up. It ended up being a little involved: <a href="https://gist.github.com/schneems/681a42ee54aa91a2185f49556469b319" class="external">https://gist.github.com/schneems/681a42ee54aa91a2185f49556469b319</a>.</p>
</blockquote>
<p>The <code>nocreate</code> 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.</p> Ruby master - Feature #17295: Feature: Create a directory and file with Pathname#touchhttps://redmine.ruby-lang.org/issues/17295?journal_id=939082021-09-28T01:20:09Zschneems (Richard Schneeman)
<ul></ul><p>For what it's worth this idea isn't my favorite. I would LOVE to have a mktmpdir that returns a pathname instead of a string:</p>
<ul>
<li><a href="https://bugs.ruby-lang.org/issues/17297" class="external">https://bugs.ruby-lang.org/issues/17297</a></li>
</ul>
<p>Also, this would be handy in cases:</p>
<ul>
<li><a href="https://bugs.ruby-lang.org/issues/17296" class="external">https://bugs.ruby-lang.org/issues/17296</a></li>
</ul>
<p>For this <code>touch</code> feature, it's a nice-to-have. What do you think about adding <code>touch()</code> that just touches a file, and a kwarg that enables directory creation:</p>
<pre><code>touch() # Just touches the file
touch(mkpath: true) # Touches and creates
</code></pre>