Debugging Ruby (with Pry)

22
Or: How I Learne d To Stop Putsin g And Love Pry DEBUGGING RUBY

description

Debugging Ruby or: How I Learned To Stop Putsing And Love Pry The talk I gave at BostonRB (http://bostonrb.org/) for October 3013

Transcript of Debugging Ruby (with Pry)

Page 1: Debugging Ruby (with Pry)

Or: How

I Learne

dTo

StopPutsing

AndLovePry

DEBUGGING RUBY

Page 2: Debugging Ruby (with Pry)

ABOUT ME

Luke BergenTwitter: @lbergen

Reviewed.com

Page 3: Debugging Ruby (with Pry)

Developers spend 60% of their time debugging

puts doesn’t always do the trick

Enter pry and friends

DEBUGGING IN RUBY

Page 4: Debugging Ruby (with Pry)

“An IRB alternative and runtime developer console” - http://pryrepl.org

Three major piecesPry-corePry-customPry-ecosystem

WHAT IS PRY?

Page 5: Debugging Ruby (with Pry)

Commands are the primary way of interacting with PryHigher order of precedence than ruby code

ExpandableRegex based

PRY-CORE

Page 6: Debugging Ruby (with Pry)

binding.pryplaywtfwhereami? (show-doc)$ (show-

source)edit

COMMANDS

lscdamend-line

Also !s/foo/bar.<shell_command>exit

Page 7: Debugging Ruby (with Pry)

__pry__ex__in__out__file__dir_

SPECIAL LOCALS

Result of last lineLocal pry instanceLast exceptionArray of all ruby input expressionsArray of all ruby output resultsLast referenced fileLast referenced directory

Page 8: Debugging Ruby (with Pry)

Pry can be configured by a .pryrc file~/.pryrc./.pryrc

Project level pryrc files are especially useful when paired with custom commands

PRY-CUSTOM

Page 9: Debugging Ruby (with Pry)

Almost everything in pry is configurable

Some configuration can even be modified at runtime via _pry_.<config>

Of particular importance/interest:EditorCommand prefixCustom commands

BASIC CUSTOMIZATIONS

Page 10: Debugging Ruby (with Pry)

When using ‘edit’ pry uses this setting

If editor is not set, Pry defaults to nano

You can set editor to a proc which accepts 2 params: file and line

E.g. for sublime text:

EDITOR

Page 11: Debugging Ruby (with Pry)

COMMAND PREFIX

A problem and a solution

Page 12: Debugging Ruby (with Pry)

COMMAND PREFIX

A more permanent solutionPry.config.command_prefix = ‘%’Now all pry commands must be prefixed with ‘%’

Page 13: Debugging Ruby (with Pry)

CUSTOM COMMANDS

Pry also allows you to define your own custom commands

By block or classUseful for often repeated code that shouldn’t be checked in

Page 14: Debugging Ruby (with Pry)

CUSTOM COMMANDS

Suppose you need to clean your database from time to time

Page 15: Debugging Ruby (with Pry)

CUSTOM COMMANDS

Programmers are lazy (in a good way)

We like our code DRY and expressive

Where is that laziness when it comes to the console?

Pry + custom commands = DRY, expressive console

Page 16: Debugging Ruby (with Pry)

Any gem whose name fits the format pry-<plugin> is a plugin

Beware: these gems are auto-loaded by pry

--no-plugins option will skip auto-loading

.pryrc files get loaded before pluginsPlugin specific suppression: Pry.plugins[“debugger”].disable!

PRY-ECOSYSTEM (PLUGINS)

Page 17: Debugging Ruby (with Pry)

Pry-debuggerWorks only in MRI 1.8 – 1.9

Pry-byebugA fork of pry-debuggerWorks in MRI 2.0 – 2.1

Pry-navPure ruby implementationWorks on all rubies

DEBUGGERS

Page 18: Debugging Ruby (with Pry)

Pry-stack-explorerGives pry commands: up, down, frame, and show-stack

Pry-rescue (Pry-exception_explorer has been deprecated)Allows you to rescue from exceptions into a pry session

Rescue for entire program, or only for specified blocks

Plymouth Jump into a pry session on test failure

WHEN THINGS BREAK

Page 19: Debugging Ruby (with Pry)

Uses regex magic to add some easy-access syntax hackery

Accessing instance variablesfoo.@instance_var

Calling private methodsfoo.!private_meth()

Examining things in parent pry sessionsputs ../local_var

Just be careful with your regexes

PRY-SYNTAX-HACKS

Page 20: Debugging Ruby (with Pry)

Pry-docAdds core ruby class documentation for “?” and “$” commands

Pry-vterm_aliasesMakes your ZSH and Bash aliases available to pry’s .<shell> command

Pry-themeColor themes for pry’s syntax highlighting

MISCELLANEOUS PLUGINS

Page 21: Debugging Ruby (with Pry)

Pry-railsTurns rails console into a pry sessionTeaches Pry new commands

show-[middleware|model|models|route] recognize-path

Pry-plusA pre-packaged group of useful plugins

INTEGRATION

Page 22: Debugging Ruby (with Pry)

Puts is ok. But don’t hesitate to drop back and use the big guns

REPL driven programming is incredibly powerfulEspecially when working with new code/library

Explore codeBundle open your favorite gem when it bugs out (or just cd into it)

You never know what you’ll find

CONCLUSION