Issue #17295 has been updated by schneems (Richard Schneeman).


> Shouldn't this method take keyword arguments that FileUtils.touch accepts?


I looked into it. Of the existing pathnames that delegate to FileUtils, onl=
y one supports kwargs and it does not support all of them, just one:

```
=A0 def mkpath(mode: nil)
```

This was added by nobu 16 days ago https://github.com/ruby/ruby/commit/2dd2=
6bed86f721ed1982d00c3a0bd5ed37568e96. =


I explored what it would look like to support all kwargs and wrote it up. I=
t ended up being a little involved: https://gist.github.com/schneems/681a42=
ee54aa91a2185f49556469b319.

I am fine merging this and adding kwarg support as people see fit. Or if th=
e rest of core wants it in I can add support for all the kwargs that I've d=
escribed. I want to get some feedback before implementing such a change.

Pending an agreeable implementation what do you think of the opportunity to=
 add such an interface?

----------------------------------------
Feature #17295: Feature: Create a directory and file with Pathname#touch
https://bugs.ruby-lang.org/issues/17295#change-93679

* 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 c=
onfusing to have a `mkfile` and a `mkpath` where one creates a directory an=
d 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 intermediat=
e directories that don't yet
+  # exist.
+  #
+  # Example:
+  #
+  #   Dir.exist?("/a/b/c") # =3D> false
+  #
+  #   p =3D Pathname.new("/a/b/c/d.txt")
+  #   p.file? =3D> false
+  #   p.touch
+  #   p.file? =3D> true
+  #
+  #   Dir.exist?("/a/b/c") # =3D> 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=3Dunsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>