ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:100668] [Ruby master Feature#17294] Feature: Allow method chaining with Pathname#mkpath Pathname#rmtree
@ 2020-10-30 15:04 get.codetriage
  2020-10-31  0:56 ` [ruby-core:100676] " brooke
  2021-08-30  6:52 ` [ruby-core:105089] " hsbt (Hiroshi SHIBATA)
  0 siblings, 2 replies; 3+ messages in thread
From: get.codetriage @ 2020-10-30 15:04 UTC (permalink / raw
  To: ruby-core

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

----------------------------------------
Feature #17294: Feature: Allow method chaining with Pathname#mkpath Pathname#rmtree
https://bugs.ruby-lang.org/issues/17294

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

Currently in my code when I want to create a pathname object and create a path at the same time I must use tap

```
path = Pathname.new("/tmp/new").tap(&:mkpath)
```

I think it would be cleaner to be able to chain on the results of these methods instead:

```
path = Pathname.new("/tmp/new").mkpath
```

This is a change in return value but after research on github I do not believe many (if any) are relying on the current behavior to return nil https://github.com/search?l=&p=1&q=.mkpath+language%3ARuby&ref=advsearch&type=Code.

Here is my diff: 

```
$ git diff master schneems/return-self-pathname
diff --git a/ext/pathname/lib/pathname.rb b/ext/pathname/lib/pathname.rb
index e6fb90277d..f1eb1e00ae 100644
--- a/ext/pathname/lib/pathname.rb
+++ b/ext/pathname/lib/pathname.rb
@@ -582,7 +582,7 @@ class Pathname    # * FileUtils *
   def mkpath
     require 'fileutils'
     FileUtils.mkpath(@path)
-    nil
+    self
   end

   # Recursively deletes a directory, including all directories beneath it.
@@ -593,7 +593,7 @@ def rmtree
     # File::Path provides "mkpath" and "rmtree".
     require 'fileutils'
     FileUtils.rm_r(@path)
-    nil
+    self
   end
 end

diff --git a/test/pathname/test_pathname.rb b/test/pathname/test_pathname.rb
index 43cef4849f..149fe15c3a 100644
--- a/test/pathname/test_pathname.rb
+++ b/test/pathname/test_pathname.rb
@@ -1389,7 +1389,8 @@ def test_find

   def test_mkpath
     with_tmpchdir('rubytest-pathname') {|dir|
-      Pathname("a/b/c/d").mkpath
+      path = Pathname("a/b/c/d")
+      assert_equal(path, path.mkpath)
       assert_file.directory?("a/b/c/d")
     }
   end
@@ -1398,7 +1399,8 @@ def test_rmtree
     with_tmpchdir('rubytest-pathname') {|dir|
       Pathname("a/b/c/d").mkpath
       assert_file.exist?("a/b/c/d")
-      Pathname("a").rmtree
+      path = Pathname("a")
+      assert_equal(path, path.rmtree)
       assert_file.not_exist?("a")
     }
   end
```

Github PR: https://github.com/ruby/ruby/pull/3705. If accepted I will make a pr to update the tests here as well https://github.com/ruby/rbs/blob/b0dee64fdd00cc41c0729fa2c239fc2dcb9c3b18/test/stdlib/Pathname_test.rb#L456-L463.



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

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

* [ruby-core:100676] [Ruby master Feature#17294] Feature: Allow method chaining with Pathname#mkpath Pathname#rmtree
  2020-10-30 15:04 [ruby-core:100668] [Ruby master Feature#17294] Feature: Allow method chaining with Pathname#mkpath Pathname#rmtree get.codetriage
@ 2020-10-31  0:56 ` brooke
  2021-08-30  6:52 ` [ruby-core:105089] " hsbt (Hiroshi SHIBATA)
  1 sibling, 0 replies; 3+ messages in thread
From: brooke @ 2020-10-31  0:56 UTC (permalink / raw
  To: ruby-core

Issue #17294 has been updated by bkuhlmann (Brooke Kuhlmann).


I would like this too. I've been using [Refinements](https://www.alchemists.io/projects/refinements/#_pathname) to improve Pathname behavior but this would be better.

----------------------------------------
Feature #17294: Feature: Allow method chaining with Pathname#mkpath Pathname#rmtree
https://bugs.ruby-lang.org/issues/17294#change-88317

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

Currently in my code when I want to create a pathname object and create a path at the same time I must use tap

```
path = Pathname.new("/tmp/new").tap(&:mkpath)
```

I think it would be cleaner to be able to chain on the results of these methods instead:

```
path = Pathname.new("/tmp/new").mkpath
```

This is a change in return value but after research on github I do not believe many (if any) are relying on the current behavior to return nil https://github.com/search?l=&p=1&q=.mkpath+language%3ARuby&ref=advsearch&type=Code.

Here is my diff: 

```
$ git diff master schneems/return-self-pathname
diff --git a/ext/pathname/lib/pathname.rb b/ext/pathname/lib/pathname.rb
index e6fb90277d..f1eb1e00ae 100644
--- a/ext/pathname/lib/pathname.rb
+++ b/ext/pathname/lib/pathname.rb
@@ -582,7 +582,7 @@ class Pathname    # * FileUtils *
   def mkpath
     require 'fileutils'
     FileUtils.mkpath(@path)
-    nil
+    self
   end

   # Recursively deletes a directory, including all directories beneath it.
@@ -593,7 +593,7 @@ def rmtree
     # File::Path provides "mkpath" and "rmtree".
     require 'fileutils'
     FileUtils.rm_r(@path)
-    nil
+    self
   end
 end

diff --git a/test/pathname/test_pathname.rb b/test/pathname/test_pathname.rb
index 43cef4849f..149fe15c3a 100644
--- a/test/pathname/test_pathname.rb
+++ b/test/pathname/test_pathname.rb
@@ -1389,7 +1389,8 @@ def test_find

   def test_mkpath
     with_tmpchdir('rubytest-pathname') {|dir|
-      Pathname("a/b/c/d").mkpath
+      path = Pathname("a/b/c/d")
+      assert_equal(path, path.mkpath)
       assert_file.directory?("a/b/c/d")
     }
   end
@@ -1398,7 +1399,8 @@ def test_rmtree
     with_tmpchdir('rubytest-pathname') {|dir|
       Pathname("a/b/c/d").mkpath
       assert_file.exist?("a/b/c/d")
-      Pathname("a").rmtree
+      path = Pathname("a")
+      assert_equal(path, path.rmtree)
       assert_file.not_exist?("a")
     }
   end
```

Github PR: https://github.com/ruby/ruby/pull/3705. If accepted I will make a pr to update the tests here as well https://github.com/ruby/rbs/blob/b0dee64fdd00cc41c0729fa2c239fc2dcb9c3b18/test/stdlib/Pathname_test.rb#L456-L463.



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

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

* [ruby-core:105089] [Ruby master Feature#17294] Feature: Allow method chaining with Pathname#mkpath Pathname#rmtree
  2020-10-30 15:04 [ruby-core:100668] [Ruby master Feature#17294] Feature: Allow method chaining with Pathname#mkpath Pathname#rmtree get.codetriage
  2020-10-31  0:56 ` [ruby-core:100676] " brooke
@ 2021-08-30  6:52 ` hsbt (Hiroshi SHIBATA)
  1 sibling, 0 replies; 3+ messages in thread
From: hsbt (Hiroshi SHIBATA) @ 2021-08-30  6:52 UTC (permalink / raw
  To: ruby-core

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

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

----------------------------------------
Feature #17294: Feature: Allow method chaining with Pathname#mkpath Pathname#rmtree
https://bugs.ruby-lang.org/issues/17294#change-93498

* Author: schneems (Richard Schneeman)
* Status: Assigned
* Priority: Normal
* Assignee: akr (Akira Tanaka)
----------------------------------------

Currently in my code when I want to create a pathname object and create a path at the same time I must use tap

```
path = Pathname.new("/tmp/new").tap(&:mkpath)
```

I think it would be cleaner to be able to chain on the results of these methods instead:

```
path = Pathname.new("/tmp/new").mkpath
```

This is a change in return value but after research on github I do not believe many (if any) are relying on the current behavior to return nil https://github.com/search?l=&p=1&q=.mkpath+language%3ARuby&ref=advsearch&type=Code.

Here is my diff: 

```
$ git diff master schneems/return-self-pathname
diff --git a/ext/pathname/lib/pathname.rb b/ext/pathname/lib/pathname.rb
index e6fb90277d..f1eb1e00ae 100644
--- a/ext/pathname/lib/pathname.rb
+++ b/ext/pathname/lib/pathname.rb
@@ -582,7 +582,7 @@ class Pathname    # * FileUtils *
   def mkpath
     require 'fileutils'
     FileUtils.mkpath(@path)
-    nil
+    self
   end

   # Recursively deletes a directory, including all directories beneath it.
@@ -593,7 +593,7 @@ def rmtree
     # File::Path provides "mkpath" and "rmtree".
     require 'fileutils'
     FileUtils.rm_r(@path)
-    nil
+    self
   end
 end

diff --git a/test/pathname/test_pathname.rb b/test/pathname/test_pathname.rb
index 43cef4849f..149fe15c3a 100644
--- a/test/pathname/test_pathname.rb
+++ b/test/pathname/test_pathname.rb
@@ -1389,7 +1389,8 @@ def test_find

   def test_mkpath
     with_tmpchdir('rubytest-pathname') {|dir|
-      Pathname("a/b/c/d").mkpath
+      path = Pathname("a/b/c/d")
+      assert_equal(path, path.mkpath)
       assert_file.directory?("a/b/c/d")
     }
   end
@@ -1398,7 +1399,8 @@ def test_rmtree
     with_tmpchdir('rubytest-pathname') {|dir|
       Pathname("a/b/c/d").mkpath
       assert_file.exist?("a/b/c/d")
-      Pathname("a").rmtree
+      path = Pathname("a")
+      assert_equal(path, path.rmtree)
       assert_file.not_exist?("a")
     }
   end
```

Github PR: https://github.com/ruby/ruby/pull/3705. If accepted I will make a pr to update the tests here as well https://github.com/ruby/rbs/blob/b0dee64fdd00cc41c0729fa2c239fc2dcb9c3b18/test/stdlib/Pathname_test.rb#L456-L463.



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

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

end of thread, other threads:[~2021-08-30  6:52 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-10-30 15:04 [ruby-core:100668] [Ruby master Feature#17294] Feature: Allow method chaining with Pathname#mkpath Pathname#rmtree get.codetriage
2020-10-31  0:56 ` [ruby-core:100676] " brooke
2021-08-30  6:52 ` [ruby-core:105089] " hsbt (Hiroshi SHIBATA)

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