Debugging Ruby and Rails on Pry
Railscasts has a very good screencast regarding http://railscasts.com/episodes/280-pry-with-rails. The Pry screencast from Joshua Cheek is also very useful as an introduction to Pry, going inside a class, inspecting it, showing instance variables, etc.
- In your Gemfile, you can add the pry gems in the development group
group :development, :test do . . . gem 'better_errors' gem 'binding_of_caller' gem 'meta_request' # Pry gems related to debugging. Move this outside the development group # in case production debugging is necessary. Note, pry-debugger works # in production and conflicts locally with RubyMine, use pry-nav + ~/.pryrc instead. # Unfortunately pry-nav does not work in production, so you are left with # pry-debugger. # # Locally where RubyMine exists: # group :development, :test do # gem 'pry' # gem 'pry-remote' # gem 'pry-nav' # end # # Production Debugging: See config/environments/production.rb # gem 'pry' # gem 'pry-remote' # gem 'pry-debugger' gem 'pry' gem 'pry-remote' gem 'pry-nav' end |
- Create a ~/.pryrc
Pry.config.correct_indent = false Pry.config.auto_indent = false Pry.commands.alias_command 'c', 'continue' Pry.commands.alias_command 's', 'step' Pry.commands.alias_command 'n', 'next' Pry.commands.alias_command 'f', 'finish' # Only works in pry-debugger gem |
- To debug, add binding.pry. Execution will stop where you specify binding.pry
5: def index 6: poller 7: binding.pry 8: 9: if current_user.admin? 10: @notices = Notice.updated_descending 11: else 12: @notices = current_user.client.notices.approved.updated_descending 13: end 14: 15: end |
-
To navigate, you can use the navigation aliases on step 2
-
To use pry instead of the rails console
pry -r ./config/environment |
- To remotely debug a rails application you can use
pry-remote
. Instead of usingbinding.pry
we usebinding.remote_pry
. From there, you can use pry normally and navigate using our navigation aliases (step 2). See also http://blog.bignerdranch.com/1629-debugging-remote-processes-with-pry-remote/
On the server:
5: def index 6: poller 7: binding.remote_pry 8: 9: if current_user.admin? 10: @notices = Notice.updated_descending 11: else 12: @notices = current_user.client.notices.approved.updated_descending 13: end 14: 15: end |
On the client:
# pry-remote -h 127.0.0.1 -p 9876 5: def index 6: poller =>7: binding.remote_pry 8: 9: if current_user.admin? 10: @notices = Notice.updated_descending 11: else 12: @notices = current_user.client.notices.approved.updated_descending 13: end 14: 15: end |
Note: If you experience issues with pry unable to exit or continue. You might need to specify the proper Pry.config
settings. Since, we have a rails app + apache passenger
and having .pryrc
doesn’t seem to work, I specified the Pry.config settings in my config/environments/production.rb
and moved the gems out of development (irk!). From there, it works as expected.
MyApp::Application.configure do . . . # Settings for PRY debugging in production. Yes, it is enabled, use with caution # as you really need to know what you are doing. If you are uncommenting the lines below # then you should have a very good reason why. If you are doing this in production too often, # then you are not writing more tests! When you are finished debugging, please # do comment the lines below. Pry.config.correct_indent = false Pry.config.auto_indent = false Pry.config.history.file = "#{Rails.root}/log/pry_history.log" Pry.commands.alias_command 'c', 'continue' Pry.commands.alias_command 's', 'step' Pry.commands.alias_command 'n', 'next' Pry.commands.alias_command 'f', 'finish' end |
RuntimeError - Debugger.start is not called yet.: debugger (1.6.1) lib/ruby-debug/processor.rb:144:in `at_breakpoint' (eval):5:in `block in at_breakpoint' <internal:prelude>:10:in `synchronize' (eval):3:in `at_breakpoint' debugger (1.6.1) lib/ruby-debug-base.rb:41:in `at_breakpoint' |
Update 1: pry-debugger
has a gem dependency with debugger
which conflicts with RubyMine’s debugging gems. See step 1 which now has updated gems. To uninstall pry-debugger and debugger, simply do:
# gem uninstall pry-debugger # gem uninstall debugger |
References:
http://railscasts.com/episodes/280-pry-with-rails
Pry screencast from Joshua Cheek
https://github.com/pry/pry/wiki/Remote-sessions
https://github.com/pry/pry/wiki