rails-pry.md#

# Pry Cheat Sheet#

Command Line

  • pry -r ./config/app_init_file.rb - load your app into a pry session (look at the file loaded by config.ru)
  • pry -r ./config/environment.rb - load your rails into a pry session

Debugger - help ls -- Display command options for pry command ls - ls <Object> -- Show all of the available methods that can be called by an object - _ -- Last eval - ? <Object> -- Shows more information (doc) about an object, or method - _file_ -- Represent the last file Pry touched - wtf? -- Print the stack trace, same as _ex_.backtrace - $ -- Show source, shortcut for show-source - edit Class -- Open file in $EDITOR - edit Class#instance_method -- Open file in $EDITOR - <ctrl+r> -- Search history - _out_ -- Array of all outputs values, also _in_ - cd <var> -- Step into an object, change the value of self - cd .. -- Take out of a level - binding.pry -- Breakpoint - edit --ex -- Edit the file where the last exception was thrown - .<Shell> -- Runs the command - whereami -- Print the context where the debugger is stopped - whereami 20 -- Print the context 20 lines where the debugger is stopped - ; -- Would mute the return output by Ruby - play -l -- Execute the line in the current debugging context

pry-nav#

  • next -- execute next line
  • step -- step into next function call
  • continue -- continue through stack

pry-rescue#

  • rescue rspec -- break on exception in rspec
  • rescue rails server -- break on exception in rails server
  • try-again -- run last failing spec, reloads the file not the enviornment

Section#

> gem install pry pry-doc # pry-doc provides MRI Core documentation and source code. We will need this in the later examples
pry(main)> show-doc Array#map
show-doc Array#map!
show-doc Enumerable#collect

Pry provides a very handy shortcut for show-doc – ?:

[3] pry(main)> ? Array#map!

[4] pry(main)> arr = [1, 2, 3]
=> [1, 2, 3]

[5] pry(main)> cd arr
[6] pry(#<Array>):1>
Notice how when we cd-ed into arr, the prompt changes to pry(#):1>. This tells us that the current object is an Array instance, denoted by the # notation. “

[6] pry(#<Array>):1> ls
Enumerable#methods:
  all?            each_entry        find_all  max      minmax_by     sort_by
  any?            each_slice        flat_map  max_by   none?
  chunk           each_with_index   grep      member?  one?
  collect_concat  each_with_object  group_by  min      partition
  detect          entries           inject    min_by   reduce
  each_cons       find              lazy      minmax   slice_before
Array#methods:
  &            count       include?            reject                slice
  *            cycle       index               reject!               slice!
  +            delete      insert              repeated_combination  sort
  -            delete_at   inspect             repeated_permutation  sort!
  <<           delete_if   join                replace               sort_by!
  <=>          drop        keep_if             reverse               take
  ==           drop_while  last                reverse!              take_while
  []           each        length              reverse_each          to_a
  []=          each_index  map                 rindex                to_ary
  assoc        empty?      map!                rotate                to_s
  at           eql?        pack                rotate!               transpose
  bsearch      fetch       permutation         sample                uniq
  clear        fill        place               select                uniq!
  collect      find_index  pop                 select!               unshift
  collect!     first       pretty_print        shelljoin             values_at
  combination  flatten     pretty_print_cycle  shift                 zip
  compact      flatten!    product             shuffle               |
  compact!     frozen?     push                shuffle!
  concat       hash        rassoc              size
self.methods: __pry__
locals: _  __  _dir_  _ex_  _file_  _in_  _out_  _pry_
[14] (pry) main: 0> ls -h
Usage: ls [-m|-M|-p|-pM] [-q|-v] [-c|-i] [Object]
       ls [-g] [-l]

ls shows you which methods, constants and variables are accessible to Pry. By default it shows you the local variables defined in the current shell, and any public methods or instance variables defined on the current object.

The colours used are configurable using Pry.config.ls.*_color, and the separator is Pry.config.ls.separator.

Pry.config.ls.ceiling is used to hide methods defined higher up in the inheritance chain, this is by default set to [Object, Module, Class] so that methods defined on all Objects are omitted. The -v flag can be used to ignore this setting and show all methods, while the -q can be used to set the ceiling much lower and show only methods defined on the object or its direct class.


options:

    -m, --methods               Show public methods defined on the Object (default)
    -M, --instance-methods      Show methods defined in a Module or Class
    -p, --ppp                   Show public, protected (in yellow) and private (in green) methods
    -q, --quiet                 Show only methods defined on object.singleton_class and object.class
    -v, --verbose               Show methods and constants on all super-classes (ignores Pry.config.ls.ceiling)
    -g, --globals               Show global variables, including those builtin to Ruby (in cyan)
    -l, --locals                Show locals, including those provided by Pry (in red)
    -c, --constants             Show constants, highlighting classes (in blue), and exceptions (in purple)
    -i, --ivars                 Show instance variables (in blue) and class variables (in bright blue)
    -G, --grep                  Filter output by regular expression
    -h, --help                  Show this message.
[10] pry(#<Array>):1> show-source map!

[11] pry(#<Array>):1> cd ..
[12] pry(main)>

The following are all equivalent:

[13] pry(main)> show-source Array#map! [14] pry(main)> show-source arr.map! Just like show-doc and ?, the equivalent for show-source is $:

[15] pry(main)> $ Array#map! [16] pry(main)> $ arr.map!

We need to tell Pry what our default editor should be. Create a file called .pryrc in your home directory.#

Pry.config.editor = 'vim'

Themeing pry#

gem install pry-theme

If detected will auto switch pry to selected theme

.pryrc

Pry.config.theme = "theme-name"

If you already have your theme stored somewhere on disk, just put it in the $HOME/.pry/themes directory

Reference documents to above#

  1. An easy way to customize Pry colors via theme files
  2. pry wiki (2019)
  3. Debugging Rails apps - official doc
  4. Using binding.pry in Rails app's view or partial
  5. Pry Tips and Tricks (2018)
  6. Pry with Rails (2011)
  7. Debugging in ruby 2 (2019 - byebug)
  8. Debugging Remote Processes With Pry-Remote

finish these#

https://www.bignerdranch.com/blog/debugging-remote-processes-with-pry-remote/ https://medium.com/@josh_works/pry-tips-and-tricks-2e5d2e838fc8 http://railscasts.com/episodes/280-pry-with-rails?view=asciicast https://github.com/pry/pry/wiki https://www.rubyguides.com/2015/07/ruby-debugging/ https://docs.gitlab.com/ee/development/pry_debugging.html https://www.reddit.com/r/rails/comments/5uberp/how_to_debug_in_view/ https://edgeguides.rubyonrails.org/debugging_rails_applications.html https://github.com/puma/puma-dev/issues/70 https://www.jackkinsella.ie/articles/debugging-rails-with-pry-debugger https://www.jackkinsella.ie/articles/debugging-rails-with-built-in-tools https://fromrailstoember.com/8-debugging-and-the-ember-inspector/ https://riptutorial.com/ruby-on-rails/example/31513/debugging-ruby-on-rails-application-with-pry https://riptutorial.com/ruby-on-rails/example/31513/debugging-ruby-on-rails-application-with-pry https://cognitohq.com/5-pry-features-every-ruby-developer-should-know/ https://www.honeybadger.io/blog/debugging-ruby-with-pry/ https://www.jackkinsella.ie/articles/debugging-rails-with-pry-console https://www.google.com/search?rlz=1C1JZAP_enAU901AU901&sxsrf=ALeKk01x5B0Nmxbao1MGrlQ6ilFk47mD5A:1589770999697&q=binding.pry+not+working&sa=X&ved=2ahUKEwj13te9trzpAhXSH7cAHcU3D2IQ1QIoAXoECBQQAg https://medium.com/rubyinside/powering-your-ruby-rails-development-with-pry-3d5dbd2a8b80 https://syndicode.com/2018/12/06/get-to-know-pry-ruby-gem/ https://dev.to/elimerrell/debugging-with-pry-a-beginners-guide-3p99 https://github.com/pry/pry/wiki/Setting-up-Rails-or-Heroku-to-use-Pry https://www.sitepoint.com/pry-friends-rails/ https://www.sitepoint.com/pry-friends-rails/ http://blog.joncairns.com/2015/11/better-rails-debugging-with-pry/ https://batsov.com/articles/2012/10/14/rails-tip-number-1-use-pry-instead-of-irb-for-the-rails-console/ https://thoughtbot.com/upcase/videos/rails-console https://thoughtbot.com/upcase/videos/debugging-for-fun-and-profit https://www.sitepoint.com/rubyists-time-pry-irb/ https://github.com/pry/pry/wiki/Available-plugins https://github.com/pry/pry/wiki https://supergood.software/a-ruby-gem-debugging-strategy/ https://github.com/pry/pry-stack_explorer https://web-crunch.com/posts/debugging-ruby-on-rails https://pragmaticstudio.com/tutorials/rails-console-shortcuts-tips-tricks https://www.bugsnag.com/blog/production-pry https://www.sitepoint.com/rubyists-time-pry-irb/ https://github.com/nixme/jazz_hands https://github.com/BetterErrors/better_errors-pry https://www.bugsnag.com/blog/production-pry https://www.jackkinsella.ie/articles/a-comprehensive-guide-to-debugging-rails https://www.jackkinsella.ie/articles/debugging-rails-with-custom-instrumentation https://www.jackkinsella.ie/articles/debugging-rails-with-pry-console https://medium.com/launch-school/params-in-rails-where-do-they-come-from-b172cdb46eb4 https://www.rubypigeon.com/posts/examining-internals-of-rails-request-response-cycle/ https://www.rubyguides.com/2019/06/rails-params/https://medium.com/rubyinside/powering-your-ruby-rails-development-with-pry-3d5dbd2a8b80https://medium.com/rubyinside/powering-your-ruby-rails-development-with-pry-3d5dbd2a8b80https://github.com/pry/pry/wiki/Setting-up-Rails-or-Heroku-to-use-Pryhttps://www.sitepoint.com/pry-friends-rails/