【问题标题】:How to get the Ruby documentation from the command line [duplicate]如何从命令行获取 Ruby 文档 [重复]
【发布时间】:2013-03-19 19:09:34
【问题描述】:

有没有办法找出我的 ri 命令的哪一部分没有显示 Ruby 的文档:

 $ ruby --version
 ruby 1.9.3p392 (2013-02-22 revision 39386) [i686-linux]

 $ ri --version
 ri 3.12.2     

 $ ri String
 Nothing known about String

当我使用撬时:

 $ pry --version
 Pry version 0.9.12 on Ruby 1.9.3

 $ pry 
 [1] pry(main)> ri String
 # shows String documentation
 [2] pry(main)> ri String.split
 error: 'String.split' not found
 [3] pry(main)> ri String.strip
 String.strip not found, maybe you meant:
 String#strip_heredoc

我应该怎么做才能使文档出现?

【问题讨论】:

    标签: ruby ri


    【解决方案1】:

    如果您使用 RVM 来管理您的 Ruby 安装,您可以这样做:

    rvm docs generate
    

    如果没有,请尝试这样做:

    gem install rdoc-data
    rdoc-data --install
    

    然后再次尝试ri 命令。

    【讨论】:

    • 不,我正在使用来自 archlinux 包管理器的 ruby​​
    • 好的,我会尝试第二个:3 谢谢
    • 是的,它可以工作:3 谢谢
    • 正如我在回答中所说,使用 pry 时,最好先使用 pry-doc gem,然后再使用 show-doc
    【解决方案2】:

    您在评论中提到您正在使用来自 archlinux 的包管理器的 Ruby 包。 ri 需要的是安装 ruby-docs 包:

    $ pacman -S ruby-docs
    

    我猜他们将包分开,这样不需要文档的人就可以节省磁盘使用量。

    【讨论】:

      【解决方案3】:

      当我使用撬时:

      $ pry --version
      Pry version 0.9.12 on Ruby 1.9.3
      
      $ pry 
      [1] pry(main)> ri String
      # shows String documentation
      [2] pry(main)> ri String.split
      error: 'String.split' not found
      [3] pry(main)> ri String.strip
      String.strip not found, maybe you meant:
      String#strip_heredoc
      

      我应该怎么做才能使文档出现?

      嗯,没有方法String.splitString.strip。但是,有String#splitString#strip 方法。尝试询问这些,您可能会得到他们的文档。

      【讨论】:

      • 啊,是的,两者都有。和 # 在 pry 时不工作,但在正常的 ri comamnd 行中都工作
      【解决方案4】:

      使用pry,最好安装pry-doc gem,然后使用show-doc 命令:

      [17] pry(main)> show-doc String#inspect
      
      From: string.c (C Method):
      Owner: String
      Visibility: public
      Signature: inspect()
      Number of lines: 6
      
      Returns a printable version of _str_, surrounded by quote marks,
      with special characters escaped.
      
         str = "hello"
         str[3] = "\b"
         str.inspect       #=> "\"hel\\bo\""
      [18] pry(main)> show-doc Array#pop
      
      From: array.c (C Method):
      Owner: Array
      Visibility: public
      Signature: pop(*arg1)
      Number of lines: 11
      
      Removes the last element from self and returns it, or
      nil if the array is empty.
      
      If a number n is given, returns an array of the last n elements
      (or less) just like array.slice!(-n, n) does. See also
      Array#push for the opposite effect.
      
         a = [ "a", "b", "c", "d" ]
         a.pop     #=> "d"
         a.pop(2)  #=> ["b", "c"]
         a         #=> ["a"]
      [19] pry(main)> 
      

      注意:如果您愿意,也可以为 show-doc 使用 ? 别名。

      【讨论】:

        猜你喜欢
        • 2015-08-13
        • 1970-01-01
        • 1970-01-01
        • 2016-06-09
        • 2020-07-11
        • 2015-07-09
        • 2010-09-16
        • 2013-02-04
        • 1970-01-01
        相关资源
        最近更新 更多