Skip to content

Commit 45cea18

Browse files
hsbtclaude
andcommitted
Implement Pathname#rmtree in builtin Pathname
Move rmtree from lib/pathname.rb (which delegated to FileUtils.rm_rf) into pathname_builtin.rb with a pure Ruby implementation using File.lstat, Dir.each_child, Dir.rmdir, and File.unlink. The noop, verbose, and secure keyword arguments are intentionally dropped to remove the fileutils dependency. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 1b8ebb5 commit 45cea18

2 files changed

Lines changed: 30 additions & 15 deletions

File tree

lib/pathname.rb

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,6 @@
1010
# For documentation, see class Pathname.
1111
#
1212

13-
class Pathname # * FileUtils *
14-
# Recursively deletes a directory, including all directories beneath it.
15-
#
16-
# Note that you need to require 'pathname' to use this method.
17-
#
18-
# See FileUtils.rm_rf
19-
def rmtree(noop: nil, verbose: nil, secure: nil)
20-
# The name "rmtree" is borrowed from File::Path of Perl.
21-
# File::Path provides "mkpath" and "rmtree".
22-
require 'fileutils'
23-
FileUtils.rm_rf(@path, noop: noop, verbose: verbose, secure: secure)
24-
self
25-
end
26-
end
27-
2813
class Pathname # * tmpdir *
2914
# call-seq:
3015
# Pathname.mktmpdir -> new_pathname

pathname_builtin.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1383,6 +1383,36 @@ 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
1399+
remove_entry(@path)
1400+
self
1401+
end
1402+
1403+
private
1404+
1405+
def remove_entry(path)
1406+
st = File.lstat(path)
1407+
if st.directory?
1408+
Dir.each_child(path) do |child|
1409+
remove_entry(File.join(path, child))
1410+
end
1411+
Dir.rmdir(path)
1412+
else
1413+
File.unlink(path)
1414+
end
1415+
end
13861416
end
13871417

13881418
class Pathname

0 commit comments

Comments
 (0)