Debugging Ruby (with Pry)


Published on

Debugging Ruby or: How I Learned To Stop Putsing And Love Pry
The talk I gave at BostonRB ( for October 3013

Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Let’s be honest. The vast majority of the time, “puts the_thing_that_makes_no_sense” is completely sufficient to figure out what’s going wrong in your programI don’t believe that we spend 60% of our time debugging the kinds of problems that can be solved with a puts
  • Also, these are useful
  • Some plugins like the debuggers hook into the Pryinit process so using pryrc suppression won’t always save the day.
  • Demonstration of bug running ./todo.rb items buggy_list
  • 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
    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” - 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