如何从命令行获取Ruby文档

有没有办法找出我的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 

我该怎么做才能显示文档?

如果您使用RVM来管理Ruby安装,则可以执行以下操作:

 rvm docs generate 

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

 gem install rdoc-data rdoc-data --install 

然后再次尝试ri命令。

使用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别名。

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

 $ pacman -S ruby-docs 

我猜他们将这些软件包分开,所以不想要文档的人可以节省磁盘使用量。

当我使用撬:

 $ 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 。 试着要求那些,你可能会得到他们的文件。