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/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>