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. 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!        
  2. 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  
  3. 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  
  4. 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
  5. 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
  6. 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
  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
  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
  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
  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 [3] pry(main)>
  11. 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”
  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)> Base64.encode64(“hi”) => “aGk=\n” [4] pry(main)> Base64.decode64(_) => “hi”
  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.enc<tab>4(“hi”) => “aGk=\n” [4] pry(main)> Base64.decode64(_) => “hi”
  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.encode64(“hi”) => “aGk=\n” [4] pry(main)> Base64.decode64(_) => “hi”
  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”
  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”
  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”
  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”
  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”
  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 [5] pry(main)>
  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)> ? Base64.strict_encode64
  22. How  do  I  use  the  Base64  library?   [5] pry(main)>

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

    ? Base64.encode64 From: ~/ruby-1.9.3/lib/ruby/base64.rb Owner: #<Class:Base64> 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.
  24. How  do  I  use  the  Base64  library?   •  Evaluate

     ruby  code.   •  Colour   •  Tab-­‐compleFon   •  _  (the  last  output)   •  ls  (list  methods)   •  ?  (show-­‐doc)  
  25. 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  
  26. 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
  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
  28. 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’
  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’
  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’
  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 [5] pry(main)> wtf? 0’
  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’
  33. 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__’
  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 `decode64’ 1: ~/basic_auth.rb:10 in `decode’ 2: (pry):3 in `__pry__’ [6] pry(main)>
  35. 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
  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)> show-source BasicAuth.decode
  37. Why  doesn’t  my  method  work?   [6] pry(main)> $ BasicAuth.decode

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

    [7] pry(main)> edit <alt+_> (on Linux) <esc>_ (on a Mac)
  40. Why  doesn’t  my  method  work?   [7] pry(main)> edit BasicAuth.decode

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

    [8] pry(main)> BasicAuth.encode ‘hi’, ‘mum’ => “Basic aGk6bXVt” [9] pry(main)>
  42. 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)>
  43. 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)>
  44. Why  doesn’t  my  method  work?   •  wd?  (show  backtrace,

     also  _ex_.backtrace)   •  $  (show-­‐source)   •  edit  (uses  $EDITOR)   •  <Alt+_>,  <esc>_  (copy  last  word  down)   •  <ctrl+r>  (search  history)   •  _file_  (last  shown  file,  also  _dir_)   •  _out_  (array  of  all  output  values,  also  _in_)  
  45. 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  
  46. 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 `<main>’ $
  47. 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 `<main>’ $ subl post.rb
  48. 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)>
  49. 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
  50. 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
  51. 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
  52. Where  did  that  nil  come  from?   [1] pry(main)> puts

    new_post.safe_title NoMethodError: undefined method `gsub' for nil [2] pry(main)>
  53. 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
  54. 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(#<Post>):1>
  55. 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(#<Post>):1> ls
  56. 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(#<Post>):1> ls Post#methods: make_safe safe_title instance variables: @params locals: _ _dir_ _ex_ _file_ _in_ _out_ _pry_ [4] pry(#<Post>):1>
  57. 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(#<Post>):1> ls Post#methods: make_safe safe_title instance variables: @params locals: _ _dir_ _ex_ _file_ _in_ _out_ _pry_ [4] pry(#<Post>):1> $ safe_title
  58. 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(#<Post>):1> ls Post#methods: make_safe safe_title instance variables: @params locals: _ _dir_ _ex_ _file_ _in_ _out_ _pry_ [4] pry(#<Post>):1> $ Post#safe_title
  59. Where  did  that  nil  come  from?   [4] pry(#<Post>):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(#<Post>):1>
  60. Where  did  that  nil  come  from?   [4] pry(#<Post>):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(#<Post>):1> @params
  61. Where  did  that  nil  come  from?   [4] pry(#<Post>):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(#<Post>):1> @params {“title”=>”new post”, “body”=>”your text here”} [6] pry(#<Post>):1>
  62. Where  did  that  nil  come  from?   [4] pry(#<Post>):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(#<Post>):1> @params {“title”=>”new post”, “body”=>”your text here”} [6] pry(#<Post>):1> edit --ex
  63. Where  did  that  nil  come  from?   [5] pry(#<Post>):1> @params

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

    {“title”=>”new post”, “body”=>”your text here”} [6] pry(#<Post>):1> edit --ex [7] pry(#<Post>):1> .git diff
  65. Where  did  that  nil  come  from?   [7] pry(#<Post>):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(#<Post>):1>
  66. Where  did  that  nil  come  from?   [7] pry(#<Post>):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(#<Post>):1> cd ..
  67. Where  did  that  nil  come  from?   [8] pry(#<Post>):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)>
  68. Where  did  that  nil  come  from?   [8] pry(#<Post>):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
  69. 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)>
  70. 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;
  71. 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)>
  72. 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)> <ctrl+d>
  73. 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 $
  74. 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)   •  <ctrl-­‐d>  (cd  ..  or  exit)   •  ;  (at  end  of  line,  hides  output)  
  75. 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  
  76. 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    
  77. be4er_errors   •  BetterErrors.use_pry! •  Opens  a  web  page  on

     unhandled  excepFons   •  Lets  you  debug  your  program  amer  it  crashes  
  78. pry-­‐debugger   •  break   – Adds  a  breakpoint   • 

    step/next   – Step  into  or  over  the  next  line     •  conFnue   – Stop  stepping  through    
  79. 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!  
  80. 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