How to show Ruby class documentation from the command line

For some reason I can never remember the command to show Ruby documentation (the rdoc stuff that is a lot like Javadoc) from the command line, so this blog post is especially for me ...

To show Ruby rdoc documentation from the command line, just use the ri command, like this:

ri String

A simple command like that will display the rdoc documentation for the Ruby String class. Note that this command is case-sensitive, so there is a big difference between this:

ri String

and this:

ri string

(FWIW, I always want this ri command to be called rdoc, but it's not. Hence this blog post.)

Sample String output

While I'm in the neighborhood, here's the sample output from typing the ri String command:

---------------------------------------------------------- 
Class: String
     A +String+ object holds and manipulates an arbitrary sequence of
     bytes, typically representing characters. String objects may be
     created using +String::new+ or as literals.

     Because of aliasing issues, users of strings should be aware of the
     methods that modify the contents of a +String+ object. Typically,
     methods with names ending in ``!'' modify their receiver, while
     those without a ``!'' return a new +String+. However, there are
     exceptions, such as +String#[]=+.

------------------------------------------------------------------------
     Enhance the String class with a XML escaped character version of
     to_s.

------------------------------------------------------------------------
     User defined methods to be added to String.

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

Includes:
---------
     Comparable(<, <=, ==, >, >=, between?), Enumerable(all?, any?,
     collect, detect, each_cons, each_slice, each_with_index, entries,
     enum_cons, enum_slice, enum_with_index, find, find_all, grep,
     group_by, include?, index_by, inject, map, max, member?, min,
     partition, reject, select, sort, sort_by, sum, to_a, to_set, zip)

(more ...)

Bonus coverage: irb (the interactive ruby shell)

While I'm in the command-line neighborhood, if you ever want to run simple Ruby commands from the command line, just fire up irb, the interactive ruby shell, like this:

irb

Once you run that command, irb will prompt you like this:

>>

At that point you can type whatever Ruby commands you want, just like this:

a = 1 + 2

After you type your commands, irb will show output related to that command, like this:

=> 3

I do this a lot for testing simple commands and scripts, and it's very cool.