Search code examples
pythonrubyirbrdocdocstring

Ruby inline documentation


In IRB, or other interactive interpreter such as pry, how can I get some inline documentation on objects and methods? For example, I can get this far:

[1] pry(main)> x = 'hello world'
=> "hello world"
[2] pry(main)> x.st
x.start_with?  x.strip        x.strip!   
[2] pry(main)> x.st

But now I want to read usage / interface / whatever rdoc has to say about those methods and their inteface. That middle line was tab-completion, by the way.

I'm looking for something similar to ipython, where ? can be appended to an attribute name to see the docstring, or even an ?? to see the source:

In [1]: x = 'potato'

In [2]: x.st
x.startswith  x.strip       

In [2]: x.strip?
Type:       builtin_function_or_method
String Form:<built-in method strip of str object at 0x15e1b10>
Docstring:
S.strip([chars]) -> string or unicode

Return a copy of the string S with leading and trailing
whitespace removed.
If chars is given and not None, remove characters in chars instead.
If chars is unicode, S will be converted to unicode before stripping

Solution

  • First you need to install

    gem install pry-doc
    

    Then you can get documentation with the show-doc [method] command (aliased to ? [method])

    pry> x = 'potato'
    => "potato"
    pry> show-doc x.strip
    
    From: string.c (C Method):
    Owner: String
    Visibility: public
    Signature: strip()
    Number of lines: 4
    
    Returns a copy of str with leading and trailing whitespace removed.
    
       "    hello    ".strip   #=> "hello"
       "\tgoodbye\r\n".strip   #=> "goodbye"
    

    You can even look at the source code with the show-source [method] command (aliased to $ [method])

    pry> show-source x.strip
    
    From: string.c (C Method):
    Owner: String
    Visibility: public
    Number of lines: 7
    
    static VALUE
    rb_str_strip(VALUE str)
    {
        str = rb_str_dup(str);
        rb_str_strip_bang(str);
        return str;
    }
    

    This example shows C source, but it will show you the actual Ruby source if there is any. Consider this simple class:

    pry> class Foo
    pry*   def bar
    pry*     puts 'hello'
    pry*   end
    pry* end
    => nil
    

    You can look at the whole class:

    pry> show-source Foo
    
    From: (pry) @ line 2:
    Class name: Foo
    Number of lines: 5
    
    class Foo
      def bar
        puts 'hello'
      end
    end
    

    But also at just a specific method:

    pry> show-source Foo#bar
    
    From: (pry) @ line 3:
    Owner: Foo
    Visibility: public
    Number of lines: 3
    
    def bar
      puts 'hello'
    end
    

    As @banister suggested, you may add custom commands via Pry.commands.command. This way you can define your ? and ?? commands like this in your ~/.pryrc:

    Pry.commands.command /(.+) \?\z/ do |a|
      run "show-doc", a
    end
    
    Pry.commands.command /(.+) \?\?\z/ do |a|
      run "show-source", a
    end
    

    Note that we need a space between the method and the ?, because Ruby methods may end in ? (e.g. Fixnum#zero?) and those methods would break. Some examples:

    pry> puts ?
    
    From: io.c (C Method):
    Owner: Kernel
    Visibility: private
    Signature: puts(*arg1)
    Number of lines: 3
    
    Equivalent to
    
        $stdout.puts(obj, ...)
    

     

    pry> puts ??
    
    From: io.c (C Method):
    Owner: Kernel
    Visibility: private
    Number of lines: 8
    
    static VALUE
    rb_f_puts(int argc, VALUE *argv, VALUE recv)
    {
        if (recv == rb_stdout) {
            return rb_io_puts(argc, argv, recv);
        }
        return rb_funcall2(rb_stdout, rb_intern("puts"), argc, argv);
    }
    

     

    pry> 0.zero?     # still works!
    => true
    
    pry> 0.zero? ?
    
    From: numeric.c (C Method):
    Owner: Fixnum
    Visibility: public
    Signature: zero?()
    Number of lines: 1
    
    Returns true if fix is zero.