How to get to the source code of a Ruby gem

Often enough I’ve followed the instructions to installing a gem in my rails application, just to see it not work. Eventually, I would have to uninstall the gem since it wasn’t working for me, and would have to either settle for a not-as-good other gem, or reinvent the wheel and write a solution to the problem again.

What I didn’t know was that you could reveal the gem’s source code in your text editor of choice. You’re actually able to do some debugging to--hopefully--get that gem working with your app.

All you have to do is go to your project’s directory in your terminal, and

  1. Set up your editor (using vim as my editor as an example): export BUNDLER_EDITOR=vim
  2. Reveal the gem’s source (using jquery-rails gem as an example): bundle open jquery-rails

And now you can not only view the directory and file composition of the gem, but you can also debug it by placing a binding.pry within it—this will stop the execution of your application at that line, and you can interact with your application at that point in the terminal where your rails server is running.