jemminger jemminger - 1 year ago 42
Ruby Question

conflicting ruby gems

I need to use two gems in my project that both claim the PDF namespace: pdf-reader and htmldoc.

Is there any way to get them to play nice together? The only way I can think of is to rewrite my own version of htmldoc to give it a different namespace.

Answer Source

There's probably no elegant solution to the problem. If you really need the two gems working side by side I think your best option is to fork one of them (or possibly both) and use your fork. This is how I'd go about it:

  • If either gem is hosted on Github then fork it, or if both are on Github fork the one that seems to be the least work.
  • If neither gem is on Github, see if you can get hold of the source (grabbing it from the gem is a possibility, but finding the real repository might be helpful as there may be other files there that are not included in the gem), and put it in a repository on Github. Make sure the gem's license allows this (which it almost certainly does if it's one of the common open source licenses).
  • Make your changes.
  • Make sure there is a .gemspec file in the root of the repository, the next step will not work otherwise.
  • Use Bundler to manage your projects dependencies. Instead of specifying the dependency to the library you've modified as

    gem 'the_gem'

    specify it like this:

    gem 'the_gem', :git => 'git://'

    (but change the URL to the repository to the actual one)

  • Send an e-mail to the maintainer of the gem you modified and ask him or her to consider merging in your changes in the next release.

Bundler makes it really easy to use alternative versions of gems with minimal hassle. I often fork gems, fix bugs or add features, change my Gemfile to point to my version, then ask the maintainer to merge in my changes. When, or if, that happens I simply change my Gemfile back to just refer to the official version of the gem.

An alternative strategy, if the maintainer does not want to merge in your changes and you want to distribute your version to others is to push up your version to Rubygems as a new gem, but in that case prefix the gem name with your name, or some other string that identifies your gem as a variant.