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

Pry — the good parts!

Pry — the good parts!

A whirlwind tour through the good parts of pry (the featureful alternative to ruby's irb).

If you've never used pry before, this should get you well on your way to understanding it. If you have, I can guarantee you'll learn something new :).

Conrad Irwin

April 30, 2013
Tweet

More Decks by Conrad Irwin

Other Decks in Programming

Transcript

  1. group :development do
    gem ‘pry-rails’
    gem ‘pry-plus’
    end

    View Slide

  2. What’s  Pry?  
    •   “like  irb,  but  be4er”  
     
    •  Everything  you  need  to  program  in  Ruby  
    •  (apart  from  a  text  editor)  
     
    •  60%  of  your  Fme  is  spent  debugging!  
     
     
     

    View Slide

  3. Brief  history  
    •  2010:  John  Mair  (@banisterfiend)  created  pry  
    •  2011:  Ryan  Fitzgerald  (@rrff__)  and  I  joined  in  
    •  2012:  reached  1,000,000  downloads  
    –  Kyrylo  Silin  (@kyrylosilin)  and  rking  
    (@sharpsawDOTorg)  started  contribuFng  
    •  2013:  reached  2,000,000  downloads  
    –  71  disFnct  contributors  

    View Slide

  4. Debugging  a  blog  engine  
    •  IntroducFon  
    •  How  do  I  use  the  Base64  library?  
    •  Why  doesn’t  my  method  work?  
    •  Where  did  that  nil  come  from?  
    •  Further  reading  

    View Slide

  5. How  do  I  use  the  Base64  library?  
     
    $ pry
    [1] pry(main)> require ‘base64’
    => true
    [2] pry(main)> ls Base64
    Base64.methods: decode64 encode64
    strict_decode64 strict_encode64
    urlsafe_decode64 urlsafe_encode64

    View Slide

  6. How  do  I  use  the  Base64  library?  
     
    $ pry pry
    [1] pry(main)> require ‘base64’
    => true
    [2] pry(main)> ls Base64
    Base64.methods: decode64 encode64
    strict_decode64 strict_encode64
    urlsafe_decode64 urlsafe_encode64

    View Slide

  7. How  do  I  use  the  Base64  library?  
     
    $ pry
    [1] pry(main)> require ‘base64’
    => true
    [2] pry(main)> ls Base64
    Base64.methods: decode64 encode64
    strict_decode64 strict_encode64
    urlsafe_decode64 urlsafe_encode64

    View Slide

  8. How  do  I  use  the  Base64  library?  
     
    $ pry
    [1] pry(main)> require ‘base64’
    => true
    [2] pry(main)> ls Base64
    Base64.methods: decode64 encode64
    strict_decode64 strict_encode64
    urlsafe_decode64 urlsafe_encode64

    View Slide

  9. How  do  I  use  the  Base64  library?  
     
    $ pry
    [1] pry(main)> require ‘base64’
    => true
    [2] pry(main)> ls Base64
    Base64.methods: decode64 encode64
    strict_decode64 strict_encode64
    urlsafe_decode64 urlsafe_encode64

    View Slide

  10. How  do  I  use  the  Base64  library?  
     
    $ pry
    [1] pry(main)> require ‘base64’
    => true
    [2] pry(main)> ls Base64
    Base64.methods: decode64 encode64
    strict_decode64 strict_encode64
    urlsafe_decode64 urlsafe_encode64

    View Slide

  11. How  do  I  use  the  Base64  library?  
     
    $ pry
    [1] pry(main)> require ‘base64’
    => true
    [2] pry(main)> ls Base64
    Base64.methods: decode64 encode64
    strict_decode64 strict_encode64
    urlsafe_decode64 urlsafe_encode64
    [3] pry(main)>

    View Slide

  12. How  do  I  use  the  Base64  library?  
    [2] pry(main)> ls Base64
    Base64.methods: decode64 encode64
    strict_decode64 strict_encode64
    urlsafe_decode64 urlsafe_encode64
    [3] pry(main)> (“hi”)
    => “aGk=\n”
    [4] pry(main)> Base64.decode64(_)
    => “hi”

    View Slide

  13. How  do  I  use  the  Base64  library?  
    [2] pry(main)> ls Base64
    Base64.methods: decode64 encode64
    strict_decode64 strict_encode64
    urlsafe_decode64 urlsafe_encode64
    [3] pry(main)> Base64.encode64(“hi”)
    => “aGk=\n”
    [4] pry(main)> Base64.decode64(_)
    => “hi”

    View Slide

  14. How  do  I  use  the  Base64  library?  
    [2] pry(main)> ls Base64
    Base64.methods: decode64 encode64
    strict_decode64 strict_encode64
    urlsafe_decode64 urlsafe_encode64
    [3] pry(main)> Base64.enc4(“hi”)
    => “aGk=\n”
    [4] pry(main)> Base64.decode64(_)
    => “hi”

    View Slide

  15. How  do  I  use  the  Base64  library?  
    [2] pry(main)> ls Base64
    Base64.methods: decode64 encode64
    strict_decode64 strict_encode64
    urlsafe_decode64 urlsafe_encode64
    [3] pry(main)> Base64.encode64(“hi”)
    => “aGk=\n”
    [4] pry(main)> Base64.decode64(_)
    => “hi”

    View Slide

  16. How  do  I  use  the  Base64  library?  
    [2] pry(main)> ls Base64
    Base64.methods: decode64 encode64
    strict_decode64 strict_encode64
    urlsafe_decode64 urlsafe_encode64
    [3] pry(main)> Base64.encode64 “hi”)
    => “aGk=\n”
    [4] pry(main)> Base64.decode64(_)
    => “hi”

    View Slide

  17. How  do  I  use  the  Base64  library?  
    [2] pry(main)> ls Base64
    Base64.methods: decode64 encode64
    strict_decode64 strict_encode64
    urlsafe_decode64 urlsafe_encode64
    [3] pry(main)> Base64.encode64 “hi”
    => “aGk=\n”
    [4] pry(main)> Base64.decode64 _
    => “hi”

    View Slide

  18. How  do  I  use  the  Base64  library?  
    [2] pry(main)> ls Base64
    Base64.methods: decode64 encode64
    strict_decode64 strict_encode64
    urlsafe_decode64 urlsafe_encode64
    [3] pry(main)> Base64.encode64 “hi”
    => “aGk=\n”
    [4] pry(main)> Base64.decode64 _
    => “hi”

    View Slide

  19. How  do  I  use  the  Base64  library?  
    [2] pry(main)> ls Base64
    Base64.methods: decode64 encode64
    strict_decode64 strict_encode64
    urlsafe_decode64 urlsafe_encode64
    [3] pry(main)> Base64.encode64 “hi”
    => “aGk=\n”
    [4] pry(main)> Base64.decode64 _
    => “hi”

    View Slide

  20. How  do  I  use  the  Base64  library?  
    [2] pry(main)> ls Base64
    Base64.methods: decode64 encode64
    strict_decode64 strict_encode64
    urlsafe_decode64 urlsafe_encode64
    [3] pry(main)> Base64.encode64 “hi”
    => “aGk=\n”
    [4] pry(main)> Base64.decode64 _
    => “hi”

    View Slide

  21. How  do  I  use  the  Base64  library?  
    [2] pry(main)> ls Base64
    Base64.methods: decode64 encode64
    strict_decode64 strict_encode64
    urlsafe_decode64 urlsafe_encode64
    [5] pry(main)>

    View Slide

  22. How  do  I  use  the  Base64  library?  
    [2] pry(main)> ls Base64
    Base64.methods: decode64 encode64
    strict_decode64 strict_encode64
    urlsafe_decode64 urlsafe_encode64
    [5] pry(main)> ? Base64.strict_encode64

    View Slide

  23. How  do  I  use  the  Base64  library?  
    [5] pry(main)> ? Base64.strict_encode64
    From: ~/ruby-1.9.3/lib/ruby/base64.rb
    Owner: #
    Visibility: public
    Signature: strict_encode64(bin)
    Returns the Base64-encoded version of bin.
    This method complies with RFC 4648.
    No line feeds are added.

    View Slide

  24. How  do  I  use  the  Base64  library?  
    [5] pry(main)> ? Base64.encode64
    From: ~/ruby-1.9.3/lib/ruby/base64.rb
    Owner: #
    Visibility: public
    Signature: encode64(bin)
    Returns the Base64-encoded version of bin.
    This method complies with RFC 2045.
    Line feeds are added every 60 characters.

    View Slide

  25. How  do  I  use  the  Base64  library?  
    •  Evaluate  ruby  code.  
    •  Colour  
    •  Tab-­‐compleFon  
    •  _  (the  last  output)  
    •  ls  (list  methods)  
    •  ?  (show-­‐doc)  

    View Slide

  26. Debugging  a  blog  engine  
    •  IntroducFon  
    •  How  do  I  use  the  Base64  library?  
    •  Why  doesn’t  my  method  work?  
    •  Where  did  that  nil  come  from?  
    •  Further  reading  

    View Slide

  27. Why  doesn’t  my  method  work?  
    [1] pry(main)> cat basic_auth.rb
    require ‘base64’
    module BasicAuth
    def self.encode(username, password)
    s = [username, password].join(“:”)
    “Basic #{Base64.strict_encode64 s}”
    end
    def self.decode(header)
    base64 = header[/Basic (.*)/, 1]
    Base64.decode64 base64.split(“:”)
    end
    end

    View Slide

  28. Why  doesn’t  my  method  work?  
    [1] pry(main)> cat basic_auth.rb
    require ‘base64’
    module BasicAuth
    def self.encode(username, password)
    s = [username, password].join(“:”)
    “Basic #{Base64.strict_encode64 s}”
    end
    def self.decode(header)
    base64 = header[/Basic (.*)/, 1]
    Base64.decode64 base64.split(“:”)
    end
    end

    View Slide

  29. Why  doesn’t  my  method  work?  
    [1] pry(main)> cat basic_auth.rb
    [2] pry(main)> require _file_
    => true
    [3] pry(main)> BasicAuth.encode ‘hi’, ‘mum’
    => “Basic aGk6bXVt”
    [4] pry(main)> BasicAuth.decode _
    NoMethodError: undefined method `unpack’ for
    [“aGk6bXVt”]:Array
    [4] pry(main)> wtf?
    0’

    View Slide

  30. Why  doesn’t  my  method  work?  
    [1] pry(main)> cat basic_auth.rb
    [2] pry(main)> require _file_
    => true
    [3] pry(main)> BasicAuth.encode ‘hi’, ‘mum’
    => “Basic aGk6bXVt”
    [4] pry(main)> BasicAuth.decode _
    NoMethodError: undefined method `unpack’ for
    [“aGk6bXVt”]:Array
    [4] pry(main)> wtf?
    0’

    View Slide

  31. Why  doesn’t  my  method  work?  
    [1] pry(main)> cat basic_auth.rb
    [2] pry(main)> require _file_
    => true
    [3] pry(main)> BasicAuth.encode ‘hi’, ‘mum’
    => “Basic aGk6bXVt”
    [4] pry(main)> BasicAuth.decode _
    NoMethodError: undefined method `unpack’ for
    [“aGk6bXVt”]:Array
    [4] pry(main)> wtf?
    0’

    View Slide

  32. Why  doesn’t  my  method  work?  
    [1] pry(main)> cat basic_auth.rb
    [2] pry(main)> require _file_
    => true
    [3] pry(main)> BasicAuth.encode ‘hi’, ‘mum’
    => “Basic aGk6bXVt”
    [4] pry(main)> BasicAuth.decode _
    NoMethodError: undefined method `unpack’ for
    [“aGk6bXVt”]:Array
    [5] pry(main)> wtf?
    0’

    View Slide

  33. Why  doesn’t  my  method  work?  
    [1] pry(main)> cat basic_auth.rb
    [2] pry(main)> require _file_
    => true
    [3] pry(main)> BasicAuth.encode ‘hi’, ‘mum’
    => “Basic aGk6bXVt”
    [4] pry(main)> BasicAuth.decode _
    NoMethodError: undefined method `unpack’ for
    [“aGk6bXVt”]:Array
    [5] pry(main)> wtf?
    0’

    View Slide

  34. Why  doesn’t  my  method  work?  
    [4] pry(main)> BasicAuth.decode _
    NoMethodError: undefined method `unpack’ for
    [“aGk6bXVt”]:Array
    [5] pry(main)> wtf?
    0: ~/ruby-1.9.3/lib/ruby/base64.rb:58 in
    `decode’
    1: ~/basic_auth.rb:10 in `decode’
    2: (pry):3 in `__pry__’

    View Slide

  35. Why  doesn’t  my  method  work?  
    [4] pry(main)> BasicAuth.decode _
    NoMethodError: undefined method `unpack’ for
    [“aGk6bXVt”]:Array
    [5] pry(main)> wtf?
    0: ~/ruby-1.9.3/lib/ruby/base64.rb:58 in
    `decode64’
    1: ~/basic_auth.rb:10 in `decode’
    2: (pry):3 in `__pry__’
    [6] pry(main)>

    View Slide

  36. Why  doesn’t  my  method  work?  
    [5] pry(main)> wtf?
    0: ~/ruby-1.9.3/lib/ruby/base64.rb:58 in
    `decode64’
    1: ~/basic_auth.rb:10 in `decode’
    2: (pry):3 in `__pry__’
    [6] pry(main)> $ BasicAuth.decode

    View Slide

  37. Why  doesn’t  my  method  work?  
    [5] pry(main)> wtf?
    0: ~/ruby-1.9.3/lib/ruby/base64.rb:58 in
    `decode64’
    1: ~/basic_auth.rb:10 in `decode’
    2: (pry):3 in `__pry__’
    [6] pry(main)> show-source BasicAuth.decode

    View Slide

  38. Why  doesn’t  my  method  work?  
    [6] pry(main)> $ BasicAuth.decode
    From: ~/basic_auth.rb
    Owner: #
    Visibility: public
    Number of lines: 4
    def self.decode(header)
    base64 = header[/Basic (.*)/, 1]
    Base64.decode64 base64.split(“:”)
    end

    View Slide

  39. Why  doesn’t  my  method  work?  
    [5] pry(main)> wtf?
    0: ~/ruby-1.9.3/lib/ruby/base64.rb:58 in
    `decode64’
    1: ~/basic_auth.rb:10 in `decode’
    2: (pry):3 in `__pry__’
    [6] pry(main)> $ BasicAuth.decode
    [7] pry(main)> edit

    View Slide

  40. Why  doesn’t  my  method  work?  
    [6] pry(main)> $ BasicAuth.decode
    [7] pry(main)> edit

    View Slide

  41. Why  doesn’t  my  method  work?  
    [6] pry(main)> $ BasicAuth.decode
    [7] pry(main)> edit (on Linux)
    _ (on a Mac)

    View Slide

  42. Why  doesn’t  my  method  work?  
    [6] pry(main)> $ BasicAuth.decode
    [7] pry(main)> edit BasicAuth.decode

    View Slide

  43. View Slide

  44. View Slide

  45. Why  doesn’t  my  method  work?  
    [7] pry(main)> edit BasicAuth.decode
    [8] pry(main)>

    View Slide

  46. Why  doesn’t  my  method  work?  
    [7] pry(main)> edit BasicAuth.decode
    [8] pry(main)>

    View Slide

  47. Why  doesn’t  my  method  work?  
    [7] pry(main)> edit BasicAuth.decode
    [8] pry(main)>
    (reverse-i-search)`’:

    View Slide

  48. Why  doesn’t  my  method  work?  
    [7] pry(main)> edit BasicAuth.decode
    [8] pry(main)> BasicAuth.encode ‘hi’, ‘mum’
    (reverse-i-search)`enc’

    View Slide

  49. Why  doesn’t  my  method  work?  
    [7] pry(main)> edit BasicAuth.decode
    [8] pry(main)> BasicAuth.encode ‘hi’, ‘mum’
    => “Basic aGk6bXVt”
    [9] pry(main)>

    View Slide

  50. Why  doesn’t  my  method  work?  
    [7] pry(main)> edit BasicAuth.decode
    [8] pry(main)> BasicAuth.encode ‘hi’, ‘mum’
    => “Basic aGk6bXVt”
    [9] pry(main)> BasicAuth.decode _
    => [“hi”, “mum”]
    [10] pry(main)>

    View Slide

  51. Why  doesn’t  my  method  work?  
    [7] pry(main)> edit BasicAuth.decode
    [8] pry(main)> BasicAuth.encode ‘hi’, ‘mum’
    => “Basic aGk6bXVt”
    [9] pry(main)> BasicAuth.decode _
    => [“hi”, “mum”]
    [10] pry(main)>

    View Slide

  52. Why  doesn’t  my  method  work?  
    •  wd?  (show  backtrace,  also  _ex_.backtrace)  
    •  $  (show-­‐source)  
    •  edit  (uses  $EDITOR)  
    •  ,  _  (copy  last  word  down)  
    •   (search  history)  
    •  _file_  (last  shown  file,  also  _dir_)  
    •  _out_  (array  of  all  output  values,  also  _in_)  

    View Slide

  53. Debugging  a  blog  engine  
    •  IntroducFon  
    •  How  do  I  use  the  Base64  library?  
    •  Why  doesn’t  my  method  work?  
    •  Where  did  that  nil  come  from?  
    •  Further  reading  

    View Slide

  54. Where  did  that  nil  come  from?  
    $ ruby post.rb
    post.rb:11:in `make_safe': undefined method
    `gsub' for nil:NilClass (NoMethodError)
    from post.rb:7:in `safe_title'
    from post.rb:19:in `’
    $

    View Slide

  55. Where  did  that  nil  come  from?  
    $ ruby post.rb
    post.rb:11:in `make_safe': undefined method
    `gsub' for nil:NilClass (NoMethodError)
    from post.rb:7:in `safe_title'
    from post.rb:19:in `’
    $ subl post.rb

    View Slide

  56. View Slide

  57. View Slide

  58. Where  did  that  nil  come  from?  
    $ subl post.rb
    $
     

    View Slide

  59. Where  did  that  nil  come  from?  
    $ subl post.rb
    $ ruby post.rb
     

    View Slide

  60. Where  did  that  nil  come  from?  
    $ subl post.rb
    $ ruby post.rb
    From: post.rb @ line 18 :
    15: new_post = Post.new(
    16: 'title' => 'new post',
    17: 'body' => 'your text here')
    => 18: binding.pry # Added for debugging
    19: puts new_post.safe_title
    [1] pry(main)>

    View Slide

  61. Where  did  that  nil  come  from?  
    $ subl post.rb
    $ ruby post.rb
    From: post.rb @ line 18 :
    15: new_post = Post.new(
    16: 'title' => 'new post',
    17: 'body' => 'your text here')
    => 18: binding.pry # Added for debugging
    19: puts new_post.safe_title
    [1] pry(main)> play –l 19

    View Slide

  62. Where  did  that  nil  come  from?  
    $ subl post.rb
    $ ruby post.rb
    From: post.rb @ line 18 :
    15: new_post = Post.new(
    16: 'title' => 'new post',
    17: 'body' => 'your text here')
    => 18: binding.pry # Added for debugging
    19: puts new_post.safe_title
    [1] pry(main)> puts new_post.safe_title

    View Slide

  63. Where  did  that  nil  come  from?  
    $ subl post.rb
    $ ruby post.rb
    From: post.rb @ line 18 :
    15: new_post = Post.new(
    16: 'title' => 'new post',
    17: 'body' => 'your text here')
    => 18: binding.pry # Added for debugging
    19: puts new_post.safe_title
    [1] pry(main)> play –l 19
    NoMethodError: undefined method `gsub' for nil

    View Slide

  64. Where  did  that  nil  come  from?  
    [1] pry(main)> puts new_post.safe_title
    NoMethodError: undefined method `gsub' for nil
    [2] pry(main)>

    View Slide

  65. Where  did  that  nil  come  from?  
    [1] pry(main)> puts new_post.safe_title
    NoMethodError: undefined method `gsub' for nil
    [2] pry(main)> cd new_post

    View Slide

  66. Where  did  that  nil  come  from?  
    [1] pry(main)> puts new_post.safe_title
    NoMethodError: undefined method `gsub' for nil
    [2] pry(main)> cd new_post
    [3] pry(#):1>

    View Slide

  67. Where  did  that  nil  come  from?  
    [1] pry(main)> puts new_post.safe_title
    NoMethodError: undefined method `gsub' for nil
    [2] pry(main)> cd new_post
    [3] pry(#):1> ls

    View Slide

  68. Where  did  that  nil  come  from?  
    [1] pry(main)> puts new_post.safe_title
    NoMethodError: undefined method `gsub' for nil
    [2] pry(main)> cd new_post
    [3] pry(#):1> ls
    Post#methods: make_safe safe_title
    instance variables: @params
    locals: _ _dir_ _ex_ _file_ _in_ _out_ _pry_
    [4] pry(#):1>

    View Slide

  69. Where  did  that  nil  come  from?  
    [1] pry(main)> puts new_post.safe_title
    NoMethodError: undefined method `gsub' for nil
    [2] pry(main)> cd new_post
    [3] pry(#):1> ls
    Post#methods: make_safe safe_title
    instance variables: @params
    locals: _ _dir_ _ex_ _file_ _in_ _out_ _pry_
    [4] pry(#):1> $ safe_title

    View Slide

  70. Where  did  that  nil  come  from?  
    [1] pry(main)> puts new_post.safe_title
    NoMethodError: undefined method `gsub' for nil
    [2] pry(main)> cd new_post
    [3] pry(#):1> ls
    Post#methods: make_safe safe_title
    instance variables: @params
    locals: _ _dir_ _ex_ _file_ _in_ _out_ _pry_
    [4] pry(#):1> $ Post#safe_title

    View Slide

  71. Where  did  that  nil  come  from?  
    [4] pry(#):1> $ safe_title
    From: post.rb @ line 7:
    Number of lines: 3
    Owner: Post
    Visibility: public
    def safe_title
    make_safe @params[:title]
    end
    [5] pry(#):1>

    View Slide

  72. Where  did  that  nil  come  from?  
    [4] pry(#):1> $ safe_title
    From: post.rb @ line 7:
    Number of lines: 3
    Owner: Post
    Visibility: public
    def safe_title
    make_safe @params[:title]
    end
    [5] pry(#):1> @params

    View Slide

  73. Where  did  that  nil  come  from?  
    [4] pry(#):1> $ safe_title
    From: post.rb @ line 7:
    Number of lines: 3
    Owner: Post
    Visibility: public
    def safe_title
    make_safe @params[:title]
    end
    [5] pry(#):1> @params
    {“title”=>”new post”, “body”=>”your text here”}
    [6] pry(#):1>

    View Slide

  74. Where  did  that  nil  come  from?  
    [4] pry(#):1> $ safe_title
    From: post.rb @ line 7:
    Number of lines: 3
    Owner: Post
    Visibility: public
    def safe_title
    make_safe @params[:title]
    end
    [5] pry(#):1> @params
    {“title”=>”new post”, “body”=>”your text here”}
    [6] pry(#):1> edit --ex

    View Slide

  75. View Slide

  76. View Slide

  77. Where  did  that  nil  come  from?  
    [5] pry(#):1> @params
    {“title”=>”new post”, “body”=>”your text here”}
    [6] pry(#):1> edit --ex
    [7] pry(#):1>

    View Slide

  78. Where  did  that  nil  come  from?  
    [5] pry(#):1> @params
    {“title”=>”new post”, “body”=>”your text here”}
    [6] pry(#):1> edit --ex
    [7] pry(#):1> .git diff

    View Slide

  79. Where  did  that  nil  come  from?  
    [7] pry(#):1> .git diff
    diff --git a/post.rb b/post.rb
    index d0ed356..057d37c 100644
    --- a/post.rb
    +++ b/post.rb
    @@ -4, 7 +4,7 @@ class Post
    end
    def safe_title
    - make_safe @params[:title]
    + make_safe @params[‘title’]
    end
    [8] pry(#):1>

    View Slide

  80. Where  did  that  nil  come  from?  
    [7] pry(#):1> .git diff
    diff --git a/post.rb b/post.rb
    index d0ed356..057d37c 100644
    --- a/post.rb
    +++ b/post.rb
    @@ -4, 7 +4,7 @@ class Post
    end
    def safe_title
    - make_safe @params[:title]
    + make_safe @params[‘title’]
    end
    [8] pry(#):1> cd ..

    View Slide

  81. Where  did  that  nil  come  from?  
    [8] pry(#):1> cd ..
    [9] pry(main)>

    View Slide

  82. Where  did  that  nil  come  from?  
    [8] pry(#):1> cd ..
    [9] pry(main)> whereami

    View Slide

  83. Where  did  that  nil  come  from?  
    [8] pry(#):1> cd ..
    [9] pry(main)> whereami
    From: post.rb @ line 18 :
    15: new_post = Post.new(
    16: 'title' => 'new post',
    17: 'body' => 'your text here')
    => 18: binding.pry # Added for debugging
    19: puts new_post.safe_title
    [10] pry(main)>

    View Slide

  84. Where  did  that  nil  come  from?  
    [8] pry(#):1> cd ..
    [9] pry(main)> whereami
    From: post.rb @ line 18 :
    15: new_post = Post.new(
    16: 'title' => 'new post',
    17: 'body' => 'your text here')
    => 18: binding.pry # Added for debugging
    19: puts new_post.safe_title
    [10] pry(main)> puts new_post.safe_title

    View Slide

  85. Where  did  that  nil  come  from?  
    [9] pry(main)> whereami
    From: post.rb @ line 18 :
    15: new_post = Post.new(
    16: 'title' => 'new post',
    17: 'body' => 'your text here')
    => 18: binding.pry # Added for debugging
    19: puts new_post.safe_title
    [10] pry(main)> puts new_post.safe_title
    new-post
    => nil
    [11] pry(main)>

    View Slide

  86. Where  did  that  nil  come  from?  
    [10] pry(main)> puts new_post.safe_title
    new-post
    => nil
    [11] pry(main)> puts new_post.safe_title;

    View Slide

  87. Where  did  that  nil  come  from?  
    [10] pry(main)> puts new_post.safe_title
    new-post
    => nil
    [11] pry(main)> puts new_post.safe_title;
    new-post
    [12] pry(main)>

    View Slide

  88. Where  did  that  nil  come  from?  
    [10] pry(main)> puts new_post.safe_title
    new-post
    => nil
    [11] pry(main)> puts new_post.safe_title;
    new-post
    [12] pry(main)>

    View Slide

  89. Where  did  that  nil  come  from?  
    [10] pry(main)> puts new_post.safe_title
    new-post
    => nil
    [11] pry(main)> puts new_post.safe_title;
    new-post
    [12] pry(main)>
    new-post
    $

    View Slide

  90. Where  did  that  nil  come  from?  
    •  binding.pry  (open  pry  right  here,  right  now)  
    •  cd  (change  self)  
    •  whereami  
    •  edit  -­‐-­‐ex  
    •  play  -­‐l  
    •  .  (run’s  shell  commands)  
    •   (cd  ..  or  exit)  
    •  ;  (at  end  of  line,  hides  output)  

    View Slide

  91. Debugging  a  blog  engine  
    •  IntroducFon  
    •  How  do  I  use  the  Base64  library?  
    •  Why  doesn’t  my  method  work?  
    •  Where  did  that  nil  come  from?  
    •  Further  reading  

    View Slide

  92. pry-­‐rescue/pry-­‐stack_explorer  
    •  pry-­‐rescue  
    – AutomaFcally  opens  pry  wherever  you  have  an  
    unhandled  excepFon  or  test  failure.  
    – cd-­‐cause  lets  you  pry  into  any  excepFon  that  
    happens  inside  the  console  
       
    •  pry-­‐stack_explorer  
    – lets  you  move  up/down  the  callstack  as  though  
    you  had  a  binding.pry  at  every  level  
     

    View Slide

  93. be4er_errors  
    •  BetterErrors.use_pry!
    •  Opens  a  web  page  on  unhandled  excepFons  
    •  Lets  you  debug  your  program  amer  it  crashes  

    View Slide

  94. View Slide

  95. pry-­‐debugger  
    •  break  
    – Adds  a  breakpoint  
    •  step/next  
    – Step  into  or  over  the  next  line  
     
    •  conFnue  
    – Stop  stepping  through  
     

    View Slide

  96. Recommended  plugins  
    •  pry-­‐plus  
    – pry-­‐debugger,  pry-­‐rescue,  pry-­‐stack_explorer  
    – pry-­‐doc,  documentaFon  for  C  methods  
    – pry-­‐docmore,  documentaFon  for  ruby  syntax  
    – bond,  tab  compleFon  for  Hash#[],  etc.  
    – jist,  IntegraFon  with  gist.github.com  
    •  pry-­‐rails  
    – makes  “rails  c”  use  pry!  

    View Slide

  97. • 60%  of  your  Fme  is  spent  
    debugging  
    • use  binding.pry  

    View Slide

  98. QuesFons?!  
    •  Pry  (@pryrepl)  
    – h4p://pryrepl.org  
    – irc://irc.freenode.net/#pry  
    – type  “help”  in  pry  :)  
    •  Conrad  Irwin  (@ConradIrwin)  
    – h4p://cirw.in  
    – irc://irc.freenode.net/cirwin  

    View Slide