Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The joy of Ruby Debugging - Pry Universe

The joy of Ruby Debugging - Pry Universe

Let's be honest, in typical programming world debugging is a pain in the ass. `printf` everywhere, rerun the code every time... Such a waste of time.

But Ruby... Ruby is different, is such a great language for introspection and live code analysis. Here's where Pry shines. During this talk we will meet Pry alongside some other tool in his universe, like `pry-rescue`, `pry-stack_explorer` and we will see how debugging a simple Ruby script or a large RubyOnRails application is easy, and fun!

Avatar for Enrico Carlesso

Enrico Carlesso

November 13, 2015

Other Decks in Programming

Transcript

  1. Pry as IRB replacement Pry real magic The joy of

    ruby debugging Enrico Carlesso coders51 October 2015 Enrico Carlesso Pry universe 1/18
  2. Pry as IRB replacement Pry real magic Me Enrico Carlesso

    I spin on red things Enrico Carlesso Pry universe 2/18
  3. Pry as IRB replacement Pry real magic Debugging is boring

    Typical debugging worflow 1 Find in the backtrace the relevant file and line 2 Add some random puts (and inspect) 3 Rerun code (at least we don’t need to compile) 4 Change some code 5 Rerun 6 If you are very very lucky, move forward (likely forgetting puts around), if not iterate from number 3 Enrico Carlesso Pry universe 4/18
  4. Pry as IRB replacement Pry real magic Ruby! Then we

    remember: • Ruby is a scripting language • Introspection • Reflection • Metaprogramming • Someone maybe has put all this together? Enrico Carlesso Pry universe 5/18
  5. Pry as IRB replacement Pry real magic Pry! Pry is

    a powerful alternative to the standard IRB shell for Ruby. It features syntax highlighting, a flexible plugin architecture, runtime invocation and source and documentation browsing. from http://pryrepl.org Enrico Carlesso Pry universe 6/18
  6. Pry as IRB replacement Pry real magic What is a

    REPL? • Read • Evaluate • Print • Loop Enrico Carlesso Pry universe 7/18
  7. Pry as IRB replacement Pry real magic What is a

    REPL? • Read • Evaluate • Print • Loop def repl loop { puts eval $stdin.readline } end Enrico Carlesso Pry universe 7/18
  8. Pry as IRB replacement Pry real magic Pry cool stuff

    • Syntax Highlighting • Tab Completion • _ and __ Enrico Carlesso Pry universe 8/18
  9. Pry as IRB replacement Pry real magic Source code is

    the best documentation • ? show-doc • $ show-source • ls • cd Enrico Carlesso Pry universe 9/18
  10. Pry as IRB replacement Pry real magic Source code is

    the best documentation • ? show-doc • $ show-source • ls • cd • edit Enrico Carlesso Pry universe 9/18
  11. Pry as IRB replacement Pry real magic binding.pry binding.pry Anywhere

    in the code will stop the execution and open a REPL instance right there, with all local bindings. Enrico Carlesso Pry universe 10/18
  12. Pry as IRB replacement Pry real magic But what can

    we do? • Access to all local variables at present state • see documentation and source of relevant methods • whereami • play -l Enrico Carlesso Pry universe 11/18
  13. Pry as IRB replacement Pry real magic Plugins • pry-remote

    • pry-byebug (up and down!) pry-stack_explorer • pry-inline • pry-rescue Enrico Carlesso Pry universe 12/18
  14. Pry as IRB replacement Pry real magic Let’s see something

    DEMO! Enrico Carlesso Pry universe 13/18
  15. Pry as IRB replacement Pry real magic Pry::Commands.block_command In your

    ~/.pryrc Pry::Commands.block_command "silence_arecord", "Suppress ActiveRecord output" do ActiveRecord::Base.logger = nil end Pry::Commands.block_command "arecord_to_stdout", "Redirects ActiveRecord output to standard output" do ActiveRecord::Base.logger = Logger.new(STDOUT) end Enrico Carlesso Pry universe 14/18
  16. Pry as IRB replacement Pry real magic Pry::Commands.block_command In your

    ~/.pryrc Pry::Commands.block_command "silence_arecord", "Suppress ActiveRecord output" do ActiveRecord::Base.logger = nil end Pry::Commands.block_command "arecord_to_stdout", "Redirects ActiveRecord output to standard output" do ActiveRecord::Base.logger = Logger.new(STDOUT) end Not enough? Enrico Carlesso Pry universe 14/18
  17. Pry as IRB replacement Pry real magic Pry::Commands.create_command Pry::Commands.create_command "echo"

    do description "Echo the input: echo [ARGS]" def process output.puts args.join(" ") end end This will provide a full Pry::Commands instance Enrico Carlesso Pry universe 15/18
  18. Pry as IRB replacement Pry real magic Repl Driven Development

    • Run the code • Read the code • Understand the code Enrico Carlesso Pry universe 16/18
  19. Pry as IRB replacement Pry real magic Repl Driven Development

    • Run the code (takes seconds) • Read the code (takes minutes) • Understand the code (takes weeks? life?) Enrico Carlesso Pry universe 17/18
  20. Pry as IRB replacement Pry real magic One big thing

    Use Pry Enrico Carlesso Pry universe 18/18