如何从命令行获取Ruby文档

21

有没有办法找出我使用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:

 $ 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

我应该做什么才能使文档显示出来?

4个回答

30

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

rvm docs generate

如果还没有成功,请尝试以下操作:

gem install rdoc-data
rdoc-data --install

然后再次尝试使用ri命令。


不,我是使用ArchLinux的软件包管理器中的Ruby。 - Kokizzu
好的,我会尝试第二个 :3 谢谢 - Kokizzu
1
正如我在我的回答中所提到的,当使用Pry时最好使用pry-doc宝石,然后使用show-doc - horseyguy

12

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


3

您在评论中提到正在使用archlinux的软件包管理器中的Ruby软件包。对于 ri ,您需要安装ruby-docs软件包:

$ pacman -S ruby-docs

我猜他们把软件包和文档分开,这样不需要文档的人可以节省磁盘空间。


2

When I use pry:

$ 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

What should I do to make the documentation appear?

好的,没有方法String.splitString.strip。但是有String#splitString#strip方法。尝试请求这些方法,你可能会得到它们的文档。


啊,是的,在 pry 中 . 和 # 都无法使用,但在普通的 ri 命令行中都可以使用。 - Kokizzu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接