Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Or:
How
I
Learned
To
Stop
Putsing
And
Love
Pry
DEBUGGING RUBY
ABOUT ME
Luke Bergen
Twitter: @lbergen
Reviewed.com
Developers spend 60% of their time
debugging
puts doesn‟t always do the trick
Enter pry and friends
DEBUGGING IN RUBY
“An IRB alternative and runtime
developer console” - http://pryrepl.org
Three major pieces
Pry-core
Pry-custom
Pry-ec...
Commands are the primary way of
interacting with Pry
Higher order of precedence than ruby code
Expandable
Regex based
...
binding.pry
play
wtf
whereami
? (show-doc)
$ (show-source)
edit
COMMANDS
ls
cd
amend-line
Also !s/foo/bar
.<sh...
_
_pry_
_ex_
_in_
_out_
_file_
_dir_
SPECIAL LOCALS
Result of last line
Local pry instance
Last exception
Array of ...
Pry can be configured by a .pryrc file
~/.pryrc
./.pryrc
Project level pryrc files are especially useful
when paired w...
Almost everything in pry is configurable
Some configuration can even be
modified at runtime via _pry_.<config>
Of parti...
When using „edit‟ pry uses this setting
If editor is not set, Pry defaults to nano
You can set editor to a proc which
a...
COMMAND PREFIX
A problem and a solution
COMMAND PREFIX
A more permanent solution
Pry.config.command_prefix = „%‟
Now all pry commands must be prefixed
with „%‟
CUSTOM COMMANDS
Pry also allows you to define your own
custom commands
By block or class
Useful for often repeated code...
CUSTOM COMMANDS
Suppose you need to clean your
database from time to time
CUSTOM COMMANDS
Programmers are lazy (in a good way)
We like our code DRY and expressive
Where is that laziness when it...
Any gem whose name fits the format pry-
<plugin> is a plugin
Beware: these gems are auto-loaded by
pry
--no-plugins opt...
Pry-debugger
Works only in MRI 1.8 – 1.9
Pry-byebug
A fork of pry-debugger
Works in MRI 2.0 – 2.1
Pry-nav
Pure ruby...
Pry-stack-explorer
Gives pry commands: up, down, frame, and show-
stack
Pry-rescue (Pry-exception_explorer has been
dep...
Uses regex magic to add some easy-access
syntax hackery
Accessing instance variables
foo.@instance_var
Calling private...
Pry-doc
Adds core ruby class documentation for “?” and
“$” commands
Pry-vterm_aliases
Makes your ZSH and Bash aliases ...
Pry-rails
Turns rails console into a pry session
Teaches Pry new commands
show-[middleware|model|models|route]
recogn...
Puts is ok. But don‟t hesitate to drop back and
use the big guns
REPL driven programming is incredibly powerful
Especia...
Upcoming SlideShare
Loading in …5
×

Debugging Ruby (with Pry)

4,037 views

Published on

Debugging Ruby or: How I Learned To Stop Putsing And Love Pry
The talk I gave at BostonRB (http://bostonrb.org/) for October 3013

Published in: Technology
  • Be the first to comment

Debugging Ruby (with Pry)

  1. 1. Or: How I Learned To Stop Putsing And Love Pry DEBUGGING RUBY
  2. 2. ABOUT ME Luke Bergen Twitter: @lbergen Reviewed.com
  3. 3. Developers spend 60% of their time debugging puts doesn‟t always do the trick Enter pry and friends DEBUGGING IN RUBY
  4. 4. “An IRB alternative and runtime developer console” - http://pryrepl.org Three major pieces Pry-core Pry-custom Pry-ecosystem WHAT IS PRY?
  5. 5. Commands are the primary way of interacting with Pry Higher order of precedence than ruby code Expandable Regex based PRY-CORE
  6. 6. binding.pry play wtf whereami ? (show-doc) $ (show-source) edit COMMANDS ls cd amend-line Also !s/foo/bar .<shell_command> exit
  7. 7. _ _pry_ _ex_ _in_ _out_ _file_ _dir_ SPECIAL LOCALS Result of last line Local pry instance Last exception Array of all ruby input expressions Array of all ruby output results Last referenced file Last referenced directory
  8. 8. Pry can be configured by a .pryrc file ~/.pryrc ./.pryrc Project level pryrc files are especially useful when paired with custom commands PRY-CUSTOM
  9. 9. Almost everything in pry is configurable Some configuration can even be modified at runtime via _pry_.<config> Of particular importance/interest: Editor Command prefix Custom commands BASIC CUSTOMIZATIONS
  10. 10. When using „edit‟ pry uses this setting If editor is not set, Pry defaults to nano You can set editor to a proc which accepts 2 params: file and line E.g. for sublime text: EDITOR
  11. 11. COMMAND PREFIX A problem and a solution
  12. 12. COMMAND PREFIX A more permanent solution Pry.config.command_prefix = „%‟ Now all pry commands must be prefixed with „%‟
  13. 13. CUSTOM COMMANDS Pry also allows you to define your own custom commands By block or class Useful for often repeated code that shouldn‟t be checked in
  14. 14. CUSTOM COMMANDS Suppose you need to clean your database from time to time
  15. 15. CUSTOM COMMANDS Programmers are lazy (in a good way) We like our code DRY and expressive Where is that laziness when it comes to the console? Pry + custom commands = DRY, expressive console
  16. 16. Any gem whose name fits the format pry- <plugin> is a plugin Beware: these gems are auto-loaded by pry --no-plugins option will skip auto-loading .pryrc files get loaded before plugins Plugin specific suppression: Pry.plugins[“debugger”].disable! PRY-ECOSYSTEM (PLUGINS)
  17. 17. Pry-debugger Works only in MRI 1.8 – 1.9 Pry-byebug A fork of pry-debugger Works in MRI 2.0 – 2.1 Pry-nav Pure ruby implementation Works on all rubies DEBUGGERS
  18. 18. Pry-stack-explorer Gives pry commands: up, down, frame, and show- stack Pry-rescue (Pry-exception_explorer has been deprecated) Allows you to rescue from exceptions into a pry session Rescue for entire program, or only for specified blocks Plymouth Jump into a pry session on test failure WHEN THINGS BREAK
  19. 19. Uses regex magic to add some easy-access syntax hackery Accessing instance variables foo.@instance_var Calling private methods foo.!private_meth() Examining things in parent pry sessions puts ../local_var Just be careful with your regexes PRY-SYNTAX-HACKS
  20. 20. Pry-doc Adds core ruby class documentation for “?” and “$” commands Pry-vterm_aliases Makes your ZSH and Bash aliases available to pry‟s .<shell> command Pry-theme Color themes for pry‟s syntax highlighting MISCELLANEOUS PLUGINS
  21. 21. Pry-rails Turns rails console into a pry session Teaches Pry new commands show-[middleware|model|models|route] recognize-path Pry-plus A pre-packaged group of useful plugins INTEGRATION
  22. 22. Puts is ok. But don‟t hesitate to drop back and use the big guns REPL driven programming is incredibly powerful Especially when working with new code/library Explore code Bundle open your favorite gem when it bugs out (or just cd into it) You never know what you‟ll find CONCLUSION

×