首先你需要安装
gem install pry-doc
然后您可以使用命令获取文档show-doc [method]
(别名为? [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"
show-source [method]
您甚至可以使用命令(别名为$ [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;
}
这个例子显示了 C 源代码,但如果有的话,它会显示实际的 Ruby 源代码。考虑这个简单的类:
pry> class Foo
pry* def bar
pry* puts 'hello'
pry* end
pry* end
=> nil
你可以看看全班:
pry> show-source Foo
From: (pry) @ line 2:
Class name: Foo
Number of lines: 5
class Foo
def bar
puts 'hello'
end
end
但也只是一种特定的方法:
pry> show-source Foo#bar
From: (pry) @ line 3:
Owner: Foo
Visibility: public
Number of lines: 3
def bar
puts 'hello'
end
正如@banister 建议的那样,您可以通过添加自定义命令Pry.commands.command
。这样你就可以?
在??
你的~/.pryrc
:
Pry.commands.command /(.+) \?\z/ do |a|
run "show-doc", a
end
Pry.commands.command /(.+) \?\?\z/ do |a|
run "show-source", a
end
请注意,我们需要在 method 和 之间留一个空格?
,因为 Ruby 方法可能以?
(例如Fixnum#zero?
) 结尾,而这些方法会中断。一些例子:
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.