How to get the Ruby documentation from the command line [duplicate]

时光总嘲笑我的痴心妄想 提交于 2019-12-03 05:53:06

问题


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

 $ 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?


回答1:


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

rvm docs generate

If not, try doing this:

gem install rdoc-data
rdoc-data --install

then try the ri command again.




回答2:


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

[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.




回答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:

$ pacman -S ruby-docs

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




回答4:


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.



来源:https://stackoverflow.com/questions/15697209/how-to-get-the-ruby-documentation-from-the-command-line

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!