mirror of
https://github.com/ruby/ruby.git
synced 2025-08-15 21:49:06 +02:00
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 ```
This commit is contained in:
parent
08346e7267
commit
3c54b8e920
Notes:
git
2024-10-04 03:21:46 +00:00
2 changed files with 6 additions and 5 deletions
|
@ -588,7 +588,7 @@ class Pathname # * FileUtils *
|
||||||
def mkpath(mode: nil)
|
def mkpath(mode: nil)
|
||||||
require 'fileutils'
|
require 'fileutils'
|
||||||
FileUtils.mkpath(@path, mode: mode)
|
FileUtils.mkpath(@path, mode: mode)
|
||||||
nil
|
self
|
||||||
end
|
end
|
||||||
|
|
||||||
# Recursively deletes a directory, including all directories beneath it.
|
# Recursively deletes a directory, including all directories beneath it.
|
||||||
|
@ -599,7 +599,7 @@ class Pathname # * FileUtils *
|
||||||
# File::Path provides "mkpath" and "rmtree".
|
# File::Path provides "mkpath" and "rmtree".
|
||||||
require 'fileutils'
|
require 'fileutils'
|
||||||
FileUtils.rm_rf(@path, noop: noop, verbose: verbose, secure: secure)
|
FileUtils.rm_rf(@path, noop: noop, verbose: verbose, secure: secure)
|
||||||
nil
|
self
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -619,4 +619,3 @@ class Pathname # * tmpdir *
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1475,7 +1475,8 @@ class TestPathname < Test::Unit::TestCase
|
||||||
|
|
||||||
def test_mkpath
|
def test_mkpath
|
||||||
with_tmpchdir('rubytest-pathname') {|dir|
|
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")
|
assert_file.directory?("a/b/c/d")
|
||||||
unless File.stat(dir).world_readable?
|
unless File.stat(dir).world_readable?
|
||||||
# mktmpdir should make unreadable
|
# mktmpdir should make unreadable
|
||||||
|
@ -1491,7 +1492,8 @@ class TestPathname < Test::Unit::TestCase
|
||||||
with_tmpchdir('rubytest-pathname') {|dir|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
||||||
Pathname("a/b/c/d").mkpath
|
Pathname("a/b/c/d").mkpath
|
||||||
assert_file.exist?("a/b/c/d")
|
assert_file.exist?("a/b/c/d")
|
||||||
Pathname("a").rmtree
|
path = Pathname("a")
|
||||||
|
assert_equal(path, path.rmtree)
|
||||||
assert_file.not_exist?("a")
|
assert_file.not_exist?("a")
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue