From 3c54b8e9205fa8debe09447138fd08aeaa59e69c Mon Sep 17 00:00:00 2001 From: schneems Date: Mon, 26 Oct 2020 13:46:40 -0500 Subject: [PATCH] Allow method chaining with Pathname#mkpath 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 ``` --- ext/pathname/lib/pathname.rb | 5 ++--- test/pathname/test_pathname.rb | 6 ++++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/ext/pathname/lib/pathname.rb b/ext/pathname/lib/pathname.rb index 52e1d5cfbb..2deda00ff2 100644 --- a/ext/pathname/lib/pathname.rb +++ b/ext/pathname/lib/pathname.rb @@ -588,7 +588,7 @@ class Pathname # * FileUtils * def mkpath(mode: nil) require 'fileutils' FileUtils.mkpath(@path, mode: mode) - nil + self end # Recursively deletes a directory, including all directories beneath it. @@ -599,7 +599,7 @@ class Pathname # * FileUtils * # File::Path provides "mkpath" and "rmtree". require 'fileutils' FileUtils.rm_rf(@path, noop: noop, verbose: verbose, secure: secure) - nil + self end end @@ -619,4 +619,3 @@ class Pathname # * tmpdir * end end end - diff --git a/test/pathname/test_pathname.rb b/test/pathname/test_pathname.rb index edd96e2724..6a4bb784bd 100644 --- a/test/pathname/test_pathname.rb +++ b/test/pathname/test_pathname.rb @@ -1475,7 +1475,8 @@ class TestPathname < Test::Unit::TestCase 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") unless File.stat(dir).world_readable? # mktmpdir should make unreadable @@ -1491,7 +1492,8 @@ class TestPathname < Test::Unit::TestCase 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