Recently I was fiddling with a gem and noticed that I was including some extra files when building it. That was easy enough to clean up using the Gem::Specification#files attribute, but poking around there made me wonder how other folks were specifying their file lists. There are a bunch of different ways to do it, and I bet it's the sort of thing that gets cargo culted from gem to gem; I know I've done that.
Here's a common usage pattern:
git ls-files ensures you only get files that are committed to the repository, and
-z uses a null byte line terminator rather than a newline. I'm not sure, but I think the idea behind using a null terminator as a delimiter is that that value is never going to be part of a filename (confirmed by tkannelid, thanks!). After the file list is generated it's just a matter of splitting the list and rejecting any filenames that don't match a regular expression.
Here's another one, this time from rack-mini-profiler's gemspec:
This starts with a "seed" array and concatenates any files in
lib, then removes any directories or vim temporary files. The glob could be rewritten a little more concisely as
Here's a variant on the
git ls-files method. It doesn't use
-z and relies on Ruby's
String#split default behavior, which is to split on any whitespace. Note that
ls-files accepts both individual files and directories as arguments:
The downside there is that it would break if any of the filenames contained spaces.
Here's another variant. The interesting part of this one is that the regular expression in the
select block uses
?: which is a non-capturing group:
I'm not sure why it does that. Benchmarking shows non-capturing groups giving a minor improvement over a zillion iterations:
But in this context it seems unnecessary since the array is only going to have a few dozen files. And I would think there'd be a tiny performance hit up front for Ruby to compute the NFA for the more complicated expression. Still, from a readability perspective, it does signal that the group isn't going to be referenced later on, so maybe that's a win.
No real takeaways on this one, just messing around. Always neat to see how different projects do things. Enjoy!