Ruby Directory Glob Recursive on Absolute Path

I'm trying to get the Dir.glob of an absolute path: This is my directory structure: When I run on the directory I only get the files in the root of the directory: But no entries for the app subdirectory.

I'm trying to get the Dir.glob of an absolute path:

path = "/Users/ken/templates" scaffold = Dir.glob("#{path}/*.erb", File::FNM_DOTMATCH) p scaffold.inspect 

This is my directory structure:

/Users/ken/templates/test.erb /Users/ken/templates/test2.erb /Users/ken/templates/app/one.erb /Users/ken/templates/app/two.erb 

When I run on the directory I only get the files in the root of the directory:

"[\"/Users/ken/farmstead/test.erb\", \"/Users/ken/farmstead/test2.erb\"]" 

But no entries for the app subdirectory.

How do I get the Glob to go recursive on the absolute path?

2 Answers

To get into subdirectories, one needs to instruct Dir#glob to get into subdirectories:

# ⇓⇓⇓ scaffold = Dir.glob("#{path}/**/*.erb", File::FNM_DOTMATCH) 

Quote from the documentation I linked:

**
    Matches directories recursively.

2

A cleaner way to tell glob about the absolute directory is to use the base parameter like so:

Dir.glob("**/*.erb", base: path) 

Source: https://ruby-doc.org/3.2.0/Dir.html#method-c-glob

ncG1vNJzZmirpJawrLvVnqmfpJ%2Bse6S7zGiorp2jqbawutJoa3FoYWx%2Fdn2Oq6ybsV2ZtrOxwq2mq7FdnLmwroyrnJytoqi2t7GMqKVmmZKovK3B055kqZmknQ%3D%3D

 Share!