Skip to content

Instantly share code, notes, and snippets.

@BanzaiMan
Last active January 30, 2018 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BanzaiMan/d39b70db38b4bcded6dbc6d35ea0b95c to your computer and use it in GitHub Desktop.
Save BanzaiMan/d39b70db38b4bcded6dbc6d35ea0b95c to your computer and use it in GitHub Desktop.
A few `pry` tricks I learned from @JoshCheek

Pry tricks

ls [-v] obj

Lists methods defined on obj.

[1] pry(main)> str = "foobar"
=> "foobar"
[2] pry(main)> ls str
Comparable#methods: <  <=  >  >=  between?
String#methods: 
  %    []=          center      concat     each_char       force_encoding  index    match      reverse!    setbyte      squeeze!     swapcase   tr             
  *    ascii_only?  chars       count      each_codepoint  freeze          insert   next       rindex      shell_split  start_with?  swapcase!  tr!            
  +    b            chomp       crypt      each_line       getbyte         inspect  next!      rjust       shellescape  strip        to_c       tr_s           
  <<   bytes        chomp!      delete     empty?          getord          intern   oct        rpartition  shellsplit   strip!       to_f       tr_s!          
  <=>  bytesize     chop        delete!    encode          gsub            length   ord        rstrip      size         sub          to_i       unpack         
  ==   byteslice    chop!       downcase   encode!         gsub!           lines    partition  rstrip!     slice        sub!         to_r       upcase         
  ===  capitalize   chr         downcase!  encoding        hash            ljust    prepend    scan        slice!       succ         to_s       upcase!        
  =~   capitalize!  clear       dump       end_with?       hex             lstrip   replace    scrub       split        succ!        to_str     upto           
  []   casecmp      codepoints  each_byte  eql?            include?        lstrip!  reverse    scrub!      squeeze      sum          to_sym     valid_encoding?

It does not show all the methods defined at each level of class hierarchy. For this, use -v:

[3] pry(main)> ls -v str
BasicObject#methods: !  !=  __id__  __send__  equal?  instance_eval  instance_exec
Kernel#methods: 
  !~        define_singleton_method  frozen?                     instance_variables  nil?               public_method             send               tainted?  untrust   
  byebug    display                  instance_of?                is_a?               object_id          public_methods            singleton_class    tap       untrusted?
  class     dup                      instance_variable_defined?  kind_of?            pretty_inspect     public_send               singleton_method   to_enum 
  clone     enum_for                 instance_variable_get       method              private_methods    remove_instance_variable  singleton_methods  trust   
  debugger  extend                   instance_variable_set       methods             protected_methods  respond_to?               taint              untaint 
PP::ObjectMixin#methods: pretty_print  pretty_print_cycle  pretty_print_inspect  pretty_print_instance_variables
Object#methods: __binding__  pry  pry_remote  remote_pry
Comparable#methods: <  <=  >  >=  between?
String#methods: 
  %    []=          center      concat     each_char       force_encoding  index    match      reverse!    setbyte      squeeze!     swapcase   tr             
  *    ascii_only?  chars       count      each_codepoint  freeze          insert   next       rindex      shell_split  start_with?  swapcase!  tr!            
  +    b            chomp       crypt      each_line       getbyte         inspect  next!      rjust       shellescape  strip        to_c       tr_s           
  <<   bytes        chomp!      delete     empty?          getord          intern   oct        rpartition  shellsplit   strip!       to_f       tr_s!          
  <=>  bytesize     chop        delete!    encode          gsub            length   ord        rstrip      size         sub          to_i       unpack         
  ==   byteslice    chop!       downcase   encode!         gsub!           lines    partition  rstrip!     slice        sub!         to_r       upcase         
  ===  capitalize   chr         downcase!  encoding        hash            ljust    prepend    scan        slice!       succ         to_s       upcase!        
  =~   capitalize!  clear       dump       end_with?       hex             lstrip   replace    scrub       split        succ!        to_str     upto           
  []   casecmp      codepoints  each_byte  eql?            include?        lstrip!  reverse    scrub!      squeeze      sum          to_sym     valid_encoding?

show-source method

Shows the source of the method. It can show C source:

[4] pry(main)> show-source str.clear

From: string.c (C Method):
Owner: String
Visibility: public
Number of lines: 13

static VALUE
rb_str_clear(VALUE str)
{
    str_discard(str);
    STR_SET_EMBED(str);
    STR_SET_EMBED_LEN(str, 0);
    RSTRING_PTR(str)[0] = 0;
    if (rb_enc_asciicompat(STR_ENC_GET(str)))
        ENC_CODERANGE_SET(str, ENC_CODERANGE_7BIT);
    else
        ENC_CODERANGE_SET(str, ENC_CODERANGE_VALID);
    return str;
}

or Ruby:

[5] pry(main)> class A; def foo; puts "foo"; end; end
=> :foo
[6] pry(main)> show-source A.new.foo

From: (pry) @ line 2:
Owner: A
Visibility: public
Number of lines: 1

class A; def foo; puts "foo"; end; end

edit [-p] method

It is possible to edit a method while you are executing the program. If the method is written in Ruby and exists on disk, then this command will open the file with a text editor and lets you edit it. When you save the file, it will be immediately available in the current pry session.

The -p flag is short for "patch". It allows modification of the method in-memory; no changes are written to disk.

~/.pryrc

Configuration file.

Pry.editor

Defines the text editor to use with edit. If unset, pry uses something basic… on my Mac, it is nano.

I use Sublime, and for this, I can set:

Pry.editor='subl -w'

.<CMD>

Runs CMD in the shell, and displays output on the console.

[1] pry(main)> .date
Fri Jul  1 00:50:58 EDT 2016
[2] pry(main)> .TZ=HST date
Thu Jun 30 18:52:40 HST 2016

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