如何从命令行获取Ruby文档[重复]

时间:2022-12-14 00:11:30

This question already has an answer here:

这个问题在这里已有答案:

Is there a way to find out which part of my ri command that is not showing Ruby's documentation:

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

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?

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

4 个解决方案

#1


26  

If you're using RVM to manage your Ruby installations you can do this:

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

rvm docs generate

If not, try doing this:

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

gem install rdoc-data
rdoc-data --install

then try the ri command again.

然后再次尝试ri命令。

#2


11  

With pry, it's better to install the pry-doc gem, and then use the show-doc command:

使用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)> 

Note: you can also use the ? alias for show-doc if you prefer.

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

#3


3  

You mentioned in a comment that you're using the Ruby package from archlinux's package manager. What you need for ri is to install the ruby-docs package:

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

$ pacman -S ruby-docs

I guess they separate the packages so people who don't want the docs can save on disk usage.

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

#4


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?

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

Well, there are no methods String.split or String.strip. There are, however, methods String#split and String#strip. Try asking for those, and you'll probably get their documentation.

好吧,没有方法String.split或String.strip。但是,有一些方法String#split和String#strip。试着要求那些,你可能会得到他们的文件。

#1


26  

If you're using RVM to manage your Ruby installations you can do this:

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

rvm docs generate

If not, try doing this:

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

gem install rdoc-data
rdoc-data --install

then try the ri command again.

然后再次尝试ri命令。

#2


11  

With pry, it's better to install the pry-doc gem, and then use the show-doc command:

使用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)> 

Note: you can also use the ? alias for show-doc if you prefer.

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

#3


3  

You mentioned in a comment that you're using the Ruby package from archlinux's package manager. What you need for ri is to install the ruby-docs package:

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

$ pacman -S ruby-docs

I guess they separate the packages so people who don't want the docs can save on disk usage.

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

#4


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?

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

Well, there are no methods String.split or String.strip. There are, however, methods String#split and String#strip. Try asking for those, and you'll probably get their documentation.

好吧,没有方法String.split或String.strip。但是,有一些方法String#split和String#strip。试着要求那些,你可能会得到他们的文件。