Jean-Philippe Doyle (j15e)

Developer, homebrewer & more

Using File#fnmatch? to mimick Dir#glob

22 Jun 2013

Someone opened an issue on sinatra-assetpack recently regarding unexpected behaviour of our Dir#glob-like feature to match source files in a given assets package.

I found out that File#fnmatch? method we use to reproduce a glob-like matching on assets soruces is not behaving exactly the same way a Dir#glob would under certain conditions.

There was a very tiny hint in the documentation :

The pattern is not a regular expression; instead it follows rules similar to shell filename globbing.

Specifically in this case, fnmatch? will not match starting / or . with subfolders /**/ matching.

But you can specify an option flag to enable matching of those cases (likely what you expect in most cases) :

File.fnmatch?(expected, filename, File::FNM_PATHNAME | File::FNM_DOTMATCH)