【问题标题】:Any way, in the console, to get info on what a certain method does?有什么方法可以在控制台中获取有关某种方法的作用的信息?
【发布时间】:2014-06-24 12:25:52
【问题描述】:

假设我使用 gem csv

通过控制台中的以下内容,我得到了所有实例方法的列表:

require "csv"
csv = CSV.open(FILE_NAME, "a+")
csv.methods

我现在在列表中找到的一种方法是,例如first.

我可以从命令行获取有关此方法的信息吗? (是的,我知道我可以用谷歌搜索文档。)

【问题讨论】:

    标签: ruby command-line console gem


    【解决方案1】:

    Pry(IRB 替代方案)支持documentation browsing

    $ pry
    [1] pry(main)> require 'csv'
    #=> true
    
    [2] pry(main)> csv = CSV.new('')
    #=> <#CSV io_type:StringIO encoding:UTF-8 lineno:0 col_sep:"," row_sep:"\n" quote_char:"\"">
    
    [3] pry(main)> show-doc csv.first
    
    From: enum.c (C Method):
    Owner: Enumerable
    Visibility: public
    Signature: first(*arg1)
    Number of lines: 8
    
    Returns the first element, or the first n elements, of the enumerable.
    If the enumerable is empty, the first form returns nil, and the
    second form returns an empty array.
    
      %w[foo bar baz].first     #=> "foo"
      %w[foo bar baz].first(2)  #=> ["foo", "bar"]
      %w[foo bar baz].first(10) #=> ["foo", "bar", "baz"]
      [].first                  #=> nil
    

    根据文档,您不必事先生成文档:

    Pry 文档系统不依赖于预先生成的 rdocri,而是根据需要直接在方法上方抓取 cmets。 这导致更快的文档检索并允许 Pry 系统检索不会选择的方法的文档 rdoc.

    【讨论】:

    • 这可能是因为您必须安装 Pry。值得注意的是。
    【解决方案2】:

    最简单的方法大概就是ri

    $ ri 'CSV#first'
    
    = CSV#first
    
    (from ruby core)
    === Implementation from Enumerable
    ------------------------------------------------------------------------------
      enum.first       ->  obj or nil
      enum.first(n)    ->  an_array
    
    
    ------------------------------------------------------------------------------
    
    Returns the first element, or the first n elements, of the enumerable. If the
    enumerable is empty, the first form returns nil, and the second form returns
    an empty array.
    
      %w[foo bar baz].first     #=> "foo"
      %w[foo bar baz].first(2)  #=> ["foo", "bar"]
      %w[foo bar baz].first(10) #=> ["foo", "bar", "baz"]
      [].first                  #=> nil
    

    您可能需要先安装文档,如:How to get the Ruby documentation from the command line

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-26
      • 2019-01-11
      • 1970-01-01
      • 2019-10-06
      • 2020-08-17
      • 1970-01-01
      • 2020-12-28
      相关资源
      最近更新 更多