简体   繁体   English

Ruby相当于Python的帮助()?

[英]Ruby equivalent to Python's help()?

When working in interactive Python, I tend to rely on the built-in help() function to tell me what something expects and/or returns, and print out any documentation that might help me. 在交互式Python中工作时,我倾向于依赖内置的help()函数来告诉我期望和/或返回的内容,并打印出可能对我有帮助的任何文档。 Is there a Ruby equivalent to this function? 有没有Ruby相当于这个功能?

I'm looking for something I could use in irb. 我正在寻找一些我可以在irb中使用的东西。 For example, in interactive Python I could type: 例如,在交互式Python中我可以输入:

>>> help(1)

which would then print 然后打印

Help on int object:

class int(object)  |  int(x[, base])
-> integer  |    |  

Convert a string or number to an integer, if possible. A ...

Try using ri from the command line. 尝试从命令行使用ri

It takes a class name, method, or module as an argument, and gives you appropriate documentation. 它将类名,方法或模块作为参数,并为您提供适当的文档。 Many popular gems come with this form of documentation, as well, so it should typically work even beyond the scope of core Ruby modules. 许多流行的宝石也带有这种形式的文档,因此它通常应该超出核心Ruby模块的范围。

It's definitely a poor cousin to iPython's help, and one of the main features I miss after moving to Ruby, but you can also use ri from within irb . 对于iPython的帮助来说,这绝对是一个可怜的表兄弟,也是我转移到Ruby后我想念的主要功能之一,但你也可以在irb使用ri I'd recommend the wirble gem as an easy way to set this up. 我推荐使用wirble gem作为设置它的简单方法。

It's now late 2014 and here's the two ways to get the Python help() *similarity, as long as you have the Ruby Docs installed: 它现在已经是2014年末,只要您安装了Ruby Docs ,就可以通过以下两种方式获得Python help() *相似性:

  1. From inside irb , You can call the help method with a string describing what you're looking for. irb内部,您可以使用描述您要查找的内容的字符串调用help方法。

    Example 1: help 'Array' for the Array class 示例1:为Array类提供help 'Array'
    Example 2: help 'Array#each' for the Array class each method. 示例2: help 'Array#each'Array类的each方法。

  2. From the command line, outside of irb , you can use the ri program: 从命令行, irb 之外 ,您可以使用ri程序:

    Example 1: ri 'Array' for the Array class 示例1:Array类ri 'Array'
    Example 2: ri 'Array#each' for the Array class each method. 示例2: each方法的Arrayri 'Array#each'

* Not quite as good as Python's, but still better than nothing *不如Python那么好,但总比没有好

There's supposed to be irb_help . 应该是irb_help But like mentioned in that post, it's broken in my ruby install as well. 但就像那篇文章中提到的那样,它在我的ruby安装中也被打破了。

For quick shell access to ruby documentation, just type ri followed by the method you're wanting to learn more about (from your shell). 要快速访问ruby文档,只需键入ri,然后输入您想要了解更多信息的方法(从shell开始)。

For example: 例如:

ri puts

This must be fired up in your shell, not your irb (interactive ruby environment) 这必须在你的shell中启动,而不是你的irb(交互式ruby环境)

If you're in your irb environment, then another way, is to simply type help followed by the method you want to learn more about as follows: 如果您处于irb环境中,那么另一种方法是简单地键入help,然后输入您想要了解的方法,如下所示:

help puts

However, this assumes that you have configured your Ruby environment correctly for that (help) to work properly within irb. 但是,这假设您已正确配置Ruby环境(帮助)以在irb中正常工作。 I usually just have another shell open, and just use the ri directly for quick access when I'm in doubt about a certain method or arguments to a method. 我通常只打开另一个shell,当我对方法的某个方法或参数有疑问时,只需直接使用ri进行快速访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM