阅读irb中的文档

dis*_*dng 5 ruby ruby-on-rails irb

我想念ipython的一件事是它有一个?操作员为特定功能挖掘文档.

我知道ruby有一个类似的命令行工具但是当我在irb时调用它非常不方便.

ruby/irb有类似的东西吗?

hor*_*guy 6

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"
Run Code Online (Sandbox Code Playgroud)

您还可以使用以下$命令查找源代码:

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);
}
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请访问http://pry.github.com :)


Mch*_*chl 5

你可以先开始

irb(main):001:0> `ri Object`
Run Code Online (Sandbox Code Playgroud)

虽然这个输出不是可读的.您需要过滤掉一些元字符.

事实上,有人已经为它了一个宝石

gem install ori
Run Code Online (Sandbox Code Playgroud)

然后在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) [...]
Run Code Online (Sandbox Code Playgroud)