Best way to debug third-party gems in ruby
I would love to know if there's a better way to do this, but how I usually do it is:
- Add the ruby-debug gem to your Gemfile (or ruby-debug19 if you're on Ruby 1.9.2)
- Find the Gem by doing
bundle show gemname
. I'm on a Mac so I usually pipe this to pbcopy so it gets copied to my clipboard.bundle show rails | pbcopy
- Open the gem directory in your favorite editor.
mvim /path/to/gem/directory
- Navigate to the file and line where you want to put the breakpoint* and insert
debugger
above the line in question. - Reload page, run test, or do whatever you would to get the Gem file to execute
- When execution stops at debugger, you can inspect variables (
p variable_name
), and move line by line with the ruby debugger commands.
*Knowing where to put the breakpoint can take some understanding of the code, but you should start in lib/gemname.rb
I would avoid editing the Gem files as suggested in the currently accepted answer. Instead, put the debugger
command in one of your app files and use the break
command to set a breakpoint in the gem. I'm using rvm
with a gemset
so here is how I do it:
break /Users/chris/.rvm/gems/ruby-1.9.3-p125@<gemset>/gems/<gem_name>-<gem-version>/<path_to_file>:<line_number>