Skip to content

Commit 5d4fff6

Browse files
hsbtclaude
andcommitted
Implement Pathname#rmtree in builtin Pathname
Copy the deletion logic from FileUtils.remove_entry (lib/fileutils.rb) into pathname_builtin.rb to remove the runtime dependency on the fileutils library. The remove_entry helper uses the same force-based StandardError suppression as FileUtils.rm_rf. The keyword arguments noop, verbose, and secure are kept for signature compatibility. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 92e95d8 commit 5d4fff6

2 files changed

Lines changed: 32 additions & 15 deletions

File tree

lib/pathname.rb

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,6 @@
99
#
1010
# For documentation, see class Pathname.
1111
#
12-
class Pathname # * FileUtils *
13-
# Recursively deletes a directory, including all directories beneath it.
14-
#
15-
# Note that you need to require 'pathname' to use this method.
16-
#
17-
# See FileUtils.rm_rf
18-
def rmtree(noop: nil, verbose: nil, secure: nil)
19-
# The name "rmtree" is borrowed from File::Path of Perl.
20-
# File::Path provides "mkpath" and "rmtree".
21-
require 'fileutils'
22-
FileUtils.rm_rf(@path, noop: noop, verbose: verbose, secure: secure)
23-
self
24-
end
25-
end
26-
2712
class Pathname # * tmpdir *
2813
# call-seq:
2914
# Pathname.mktmpdir -> new_pathname

pathname_builtin.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1383,6 +1383,38 @@ def unlink()
13831383
File.unlink @path
13841384
end
13851385
alias delete unlink
1386+
1387+
# Recursively deletes a directory, including all directories beneath it.
1388+
#
1389+
# The name "rmtree" is borrowed from File::Path of Perl.
1390+
# File::Path provides "mkpath" and "rmtree".
1391+
#
1392+
# The previous implementation delegated to FileUtils.rm_rf and accepted
1393+
# +noop+, +verbose+, and +secure+ keyword arguments. This builtin
1394+
# implementation intentionally drops them to remove the fileutils dependency.
1395+
#
1396+
# Pathname("/tmp/testdir").rmtree
1397+
#
1398+
def rmtree(noop: nil, verbose: nil, secure: nil)
1399+
remove_entry(@path)
1400+
self
1401+
end
1402+
1403+
private
1404+
1405+
def remove_entry(path, force = true) # :nodoc:
1406+
st = File.lstat(path)
1407+
if st.directory?
1408+
Dir.each_child(path) do |child|
1409+
remove_entry(File.join(path, child), force)
1410+
end
1411+
Dir.rmdir(path)
1412+
else
1413+
File.unlink(path)
1414+
end
1415+
rescue StandardError
1416+
raise unless force
1417+
end
13861418
end
13871419

13881420
class Pathname

0 commit comments

Comments
 (0)