新手问题 请教哪里有比较详细的 Ruby API 文档

angryfrog · 2013年03月20日 · 最后由 zlx_star 回复于 2013年03月20日 · 4542 次阅读

首先我知道应该用 ri,但我的 ri 貌似有些毛病。 ri String 可以找到,但 ri String#gsub或ri String.gsub,就是找不到文档。

我还知道可以用 API 反射,但 APi 反射我只能知道哪些对象有哪些方法,但不知道具体方法的作用和参数以及返回值之类的。

所以想找个 web 文档,我搜了一些,但好像和用反射出来的一样,没有具体的方法详细介绍。

请问各位知道哪里有比较详细的内部和外部常用库的 API 文档嘛?比如我想找 Net::HTTP 的相关资料就很难找到一个详细的。

也许是我不会找,所以请大家赐教,另外如果有人知道我的第一个问题是怎么回事也请指教。先谢过了。

rubydoc.info

安装 Pry 和 Pry Doc

结合 cd 和?来了解 RDoc

举个简单例子来说明 Net:HTTP

~  pry

[3] pry(main)> require 'net/http'
=> true
[4] pry(main)> cd Net::HTTP
[5] pry(Net::HTTP):1> ls
constants: Copy  Delete  Get  HAVE_ZLIB  Head  HTTPVersion  Lock  Mkcol  Move  Options  Patch  Post  Propfind  Proppatch  ProxyDelta  ProxyMod  Put  Revision  SSL_ATTRIBUTES  Trace  Unlock
Net::Protocol.methods: protocol_param
Net::HTTP.methods:
  Proxy         get        get_response       https_default_port  is_version_1_2?  newobj     proxy_address  proxy_pass  proxy_user   start         version_1_2
  default_port  get_print  http_default_port  is_version_1_1?     new              post_form  proxy_class?   proxy_port  socket_type  version_1_1?  version_1_2?
Net::HTTP#methods:
  active?   ca_path=     ciphers                   continue_timeout=  get2     key=          open_timeout=  post       proxy_address  proxyport      request       send_request      ssl_version=  use_ssl=          verify_depth=
  address   cert         ciphers=                  copy               head     lock          options        post2      proxy_pass     put            request_get   set_debug_output  start         use_ssl?          verify_mode
  ca_file   cert=        close_on_empty_response   delete             head2    mkcol         patch          propfind   proxy_port     put2           request_head  ssl_timeout       started?      verify_callback   verify_mode=
  ca_file=  cert_store   close_on_empty_response=  finish             inspect  move          peer_cert      proppatch  proxy_user     read_timeout   request_post  ssl_timeout=      trace         verify_callback=
  ca_path   cert_store=  continue_timeout          get                key      open_timeout  port           proxy?     proxyaddr      read_timeout=  request_put   ssl_version       unlock        verify_depth
instance variables: @is_proxy_class  @proxy_addr  @proxy_pass  @proxy_port  @proxy_user
locals: _  __  _dir_  _ex_  _file_  _in_  _out_  _pry_
[6] pry(Net::HTTP):1> ? get

From: /Users/jian/.rbenv/versions/1.9.3-p327-perf/lib/ruby/1.9.1/net/http.rb @ line 981:
Owner: Net::HTTP
Visibility: public
Signature: get(path, initheader=?, dest=?, &block)
Number of lines: 35

Gets data from path on the connected-to host.
initheader must be a Hash like { 'Accept' => '*/*', ... },
and it defaults to an empty hash.
If initheader doesn't have the key 'accept-encoding', then
a value of "gzip;q=1.0,deflate;q=0.6,identity;q=0.3" is used,
so that gzip compression is used in preference to deflate
compression, which is used in preference to no compression.
Ruby doesn't have libraries to support the compress (Lempel-Ziv)
compression, so that is not supported.  The intent of this is
to reduce bandwidth by default.   If this routine sets up
compression, then it does the decompression also, removing
the header as well to prevent confusion.  Otherwise
it leaves the body as it found it.

This method returns a Net::HTTPResponse object.

If called with a block, yields each fragment of the
entity body in turn as a string as it is read from
the socket.  Note that in this case, the returned response
object will *not* contain a (meaningful) body.

dest argument is obsolete.
It still works but you must not use it.

This method never raises an exception.

    response = http.get('/index.html')

    # using block
    File.open('result.txt', 'w') {|f|
      http.get('/~foo/') do |str|
        f.write str
      end
    }

[7] pry(Net::HTTP):1>

非常感谢大家。又学到了不少。

#7 楼 @Levan 赞,有多个版本的 api 加上强大的搜索

需要 登录 后方可回复, 如果你还没有账号请 注册新账号