Fellow Stranger
Fellow Stranger

Reputation: 34023

Any way, in the console, to get info on what a certain method does?

Let's say I use the gem csv.

With the following in the console I get a list of all instance methods:

require "csv"
csv = CSV.open(FILE_NAME, "a+")
csv.methods

One method I now find in the list is e.g. first.

Can I from the command line get info about this method? (Yes I know I could google for the docs.)

Upvotes: 1

Views: 91

Answers (2)

Stefan
Stefan

Reputation: 114178

Pry (an IRB alternative) supports documentation browsing:

$ pry
[1] pry(main)> require 'csv'
#=> true

[2] pry(main)> csv = CSV.new('')
#=> <#CSV io_type:StringIO encoding:UTF-8 lineno:0 col_sep:"," row_sep:"\n" quote_char:"\"">

[3] pry(main)> show-doc csv.first

From: enum.c (C Method):
Owner: Enumerable
Visibility: public
Signature: first(*arg1)
Number of lines: 8

Returns the first element, or the first n elements, of the enumerable.
If the enumerable is empty, the first form returns nil, and the
second form returns an empty array.

  %w[foo bar baz].first     #=> "foo"
  %w[foo bar baz].first(2)  #=> ["foo", "bar"]
  %w[foo bar baz].first(10) #=> ["foo", "bar", "baz"]
  [].first                  #=> nil

According to the docs, you don't have to generate the documentation beforehand:

The Pry documentation system does not rely on pre-generated rdoc or ri, instead it grabs the comments directly above the method on demand. This results in speedier documentation retrieval and allows the Pry system to retrieve documentation for methods that would not be picked up by rdoc.

Upvotes: 5

bundacia
bundacia

Reputation: 1036

The simplest way is probably ri

$ ri 'CSV#first'

= CSV#first

(from ruby core)
=== Implementation from Enumerable
------------------------------------------------------------------------------
  enum.first       ->  obj or nil
  enum.first(n)    ->  an_array


------------------------------------------------------------------------------

Returns the first element, or the first n elements, of the enumerable. If the
enumerable is empty, the first form returns nil, and the second form returns
an empty array.

  %w[foo bar baz].first     #=> "foo"
  %w[foo bar baz].first(2)  #=> ["foo", "bar"]
  %w[foo bar baz].first(10) #=> ["foo", "bar", "baz"]
  [].first                  #=> nil

You may need to install the docs first as explained in: How to get the Ruby documentation from the command line

Upvotes: 3

Related Questions