Create Break Points in Rails with Pry
Make debugging a breeze.
- Add
pry
to your GemFile - Run
bundle install
- Put
binding.pry
where you want to create breakpoint:
When the server gets to this point, it will yield to the terminal that is running rails s
, here you can check values of variables and other things to troubleshoot what is happening. Enter exit
when finished to return back to the server.
Advanced.
If you need to put binding.pry
inside of a loop and don't want it to be called several times. Then outside of the loop put:
do_bind = true
Then where you want to bind put:
if do_bind
do_bind = false
binding.pry
end
Written by JD Isaacks
Related protips
3 Responses
@icoolninja You are correct, that is something I already do, but it is good to mention it here.
@jisaacks I just wrote another protip (similar to yours) for passenger standalone. Check this out https://coderwall.com/p/rtskuw
I found it somewhat weird at first to work with binding.pry lines in my code for debugging purposes. Other than that it's a great thing to have. Way better than figuring stuff out by removing lines of code ;-)
One question remains. Is there another way to introduce breakpoints, say directly from the pry console?