阅读 irb 中的文档

问题描述 投票:0回答:4

我怀念 ipython 的一件事是它有一个 ?挖掘特定函数的文档的运算符。

我知道ruby有类似的命令行工具,但是当我在irb中调用它非常不方便。

ruby/irb 有类似的东西吗?

ruby-on-rails ruby irb
4个回答
7
投票

Pry 是 IPython 的 Ruby 版本,它支持

?
命令来查找方法文档,但使用的语法略有不同:

pry(main)> ? File.dirname

From: file.c in Ruby Core (C Method):
Number of lines: 6

visibility:  public
signature:  dirname()

Returns all components of the filename given in file_name
except the last one. The filename must be formed using forward
slashes (/'') regardless of the separator used on the
local file system.

   File.dirname("/home/gumby/work/ruby.rb")   #=> "/home/gumby/work"

您还可以使用

$
命令查找源代码:

pry(main)> $ File.link

From: file.c in Ruby Core (C Method):
Number of lines: 14

static VALUE
rb_file_s_link(VALUE klass, VALUE from, VALUE to)
{
    rb_secure(2);
    FilePathValue(from);
    FilePathValue(to);
    from = rb_str_encode_ospath(from);
    to = rb_str_encode_ospath(to);

    if (link(StringValueCStr(from), StringValueCStr(to)) < 0) {
    sys_fail2(from, to);
    }
    return INT2FIX(0);
}

请参阅 http://pry.github.com 了解更多信息:)


6
投票

您可以从

开始
irb(main):001:0> `ri Object`

尽管其输出不太可读。您需要过滤掉一些元字符。

事实上,已经有人为它制作了宝石

gem install ori

然后在irb

irb(main):001:0> require 'ori'
=> true
irb(main):002:0> Object.ri
Looking up topics [Object] o
= Object < BasicObject

------------------------------------------------------------------------------
= Includes:
Java (from gem activesupport-3.0.9)

(from gem activesupport-3.0.9) [...]

3
投票

不,不是。 Python 有文档字符串:

def my_method(arg1,arg2):
  """ What's inside this string will be made available as the __doc__ attribute """
  # some code

因此,当从 ipython 调用

?
时,它可能会调用对象上的
__doc__
属性。 Ruby 没有这个。


0
投票
$ irb
irb(main):001:0> help 'File#read'
= File#read

(from ruby core)
=== Implementation from IO
------------------------------------------------------------------------
  ios.read([length [, outbuf]])    -> string, outbuf, or nil

------------------------------------------------------------------------

Reads length bytes from the I/O stream.

length must be a non-negative integer or nil.

If length is a positive integer, read tries to read
length bytes without any conversion (binary mode). It

...
© www.soinside.com 2019 - 2024. All rights reserved.