在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
但是现在我想阅读usage/interface/rdoc对这些方法及其接口的描述。顺便说一下,中间那条线是制表符补全。在
我在寻找类似于ipython的东西,其中可以将?
附加到属性名以查看docstring,或者甚至可以添加一个??
来查看源代码:
首先需要安装
然后可以使用
^{pr2}$show-doc [method]
命令(别名为? [method]
)获取文档您甚至可以使用
show-source [method]
命令(别名为$ [method]
)查看源代码这个例子显示了C源代码,但是如果有Ruby源代码,它会显示实际的Ruby源代码。考虑这个简单的类:
你可以看看整个班级:
但也只是一种特定的方法:
正如@banister建议的那样,您可以通过
Pry.commands.command
添加自定义命令。这样您就可以在~/.pryrc
中定义?
和??
命令:注意,我们在方法和
?
之间需要一个空格,因为Ruby方法可能以?
(例如Fixnum#zero?
)结尾,这些方法将中断。一些例子:在
在
相关问题 更多 >
编程相关推荐