【问题标题】:Ruby inline documentationRuby 内联文档
【发布时间】:2013-05-29 08:02:24
【问题描述】:

在 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 关于这些方法及其接口的任何内容。顺便说一句,那条中间线是制表符完成。

我正在寻找类似于 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

【问题讨论】:

    标签: python ruby irb rdoc docstring


    【解决方案1】:

    首先你需要安装

    gem install pry-doc
    

    然后您可以使用show-doc [method] 命令(别名为? [method])获取文档

    pry> x = 'potato'
    => "potato"
    pry> show-doc x.strip
    
    From: string.c (C Method):
    Owner: String
    Visibility: public
    Signature: strip()
    Number of lines: 4
    
    Returns a copy of str with leading and trailing whitespace removed.
    
       "    hello    ".strip   #=> "hello"
       "\tgoodbye\r\n".strip   #=> "goodbye"
    

    您甚至可以使用show-source [method] 命令(别名为$ [method])查看源代码

    pry> show-source x.strip
    
    From: string.c (C Method):
    Owner: String
    Visibility: public
    Number of lines: 7
    
    static VALUE
    rb_str_strip(VALUE str)
    {
        str = rb_str_dup(str);
        rb_str_strip_bang(str);
        return str;
    }
    

    这个例子显示了 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, ...)
    

     

    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.
    

    【讨论】:

    • 非常酷,谢谢。你能破解它,让它成为某种后修复运算符(最好是内联打印,类似于制表符完成)而不是命令? irb有类似的吗?
    • 恐怕我不知道如何实现这种行为。您可以使用第三方插件在 IRB 中获得其中的一些内容,请参阅stackoverflow.com/questions/3049678/ruby-irb-self-help。但是我在回答中提到的功能是我改用 PRY 的原因。此外,PRY 有语法高亮和各种其他很酷的东西,例如编辑和动态替换代码、编辑最后一行、在 externl 编辑器中打开……我真的推荐 PRY。
    • 您也可以使用? 命令(这是show-doc 的别名,例如? String#split
    • 顺便说一句,你可以简单地添加你自己的“post-fix”命令,试着把它放在你的.pryrc中:Pry.commands.command /(.+)--/ do |a|;运行“show-doc”,一个;结尾;然后在 pry 中运行: pry(main)> Pry-- (这应该显示 Pry 类的源代码)
    • 在较新版本的pry 中,show-doc 命令似乎计划弃用。考虑改用show-source &lt;YOUR_METHOD_HERE&gt; --docWARNING: the show-doc command is deprecated. It will be removed from future Pry versions. Please use 'show-source' with the -d (or --doc) switch instead
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-18
    • 1970-01-01
    相关资源
    最近更新 更多