Since there may be a lot of Ghost Methods inside a ruby gem, I don\'t think it is a good idea to study the inner mechanism of a ruby gem just by reading
If you are using visual studio code, and you have normal debugging enabled for rails you can just open the gem file you want to debug in VS Code, example:
C:\Ruby26-x64\lib\ruby\gems\2.6.0\gems\devise-4.7.3\app\controllers\devise\registrations_controller.rb
and set a breakpoint with VSCode. It will stop execution on that line.
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>
I would love to know if there's a better way to do this, but how I usually do it is:
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
mvim /path/to/gem/directory
debugger
above the line in question.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
In languages that change the code at runtime, like Ruby, it is hard to accurately predict 100% the origins of symbols, methods etc.
I deal with a lot of third party gems that need source code analysis and I found that the best tool for this task is Netbeans + its Ruby and Rails plugin.
** it has some glitches with method calls in yielded code blocks (like each {}
) but I learned to deal with those.
What I usually do is set breakpoints and analyse the code at runtime.
Both commands output a path to the gem source in your computer.
# if it is present in your gemfile
bundle show gemname
# if not in your gemfile
gem which gemname
Then just open the folder in your text editor of choice, and good debugging!