liveBook · Manning

Directory entry

As explained in The Well-Grounded Rubyist, Fourth Edition

A directory entry is an item listed within a directory, such as a file, link, or subdirectory.

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

How The Well-Grounded Rubyist, Fourth Edition covers it

How deep it goes
Explains retrieval, hidden-entry differences, path construction, file filtering, and aggregation without exhaustive directory API coverage.
Examples
Demonstrates listings, hidden-name matching, wildcard globbing, and totaling regular-file sizes.
What you can do after
Operate Ruby directory listings by filtering entries, constructing paths, selecting regular files, and calculating their combined size.
How it connects
Connects directory entries to the Dir class, directory globbing, File.join, File.file?, and File.size.

Related concepts

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.