3

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
wim
  • 338,267
  • 99
  • 616
  • 750

1 Answers1

7

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.
Patrick Oscity
  • 53,604
  • 17
  • 144
  • 168
  • Very cool, thanks. Can you hack it so that it's some sort of post-fix operator (preferably printing out inline, similar to a tab completion) rather than a command? Is there something similar in irb? – wim May 29 '13 at 08:48
  • I'm afraid i don't know how to acheive this behavior. You can get some of this in IRB with third-party plugins, see http://stackoverflow.com/questions/3049678/ruby-irb-self-help. But the features i mentioned in my answer were the reason i switched to PRY. Also, PRY has syntax highlighting and all sorts of other cool stuff such as editing and dynamically replacing code, edit last line, open in externl editor, ... I really recommend PRY. – Patrick Oscity May 29 '13 at 09:55
  • 2
    You can also use the `?` command (which is an alias for `show-doc`, e.g `? String#split` – horseyguy May 29 '13 at 11:01
  • 2
    BTW you can trivially add your own "post-fix" commands, try putting this n your .pryrc: Pry.commands.command /(.+)--/ do |a|; run "show-doc", a; end; then run this in pry: pry(main)> Pry-- (this should show the source for the Pry class) – horseyguy May 29 '13 at 11:08
  • In newer versions of `pry`, it looks like the `show-doc` command is scheduled for deprecation. Consider using `show-source --doc` instead: `WARNING: the show-doc command is deprecated. It will be removed from future Pry versions. Please use 'show-source' with the -d (or --doc) switch instead` – David Bodow Dec 12 '20 at 23:08