A hidden file is a filesystem entry whose name begins with a period. On UNIX-like systems, ordinary directory globbing excludes these dot files, while directory-entry methods return them. section 12.4.1
Ruby directory operations treat hidden entries differently:
- Dir.entries includes hidden names, along with the special . and .. entries; filter names beginning with a dot when they should be excluded. section 12.4.1
- Directory globbing omits hidden entries by default but can include them with File::FNM_DOTMATCH. section 12.4.1
- After filtering, combine entries with Dir#path and File.join, then use File.file? to distinguish regular files from directories. section 12.4.1
Exclude hidden files before summing sizes
entries = d.entries entries.delete_if { |entry| entry =~ /^\./ } entries.map! { |entry| File.join(d.path, entry) } entries.delete_if { |entry| !File.file?(entry) } entries.inject(0) { |total, entry| total + File.size(entry) } 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
Inspect the complete filtering pipeline for removing dot entries, constructing full paths, selecting regular files, and totaling their sizes. section 12.4.1
