A directory entry is an item returned for a directory, such as a file, link, subdirectory, or special name. Ruby exposes entries through the Dir class or through directory globbing. section 12.4.1
Ruby directory operations distinguish entries in several useful ways:
- Dir#entries includes hidden names and the special "." and ".." entries; filter names beginning with a dot when they are unwanted. section 12.4.1
- Directory globbing supports wildcards and recursive matching, but normally omits hidden entries unless dot matching is enabled. section 12.4.1
- Convert names into full pathnames with Dir#path and File.join, then use File.file? to retain regular files. section 12.4.1
Summing regular-file sizes
Create a Dir object, remove dot-prefixed entries, join each remaining name to the directory path, discard non-files, and inject File.size values into a total. section 12.4.1
d = Dir.new("/Users/dblack/Devel/ruby-4.0.1/lib/net")
entries = d.entries 1
entries.delete_if {|entry| entry =~ /^\./ } 2
entries.map! {|entry| File.join(d.path, entry) } 3
entries.delete_if {|entry| !File.file?(entry) } 4
print "Total bytes: "
puts entries.inject(0) {|total, entry|total + File.size(entry) } 5
A runnable sequence shows directory entries being filtered, converted to full paths, tested as regular files, and totaled. section 12.4.1
