debugging ruby (with pry)

Post on 10-May-2015

2.043 Views

Category:

Technology

2 Downloads

Preview:

Click to see full reader

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

Or: How

I Learne

dTo

StopPutsing

AndLovePry

DEBUGGING RUBY

ABOUT ME

Luke BergenTwitter: @lbergen

Reviewed.com

Developers spend 60% of their time debugging

puts doesn’t always do the trick

Enter pry and friends

DEBUGGING IN RUBY

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

Three major piecesPry-corePry-customPry-ecosystem

WHAT IS PRY?

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

ExpandableRegex based

PRY-CORE

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

source)edit

COMMANDS

lscdamend-line

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

__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

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

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

PRY-CUSTOM

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

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

COMMAND PREFIX

A problem and a solution

COMMAND PREFIX

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

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

CUSTOM COMMANDS

Suppose you need to clean your database from time to time

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

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)

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

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

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

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

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

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

top related