🚀 See the 2024 Ruby on Rails Community Survey results!
Article  |  Development

Behind the Ruby Gems Curtain

Reading time: Less than a minute

Behind the Ruby Gems Curtain

I recently ran into an issue where I had to crack open the Bundler gem in order to get a client project or ours running on my new MacBook.

The error that I was getting was a bit strange: Bundler::GemRequireError: There was an error while trying to load the gem 'ts-delayed-delta'.

I looked at the Bundler source to see where that exception was being raised and it looks like they are rescuing any exception and then re-raise a Bundler::GemRequireError:

        begin
          Kernel.require file
        rescue => e
          raise e if e.is_a?(LoadError) # we handle this a little later
          raise Bundler::GemRequireError.new e,
            "There was an error while trying to load the gem '#{file}'."
        end

Unfortunately, this has the side effect of swallowing exceptions and showing the most unhelpful one first. Merely commenting out the line raise Bundler::GemRequireError.new e allowed me to see the real error – there was a missing YAML configuration file.

So how does one go about cracking open a gem and making changes?

There are actually a few ways that you can do this.

The old fashioned way:

Find the location of the gem and open it in your favorite editor. Since I'm using rbenv, I opened ~/.rbenv/versions/1.9.3-p547/lib/ruby/gems/1.9.1/gems/bundler-1.11.2 in vim (my text editor of choice) and changed the file.

If you want to open a bundled gem and you vendor your gems for a Rails project simply look in the vendor/ruby directory for the gem.

The not so old fashioned way:

Install https://github.com/fnando/gem-open the GemOpen gem and run the command gem open .

The new fashioned way:

Use bundler itself to open the gem by running the command bundle open .

Don't be afraid to dig into other people's code to help troubleshoot an issue that you're having. You'll definitely learn a lot from the process.

Have a project that needs help?