TL;DR: Pry is really cool and is very powerful with a ton of built in shortcuts for effective debugging and working with your code.
So, let’s start with what is Pry? Pry catches exceptions. It is a REPL which means read, evaluate, print, loop. It is more robust, fun and easy to use, and adds introspection to the REPL. Introspection basically means that you are able to look around and see what your code is doing. This is what I’ve used pry for in the past. You can put binding.pry into your code and run it in your terminal and your terminal will automatically open pry instead of IRB and will allow you to both step through your code or look at each step in your code individually allowing you to see what’s going on and help determine and solve problems more effectively.
Conrad talked about recommending REPL driven development. So, instead of working out the code and testing it, he suggests you work out the code you’re going to use in a REPL first and then write the tests and the code it.
Now, for the cool tips and tricks he mentioned (note: I haven’t had a chance to try these all out since I’ve been back)…
?$mongo
shows the documentation for a method$$mongo
shows the source codels Moped::Query
shows the methods availableFor more fun stuff, you can install the pry-plus gem and then require
‘pry-rescue/rspec’
Typing
play-l6
will prompt line 6 to play that 1 lineEdit –m
edits the current method (so if you want to fix an issue)And then
try-again
will rerun that 1 test so that you don’t have to deal with rerunning the entire test suite just to see if you fixed that one test.You can also use pry to vertically in a method by using the up and down keys. You can use the
cd
command to move through the code horizontally.Typing an underscore will refer to the last line and typing __ or two underscores will give you two lines back.
_ex_
raises the most recent exceptionwtf?
Gives you the first five lines of the stack trace so that you don’t have to scroll through a bunch of unnecessary stuff.Finally
whereami
will show you where you are in the code!This talk was really cool and super useful (as in, I can start using these shortcuts and tips immediately). It was a lot of fun to hear all about pry and all the things it can do.
No comments:
Post a Comment