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

Efficient debugging with Pry

Efficient debugging with Pry

A short talk summarizing how to use Pry to save yourself time when debugging.

Conrad Irwin

October 02, 2012
Tweet

More Decks by Conrad Irwin

Other Decks in Programming

Transcript

  1. puts l  Most common debugging tool l  Very easy to

    use l  Gather information one chunk at a time
  2. binding.pry l  Opens pry at that line in your code

    l  `gem install pry` l  Gather as much information as you need l  Test theories immediately
  3. NoMethodError l  Most common exception in ruby code? l  Caused

    by: -  Typos -  Forgetting the right method name -  Using the wrong object
  4. ls  -­‐-­‐grep   l  Finds the correct method l  No

    need to google l  Doesn't rely on gems having docs ;)
  5.     [1]  pry(main)>  ls  Base64  -­‐-­‐grep  encode   Base64.methods:

     encode64     strict_encode64    urlsafe_encode64   [2]  pry(main)>  
  6. ls  -­‐-­‐grep   l  Finds the correct method l  No

    need to google l  Doesn't rely on gems having docs ;)
  7. edit  -­‐-­‐ex   l  Opens your text editor l  Jumps

    to the exception l  Reloads the code when you're done
  8. From:  /0/ruby/pry/example.rb  @  line  3:          3:

     def  base64ify(email)    =>  4:      Base64.encode(email)          5:  end   NoMethodError:  undefined  method   `encode'  for  Base64:Module'   [1]  pry(main)>    
  9. From:  /0/ruby/pry/example.rb  @  line  3:          3:

     def  base64ify(email)    =>  4:      Base64.encode(email)          5:  end   NoMethodError:  undefined  method   `encode'  for  Base64:Module'   [1]  pry(main)>  edit  -­‐-­‐ex  
  10. [1]  pry(main)>  edit  -­‐-­‐ex          3:  def

     base64ify(email)    =>  4:      Base64.encode64(email)          5:  end     [2]  pry(main)>  base64ify("hello  world")   "aGVsbG8gd29ybGQ=\n"  
  11. up and down l  Moves pry up and down the

    call stack l  Figure out why you have the wrong object l  Discover why a gem doesn't work l  `gem install pry-stack_explorer`
  12. From:  /0/ruby/pry/example2.rb  @  line  2:          2:

     def  make_safe(text)    =>  3:      text.gsub(/[^a-­‐z]/i,  '-­‐')          4:  end   NoMethodError:  undefined  method  `gsub'   for  nil:NilClass'   [1]  pry(main)>    
  13. [1]  pry(main)>  up   From:  /0/ruby/pry/example2.rb  @  line  6:  

           2:  def  safe_title(post)    =>  3:      make_safe(post[:title])          4:  end   [2]  pry(main)>  post   {"title"  =>  "Hello  Pry"}  
  14. up and down l  Moves pry up and down the

    call stack l  Figure out why you have the wrong object l  Discover why a gem doesn't work l  `gem install pry-stack_explorer`
  15. $ and ? l  show-source and show-doc l  Instant documentation

    when you need it l  No need to `cd` into gem directories.
  16. [1]  pry(main)>  $  safe_title          2:  def

     safe_title(post)          3:      make_safe(post[:title])          4:  end   [2]  pry(main)>  
  17. [2]  pry(main)>  ?  safe_title   Convert  the  title  of  the

     blog  into  a   string  suitable  for  use  in  URLs.     param  [Hash]  post   return  [String]   [3]  pry(main)>  
  18. In conclusion l  Debugging requires gathering information l  Get into

    a `binding.pry` habit l  Explore pry's extra features -  gem install pry-full -  Type `help` inside pry