< :前の番号
^ :番号順リスト
> :次の番号
P :前の記事(スレッド移動)
N :次の記事(スレッド移動)
|<:前のスレッド
>|:次のスレッド
^ :返事先
_:自分への返事
>:同じ返事先を持つ記事(前)
<:同じ返事先を持つ記事(後)
---:分割してスレッド表示、再表示
| :分割して(縦)スレッド表示、再表示
~ :スレッドのフレーム消去
.:インデックス
..:インデックスのインデックス
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/
Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>