Rubygems integration refactor

Patch for https://github.com/bundler/bundler/issues/2824