Skip to content

Instantly share code, notes, and snippets.

@skipcloud
Last active January 27, 2020 08:46
Show Gist options
  • Select an option

  • Save skipcloud/2e71e20811be098d537544cdca71a7c1 to your computer and use it in GitHub Desktop.

Select an option

Save skipcloud/2e71e20811be098d537544cdca71a7c1 to your computer and use it in GitHub Desktop.
Working with Pry

This one is for the Ruby users. irb is the REPL that comes with Ruby, there is another one called Pry which is very popular. In fact when you SSH into Orderweb this is the REPL you get put into. It has some fun tricks that make poking around in code much easier. In my code examples >> is the pry prompt.

Editing code [1]

This one is better explained with a video but the basics of it is if you have the EDITOR environment variable set export EDITOR=vim or set it in your .pryrc Pry.config.editor = 'vim' you can then edit code. the edit command will open your text editor at a blank file saved in /tmp and you can write some ruby, save it and exit and it will bring you back to pry where your code will have been loaded in. If you edit again it will open the tmp file back up. The cool thing is you can edit specific methods with edit <method>. This one won't work on production containers but it's handy for local development.

>> edit

*inside vim*
def hi
  puts "hi"
end
*save and exit*

=> :hi
>> hi
"hi"
>> edit

*file opens up again*
def hi
  puts "hi there"
end
*save and exit*

=> :hi
>> hi
"hi there"

Viewing source code [2]

Rails is notorious for having magic methods that you have no idea where they come from, but Pry will tell you where the code is. Use the command show-source (alias $) with the name of the method you are after. This command has a whole bunch of options you can pass to it, such as -l to include line numbers and -a to show you all the source for all definitions it finds.

>> $ hi

From: pry-redefined(0x2b097969ab9c#hi) @ line 1:
Owner: Object
Visibility: public
Number of lines: 3

def hi
  "hi"
end
>> $ -l puts

From: io.c (C Method):
Owner: Kernel
Visibility: private
Number of lines: 8

1: static VALUE
2: rb_f_puts(int argc, VALUE *argv, VALUE recv)
3: {
4:     if (recv == rb_stdout) {
5:      return rb_io_puts(argc, argv, recv);
6:     }
7:     return rb_funcallv(rb_stdout, rb_intern("puts"), argc, argv);
8: }

Input [3]

This is a small one but it's really handy. Have you ever been coding in pry and got a few levels of indentation deep only to mess up then get stuck trying to get out of it back to the top level? Well you should really give the edit command a go but if you are in this predicament type ! to clear the input buffer

>> def hi
 |   if true
 |     if false
 |       thing
 |       oh
 |     end
 |     oops I've messed up
 | damn
 | !
Input buffer cleared!
>>

There's a TON of other features in that gem, check out the wiki.

[1] https://github.com/pry/pry/wiki/Editor-integration [2] https://github.com/pry/pry/wiki/Source-browsing [3] https://github.com/pry/pry/wiki/User-Input#clear-the-input-buffer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment