liveBook · Manning

Hidden file

As explained in The Well-Grounded Rubyist, Fourth Edition

A hidden file is a filesystem file whose name is conventionally excluded from ordinary directory listings, commonly because it begins with a period.

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

How The Well-Grounded Rubyist, Fourth Edition covers it

How deep it goes
Explains naming convention, listing differences, explicit filtering, glob flags, path construction, and regular-file checks.
Examples
Uses Dir.entries, dot-prefix filtering, File::FNM_DOTMATCH, and a byte-totaling pipeline for non-hidden regular files.
What you can do after
Apply Ruby's Dir and File APIs to include or exclude hidden entries during directory processing.
How it connects
Links hidden files with directory entries, directory globbing, regular files, and filesystem paths.

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Rubyist, Fourth Edition. The section links point to the parts of the book it draws on.