Dir.glob returns pathnames matching a glob pattern. Its index-style spelling, Dir[pattern], is equivalent to calling Dir.glob(pattern). section 12.4.1
Match header files
Dir["/Users/dblack/Devel/ruby-4.0.1/include/ruby/r*.h"] returns the matching header pathnames, such as ractor.h, random.h, and ruby.h. section 12.4.1
Use directory globbing when you need:
- Wildcard matching, such as Dir["*info*"], rather than every directory entry. section 12.4.1
- Recursive matching in subdirectories. section 12.4.1
- A compact replacement for collecting entries, joining full paths, filtering regular files, and summing their sizes. section 12.4.1
Sum matching regular files
dir = "/Users/dblack/Devel/ruby-4.0.1/lib/net" entries = Dir["#{dir}/*"].select { |entry| File.file?(entry) } puts entries.inject(0) { |total, entry| total + File.size(entry) } section 12.4.1
Unlike Dir.entries, globbing omits hidden entries by default, including names beginning with a period on UNIX-like systems. A matching flag can include them: my_directory.glob("*info*", File::FNM_DOTMATCH). section 12.4.1
