Debugging with pry
❤
Murilo Capanema - 2015
Who Am I?
GitHub: mcapanema
BankFacil (We are Hiring!)
Murilo Capanema - 2015
What is pry ?
Pry is a powerful alternative to the standard IRB shell
for Ruby.
Murilo Capanema - 2015
Why pry ?
Features:
4 syntax highlighting;
4 flexible plugin architecture;
4 runtime invocation and source
and documentation browsing;
Murilo Capanema - 2015
Why not to use pry ?
Murilo Capanema - 2015
Murilo Capanema - 2015
What really is pry ?
One line of ruby:
def repl
loop{ puts eval $stdin.readline}
end
Murilo Capanema - 2015
A little bit of REPL
Read
Eval
Print
Loop
Murilo Capanema - 2015
A little bit of REPL
REPL with Tests
4 REPL first !;
4 Record what the code should do ";
4 Write a code that works first time #;
Murilo Capanema - 2015
Why REPL ?
REPL or Debugger Driven Development helps you to reach a code that works pretty quickly
Murilo Capanema - 2015
Backing to pry
require 'pry'
binding.pry
Murilo Capanema - 2015
Pry and his friends
4 pry-stack-explorer;
4 pry-debugger;
4 pry-rescue;
4 pry-doc;
4 pry-git;
4 help
Murilo Capanema - 2015
Pry magics
ls
[1] pry(main)> ls
self.methods: inspect to_s
locals: _ __ _dir_ _ex_ _file_ _in_ _out_ _pry_
[2] pry(main)>
Murilo Capanema - 2015
Pry magics
ls
[1] pry(main)> ls Kernel
constants: RUBYGEMS_ACTIVATION_MONITOR
Kernel.methods:
Array Rational abort block_given? exit global_variables open putc require sprintf trace_var
Complex String ap caller exit! iterator? p puts require_relative srand trap
Float __callee__ at_exit caller_locations fail lambda pp raise select syscall untrace_var
Hash __dir__ autoload catch fork load print rand set_trace_func system warn
Integer __method__ autoload? eval format local_variables printf readline sleep test
Pathname ` binding exec gets loop proc readlines spawn throw
Kernel#methods:
!~ clone frozen? kind_of? public_methods tainted?
<=> debugger hash method public_send tap
=== define_singleton_method inspect methods remove_instance_variable to_enum
=~ display instance_of? nil? respond_to? to_s
ai dup instance_variable_defined? object_id send trust
awesome_inspect enum_for instance_variable_get pretty_inspect singleton_class untaint
awesome_print eql? instance_variable_set private_methods singleton_method untrust
byebug extend instance_variables protected_methods singleton_methods untrusted?
class freeze is_a? public_method taint
[2] pry(main)>
Murilo Capanema - 2015
Pry magics
$ - show-source
[1] pry(main)> $ Kernel#puts
From: io.c (C Method):
Owner: Kernel
Visibility: private
Number of lines: 8
static VALUE
rb_f_puts(argc, argv)
int argc;
VALUE *argv;
{
rb_io_puts(argc, argv, rb_stdout);
return Qnil;
}
[2] pry(main)>
Murilo Capanema - 2015
Pry magics
$ - show-source
[1] pry(main)> $ Hash
From: /home/vagrant/.rvm/rubies/ruby-2.1.3/lib/ruby/2.1.0/pp.rb @ line 369:
Class name: Hash
Number of lines: 9
class Hash # :nodoc:
def pretty_print(q) # :nodoc:
q.pp_hash self
end
def pretty_print_cycle(q) # :nodoc:
q.text(empty? ? '{}' : '{...}')
end
end
[2] pry(main)>
Murilo Capanema - 2015
Pry magics
? - show-doc
[1] pry(main)> ? Object
From: /home/vagrant/.rvm/gems/ruby-2.1.3/gems/awesome_print-1.2.0/lib/awesome_print/core_ext/object.rb @ line 1:
Class name: Object
Number of monkeypatches: 6. Use the `-a` option to display all available monkeypatches
Number of lines: 5
...
...
...
[2] pry(main)>
Murilo Capanema - 2015
Pry magics
break
[1] pry(main)> break method_name
Breakpoint 1: /path/to/file.rb @ line 1 (Enabled):
...
...
...
[2] pry(main)>
Murilo Capanema - 2015
Pry magics
try-again
[1] pry(main)> try-again
[2] pry(main)>
Murilo Capanema - 2015
Pry magics
step/next
[1] pry(main)> step
[2] pry(main)> next
Murilo Capanema - 2015
Pry magics
play
[1] pry(main)> play -l 1
[2] pry(main)>
Murilo Capanema - 2015
Pry magics
edit [OPTIONS]
4 -e Open editor on last exception raised
4 -m Open editor on a given method
4 -c Open editor on current method
[1] pry(main)> edit -m method_name
[2] pry(main)>
Murilo Capanema - 2015
Pry magics
up
[1] pry(main)> up
[2] pry(main)>
Murilo Capanema - 2015
Pry magics
down
[1] pry(main)> down
[2] pry(main)>
Murilo Capanema - 2015
Pry magics
@ - whereami
[1] pry(Kernel):1> cd Kernel#puts
[2] pry(Kernel):2> @
Inside Kernel.
[3] pry(Kernel):2>
Murilo Capanema - 2015
Debugging
4 Reproduce locally;
4 Find the problem;
4 Fix it;
Murilo Capanema - 2015
More pry magics
wtf - _ex_.backtrace
[1] pry(Kernel):1> wtf?
...
...
...
[2] pry(Kernel):2>
Murilo Capanema - 2015
Further Reading
https://www.youtube.com/watch?v=D9j_Mf91M0I
https://www.youtube.com/watch?v=4hfMUP5iTq8
https://github.com/nixme/jazz_hands
http://pryrepl.org/
Murilo Capanema - 2015
Fin
GitHub: mcapanema
Seriously We are Hiring!
Murilo Capanema - 2015

Debugging with pry

  • 1.
  • 2.
    Who Am I? GitHub:mcapanema BankFacil (We are Hiring!) Murilo Capanema - 2015
  • 3.
    What is pry? Pry is a powerful alternative to the standard IRB shell for Ruby. Murilo Capanema - 2015
  • 4.
    Why pry ? Features: 4syntax highlighting; 4 flexible plugin architecture; 4 runtime invocation and source and documentation browsing; Murilo Capanema - 2015
  • 5.
    Why not touse pry ? Murilo Capanema - 2015
  • 6.
  • 7.
    What really ispry ? One line of ruby: def repl loop{ puts eval $stdin.readline} end Murilo Capanema - 2015
  • 8.
    A little bitof REPL Read Eval Print Loop Murilo Capanema - 2015
  • 9.
    A little bitof REPL REPL with Tests 4 REPL first !; 4 Record what the code should do "; 4 Write a code that works first time #; Murilo Capanema - 2015
  • 10.
    Why REPL ? REPLor Debugger Driven Development helps you to reach a code that works pretty quickly Murilo Capanema - 2015
  • 11.
    Backing to pry require'pry' binding.pry Murilo Capanema - 2015
  • 12.
    Pry and hisfriends 4 pry-stack-explorer; 4 pry-debugger; 4 pry-rescue; 4 pry-doc; 4 pry-git; 4 help Murilo Capanema - 2015
  • 13.
    Pry magics ls [1] pry(main)>ls self.methods: inspect to_s locals: _ __ _dir_ _ex_ _file_ _in_ _out_ _pry_ [2] pry(main)> Murilo Capanema - 2015
  • 14.
    Pry magics ls [1] pry(main)>ls Kernel constants: RUBYGEMS_ACTIVATION_MONITOR Kernel.methods: Array Rational abort block_given? exit global_variables open putc require sprintf trace_var Complex String ap caller exit! iterator? p puts require_relative srand trap Float __callee__ at_exit caller_locations fail lambda pp raise select syscall untrace_var Hash __dir__ autoload catch fork load print rand set_trace_func system warn Integer __method__ autoload? eval format local_variables printf readline sleep test Pathname ` binding exec gets loop proc readlines spawn throw Kernel#methods: !~ clone frozen? kind_of? public_methods tainted? <=> debugger hash method public_send tap === define_singleton_method inspect methods remove_instance_variable to_enum =~ display instance_of? nil? respond_to? to_s ai dup instance_variable_defined? object_id send trust awesome_inspect enum_for instance_variable_get pretty_inspect singleton_class untaint awesome_print eql? instance_variable_set private_methods singleton_method untrust byebug extend instance_variables protected_methods singleton_methods untrusted? class freeze is_a? public_method taint [2] pry(main)> Murilo Capanema - 2015
  • 15.
    Pry magics $ -show-source [1] pry(main)> $ Kernel#puts From: io.c (C Method): Owner: Kernel Visibility: private Number of lines: 8 static VALUE rb_f_puts(argc, argv) int argc; VALUE *argv; { rb_io_puts(argc, argv, rb_stdout); return Qnil; } [2] pry(main)> Murilo Capanema - 2015
  • 16.
    Pry magics $ -show-source [1] pry(main)> $ Hash From: /home/vagrant/.rvm/rubies/ruby-2.1.3/lib/ruby/2.1.0/pp.rb @ line 369: Class name: Hash Number of lines: 9 class Hash # :nodoc: def pretty_print(q) # :nodoc: q.pp_hash self end def pretty_print_cycle(q) # :nodoc: q.text(empty? ? '{}' : '{...}') end end [2] pry(main)> Murilo Capanema - 2015
  • 17.
    Pry magics ? -show-doc [1] pry(main)> ? Object From: /home/vagrant/.rvm/gems/ruby-2.1.3/gems/awesome_print-1.2.0/lib/awesome_print/core_ext/object.rb @ line 1: Class name: Object Number of monkeypatches: 6. Use the `-a` option to display all available monkeypatches Number of lines: 5 ... ... ... [2] pry(main)> Murilo Capanema - 2015
  • 18.
    Pry magics break [1] pry(main)>break method_name Breakpoint 1: /path/to/file.rb @ line 1 (Enabled): ... ... ... [2] pry(main)> Murilo Capanema - 2015
  • 19.
    Pry magics try-again [1] pry(main)>try-again [2] pry(main)> Murilo Capanema - 2015
  • 20.
    Pry magics step/next [1] pry(main)>step [2] pry(main)> next Murilo Capanema - 2015
  • 21.
    Pry magics play [1] pry(main)>play -l 1 [2] pry(main)> Murilo Capanema - 2015
  • 22.
    Pry magics edit [OPTIONS] 4-e Open editor on last exception raised 4 -m Open editor on a given method 4 -c Open editor on current method [1] pry(main)> edit -m method_name [2] pry(main)> Murilo Capanema - 2015
  • 23.
    Pry magics up [1] pry(main)>up [2] pry(main)> Murilo Capanema - 2015
  • 24.
    Pry magics down [1] pry(main)>down [2] pry(main)> Murilo Capanema - 2015
  • 25.
    Pry magics @ -whereami [1] pry(Kernel):1> cd Kernel#puts [2] pry(Kernel):2> @ Inside Kernel. [3] pry(Kernel):2> Murilo Capanema - 2015
  • 26.
    Debugging 4 Reproduce locally; 4Find the problem; 4 Fix it; Murilo Capanema - 2015
  • 27.
    More pry magics wtf- _ex_.backtrace [1] pry(Kernel):1> wtf? ... ... ... [2] pry(Kernel):2> Murilo Capanema - 2015
  • 28.
  • 29.
    Fin GitHub: mcapanema Seriously Weare Hiring! Murilo Capanema - 2015