Python Ruby内联文档
在IRB或其他交互式解释器(如pry)中,如何获取有关对象和方法的内联文档?例如,我可以做到这一点:Python Ruby内联文档,python,ruby,irb,rdoc,docstring,Python,Ruby,Irb,Rdoc,Docstring,在IRB或其他交互式解释器(如pry)中,如何获取有关对象和方法的内联文档?例如,我可以做到这一点: [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 但现在我想读一下用法/接口/rdoc对这些方法及其接口的任何看法。顺便说一句,中间这一行是制表符完成 我正在
[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
但现在我想读一下用法/接口/rdoc对这些方法及其接口的任何看法。顺便说一句,中间这一行是制表符完成
我正在寻找类似于ipython的东西,其中可以将?
附加到属性名称以查看文档字符串,甚至可以将?
附加到属性名称以查看源代码:
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
[1]中的x='potato'
In[2]:x.st
x、 从x.strip开始
在[2]:x.strip?
类型:内置函数或方法
字符串形式:
文档字符串:
S.strip([chars])->字符串或unicode
返回带前导和尾随的字符串S的副本
删除空白。
如果给定了字符而不是无,则删除字符中的字符。
如果字符是unicode,则在剥离之前将字符转换为unicode
首先需要安装
gem install pry-doc
然后,您可以使用show doc[method]
命令(别名为?[method]
)获取文档
您甚至可以使用show source[method]
命令查看源代码(别名为$[method]
)
本例显示了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
请注意,我们需要在方法和?
之间留一个空格,因为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, ...)
很酷,谢谢。你能破解它,让它成为某种事后修复操作符(最好是内联打印,类似于制表符完成)而不是命令吗?在irb中有类似的东西吗?恐怕我不知道如何避免这种行为。您可以通过第三方插件在IRB中获得一些信息,请参阅。但我在回答中提到的特征正是我改用PRY的原因。此外,PRY还有语法突出显示和其他各种很酷的东西,比如编辑和动态替换代码、编辑最后一行、在外部编辑器中打开等等。。。我真的推荐PRY。你也可以使用
?
命令(这是show doc
的别名,例如?String#split
顺便说一句,你可以简单地添加你自己的“post-fix”命令,试着把它放在你的.pryrc:PRY.commands.command/(.+)--/do | a |;运行“show doc”,a;end;然后在PRY:PRY(main)>PRY中运行它(应显示Pry类的来源)在新版本的<代码> PRY < /代码>中,看起来像“代码>显示DOC 命令被指定为弃用。考虑使用<代码>显示源-DOC 改为:<代码>警告:显示DOC命令被禁止。它将从将来的PRY版本中删除。请使用-D(OR—DOC)开关“显示源”代替
Pry.commands.command /(.+) \?\z/ do |a|
run "show-doc", a
end
Pry.commands.command /(.+) \?\?\z/ do |a|
run "show-source", a
end
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.