FizzBuzz Journal

Not really a blog, just some stuff that a future me might need to remember one day.

  1. Github
  2. Stackoverflow
  3. Twitter

Pry Notes

14 Sep 2016

Background

Pry is a great replacement for irb and is very useful if you ever need to do any debugging. It does a lot. It’s sometimes hard to remember all the things it does especially when you don’t create bugs that often.

Basics

Setting up Pry and using it as the Rails console

Set a breakpoint

require 'pry'; binding.pry;

Run the program and it will stop with a prompt when it hits the breakpoint. From here you can do everything that irb does and more.

Getting help

Pry has many commands that work like command line programs rather than Ruby methods. To get started and list the commands that are available in Pry and any installed plug-ins:

pry> help

or use the --help option to get detailed help on any command:

pry> ls --help

Exiting Pry

If you want to just continue running your program from a breakpoint:

pry> exit

This will only run until the next breakpoint so if you are stuck in a loop and you really want to just stop the program you need:

pry> exit-program

or

pry> !!!

Lost?

To get back to the code view

pry> whereami

or

pry> @

Sometimes you don’t see enough code, you can specify the line count either side of the breakpoint:

pry> @ 10

Call stack?

You need to include pry-backtrace in your Gemfile, then it’s:

pry> pry-backtrace

Viewing and editing previous commands

History

Pry stores every command you input and you can view these using the hist command:

[1] pry(main)> puts 'foo'
foo
=> nil
[2] pry(main)> hist
1: puts 'foo'
[3] pry(main)> puts 'foobar'
foobar
=> nil
[4] pry(main)> hist
1: puts 'foo'
2: hist
3: puts 'foobar'
[5] pry(main)>

The edit command

Inputting short commands in Pry is pretty convenient but what if you want to input a multi-line command such as defining a new method or you want to edit a long command that mistyped?

Fortunately Pry integrates with your default editor via the edit command. By default edit will open up the last command in your editor and then re-run it when you exit the editor after whatever changes you made. This works best if you use a terminal hosted editor like Vim.

If you need to edit a command earlier than the last then use the hist command to find out which line number it’s on and then use the `–in

` option (line-number is either a single line or a range). e.g. the following `edit` will open the `foo` method: [10] pry(main)> hist 1: puts 'hello' ... 5: def foo 6: puts 'foo' 7: end ... 10: puts 'world' [11] pry(main)> edit --in 5..7 ## Pry and the clipboard ### Copying output to the clipboard It is sometimes helpful to copy output from Pry to an external program. You can do this reasonably easily if you have the `gist` gem installed. This gem's primary use case is for creating Github gists from Pry but it also has an option to output to the clipboard. You can access this functionality using the `clipit` command which is available if you have `gist` in your bundle. The following copies the contents of the variable `foo` to the clipboard: pry> foo = 'foo' => foo pry> foo; pry> clipit -o You can also copy input lines using `clipit -i` in much the same way. Alternatively MacOS users can make use of `pbcopy` and `pbpaste` via a few tweaks to your `.pryrc`: # .pryrc def pbcopy(input) str = input.to_s IO.popen('pbcopy', 'w') { |f| f << str } str end Then you can just do: pry> foo = 'foo' => foo pry> pbcopy(foo) ### Copying multiple lines into Pry If you have a method or multi-line command in a source file you might want to copy and paste this straight into Pry. This can be troublesome because of the way Pry processes each line. Perhaps the easiest way around it is to use the built-in `edit` command to open an editor and paste the code in there before executing it. Alternatively you can add a `paste` command in your `.pryrc`: # .pryrc def pbpaste `pbpaste` end Pry::Commands.block_command( 'paste', 'Pastes from the clipboard then evals it in the context of Pry' ) do _pry_.input = StringIO.new(pbpaste) end ## Shell commands You can use the `.` operator to execute a shell command, e.g. pry> .ls