Why it is better to be specific when creating a gemspec file

I authored a few ruby gems mostly to prevent myself from having to do things manually.

I hate doing anything twice and love automating tasks when possible.

Based on the experience, I found that when writing the gemspec, I have two choices to do it.

The first is to explicitly state the files required with Dir.glob() method

 spec.files = Dir.glob("{bin,lib}/**/*") + %w[Gemfile
                                              Rakefile
                                              index_html.gemspec
                                              README.md
                                              CHANGELOG.md
                                              LICENSE
                                              .rubocop.yml
                                              .gitignore]

The second way is to use the result from Git to list what should be included:

  # ...
  # require that Git to be available
  spec.files = `git ls-files -z`.split("\x0")
  # ...

Reason why the first is better than the second are:

The only caveat about this is you will need to edit this file everytime you or someone add new file that is not inside the lib or bin directory.

For me this is ok as I like to know about what’s going on with the gem anyway.

Happy Hacking

comments powered by Disqus