ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:100669] [Ruby master Feature#17295] Feature: Create a directory and file with Pathname#touch
@ 2020-10-30 15:06 get.codetriage
  2021-08-30  6:51 ` [ruby-core:105088] " hsbt (Hiroshi SHIBATA)
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: get.codetriage @ 2020-10-30 15:06 UTC (permalink / raw)
  To: ruby-core

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

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

* Author: schneems (Richard Schneeman)
* Status: Open
* Priority: Normal
----------------------------------------


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/

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [ruby-core:105088] [Ruby master Feature#17295] Feature: Create a directory and file with Pathname#touch
  2020-10-30 15:06 [ruby-core:100669] [Ruby master Feature#17295] Feature: Create a directory and file with Pathname#touch get.codetriage
@ 2021-08-30  6:51 ` hsbt (Hiroshi SHIBATA)
  2021-09-01 13:23 ` [ruby-core:105105] " Dan0042 (Daniel DeLorme)
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: hsbt (Hiroshi SHIBATA) @ 2021-08-30  6:51 UTC (permalink / raw)
  To: ruby-core

Issue #17295 has been updated by hsbt (Hiroshi SHIBATA).

Assignee set to akr (Akira Tanaka)
Status changed from Open to Assigned

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

* 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/

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [ruby-core:105105] [Ruby master Feature#17295] Feature: Create a directory and file with Pathname#touch
  2020-10-30 15:06 [ruby-core:100669] [Ruby master Feature#17295] Feature: Create a directory and file with Pathname#touch get.codetriage
  2021-08-30  6:51 ` [ruby-core:105088] " hsbt (Hiroshi SHIBATA)
@ 2021-09-01 13:23 ` Dan0042 (Daniel DeLorme)
  2021-09-02 16:06 ` [ruby-core:105122] " schneems (Richard Schneeman)
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Dan0042 (Daniel DeLorme) @ 2021-09-01 13:23 UTC (permalink / raw)
  To: ruby-core

Issue #17295 has been updated by Dan0042 (Daniel DeLorme).


I agree having `Pathname#touch` would be nice, but the issue of making sure the parent dir exists is not limited to `touch`.
I often have code such as `path.tap{ |p| p.dirname.mkpath }.open("a"){ ... }`
So I think here it would be nice to have something like `Pathname#ensure_parent_dir_exists` (but with a shorter name) that can be used in various situations:
```ruby
path.ensure_parent_dir_exists.touch
path.ensure_parent_dir_exists.open('w'){...}
path.ensure_parent_dir_exists.write('w', str)
source.rename(dest.ensure_parent_dir_exists)
```


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

* 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/

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [ruby-core:105122] [Ruby master Feature#17295] Feature: Create a directory and file with Pathname#touch
  2020-10-30 15:06 [ruby-core:100669] [Ruby master Feature#17295] Feature: Create a directory and file with Pathname#touch get.codetriage
  2021-08-30  6:51 ` [ruby-core:105088] " hsbt (Hiroshi SHIBATA)
  2021-09-01 13:23 ` [ruby-core:105105] " Dan0042 (Daniel DeLorme)
@ 2021-09-02 16:06 ` schneems (Richard Schneeman)
  2021-09-14  6:24 ` [ruby-core:105245] " knu (Akinori MUSHA)
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: schneems (Richard Schneeman) @ 2021-09-02 16:06 UTC (permalink / raw)
  To: 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/

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [ruby-core:105245] [Ruby master Feature#17295] Feature: Create a directory and file with Pathname#touch
  2020-10-30 15:06 [ruby-core:100669] [Ruby master Feature#17295] Feature: Create a directory and file with Pathname#touch get.codetriage
                   ` (2 preceding siblings ...)
  2021-09-02 16:06 ` [ruby-core:105122] " schneems (Richard Schneeman)
@ 2021-09-14  6:24 ` knu (Akinori MUSHA)
  2021-09-15 19:24 ` [ruby-core:105268] " schneems (Richard Schneeman)
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: knu (Akinori MUSHA) @ 2021-09-14  6:24 UTC (permalink / raw)
  To: 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/

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [ruby-core:105268] [Ruby master Feature#17295] Feature: Create a directory and file with Pathname#touch
  2020-10-30 15:06 [ruby-core:100669] [Ruby master Feature#17295] Feature: Create a directory and file with Pathname#touch get.codetriage
                   ` (3 preceding siblings ...)
  2021-09-14  6:24 ` [ruby-core:105245] " knu (Akinori MUSHA)
@ 2021-09-15 19:24 ` schneems (Richard Schneeman)
  2021-09-16  1:17 ` [ruby-core:105273] " Dan0042 (Daniel DeLorme)
  2021-09-28  1:20 ` [ruby-core:105458] " schneems (Richard Schneeman)
  6 siblings, 0 replies; 8+ messages in thread
From: schneems (Richard Schneeman) @ 2021-09-15 19:24 UTC (permalink / raw)
  To: ruby-core

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, only one supports kwargs and it does not support all of them, just one:

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

This was added by nobu 16 days ago https://github.com/ruby/ruby/commit/2dd26bed86f721ed1982d00c3a0bd5ed37568e96. 

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

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.

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 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>

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [ruby-core:105273] [Ruby master Feature#17295] Feature: Create a directory and file with Pathname#touch
  2020-10-30 15:06 [ruby-core:100669] [Ruby master Feature#17295] Feature: Create a directory and file with Pathname#touch get.codetriage
                   ` (4 preceding siblings ...)
  2021-09-15 19:24 ` [ruby-core:105268] " schneems (Richard Schneeman)
@ 2021-09-16  1:17 ` Dan0042 (Daniel DeLorme)
  2021-09-28  1:20 ` [ruby-core:105458] " schneems (Richard Schneeman)
  6 siblings, 0 replies; 8+ messages in thread
From: Dan0042 (Daniel DeLorme) @ 2021-09-16  1:17 UTC (permalink / raw)
  To: ruby-core

Issue #17295 has been updated by Dan0042 (Daniel DeLorme).


In the end I agree that `touch` is enough and `ensure_parent_dir_exists` is unnecessary (even with a shorter name). Even though creating the file via "touch" is kinda  redundant before `open('a')` it's not really a problem either.

schneems (Richard Schneeman) wrote in #note-5:
> I explored what it would look like to support all kwargs and wrote it up. It ended up being a little involved: https://gist.github.com/schneems/681a42ee54aa91a2185f49556469b319.

The `nocreate` 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.


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

* 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/

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [ruby-core:105458] [Ruby master Feature#17295] Feature: Create a directory and file with Pathname#touch
  2020-10-30 15:06 [ruby-core:100669] [Ruby master Feature#17295] Feature: Create a directory and file with Pathname#touch get.codetriage
                   ` (5 preceding siblings ...)
  2021-09-16  1:17 ` [ruby-core:105273] " Dan0042 (Daniel DeLorme)
@ 2021-09-28  1:20 ` schneems (Richard Schneeman)
  6 siblings, 0 replies; 8+ messages in thread
From: schneems (Richard Schneeman) @ 2021-09-28  1:20 UTC (permalink / raw)
  To: ruby-core

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


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:

- https://bugs.ruby-lang.org/issues/17297

Also, this would be handy in cases:

- https://bugs.ruby-lang.org/issues/17296

For this `touch` feature, it's a nice-to-have. What do you think about adding `touch()` that just touches a file, and a kwarg that enables directory creation:

```
touch() # Just touches the file
touch(mkpath: true) # Touches and creates
```


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

* 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/

^ permalink raw reply related	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2021-09-28  1:20 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-30 15:06 [ruby-core:100669] [Ruby master Feature#17295] Feature: Create a directory and file with Pathname#touch get.codetriage
2021-08-30  6:51 ` [ruby-core:105088] " hsbt (Hiroshi SHIBATA)
2021-09-01 13:23 ` [ruby-core:105105] " Dan0042 (Daniel DeLorme)
2021-09-02 16:06 ` [ruby-core:105122] " schneems (Richard Schneeman)
2021-09-14  6:24 ` [ruby-core:105245] " knu (Akinori MUSHA)
2021-09-15 19:24 ` [ruby-core:105268] " schneems (Richard Schneeman)
2021-09-16  1:17 ` [ruby-core:105273] " Dan0042 (Daniel DeLorme)
2021-09-28  1:20 ` [ruby-core:105458] " schneems (Richard Schneeman)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).