26

When working in interactive Python, I tend to rely on the built-in help() function to tell me what something expects and/or returns, and print out any documentation that might help me. Is there a Ruby equivalent to this function?

I'm looking for something I could use in irb. For example, in interactive Python I could type:

>>> help(1)

which would then print

Help on int object:

class int(object)  |  int(x[, base])
-> integer  |    |  

Convert a string or number to an integer, if possible. A ...
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
mozillalives
  • 1,460
  • 16
  • 26

5 Answers5

9

It's now late 2014 and here's the two ways to get the Python help() *similarity, as long as you have the Ruby Docs installed:

  1. From inside irb, You can call the help method with a string describing what you're looking for.

    Example 1: help 'Array' for the Array class
    Example 2: help 'Array#each' for the Array class each method.

  2. From the command line, outside of irb, you can use the ri program:

    Example 1: ri 'Array' for the Array class
    Example 2: ri 'Array#each' for the Array class each method.

* Not quite as good as Python's, but still better than nothing

Community
  • 1
  • 1
Freedom_Ben
  • 11,247
  • 10
  • 69
  • 89
6

It's definitely a poor cousin to iPython's help, and one of the main features I miss after moving to Ruby, but you can also use ri from within irb. I'd recommend the wirble gem as an easy way to set this up.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Peter
  • 127,331
  • 53
  • 180
  • 211
  • 1
    ok, I think this will work. I can get the doc for the class with "Object.ri variable.class" and I can get the method doc with "Object.ri variable.class.to_s + ".method". Thanks – mozillalives Jan 05 '10 at 23:23
  • I type `ri String` and it says "Nothing known about String". – fikr4n Jun 12 '16 at 01:01
  • Do you know if wirble still works? (and is up to date?) (the link is broken) – stevec Aug 21 '20 at 16:35
5

Try using ri from the command line.

It takes a class name, method, or module as an argument, and gives you appropriate documentation. Many popular gems come with this form of documentation, as well, so it should typically work even beyond the scope of core Ruby modules.

Matchu
  • 83,922
  • 18
  • 153
  • 160
  • That's cool. But this is only available from the command line, correct? I'm wondering what I could use if I were in irb and I want to get help on a specific variable. – mozillalives Jan 05 '10 at 23:00
0

There's supposed to be irb_help. But like mentioned in that post, it's broken in my ruby install as well.

Mark
  • 106,305
  • 20
  • 172
  • 230
0

For quick shell access to ruby documentation, just type ri followed by the method you're wanting to learn more about (from your shell).

For example:

ri puts

This must be fired up in your shell, not your irb (interactive ruby environment)

If you're in your irb environment, then another way, is to simply type help followed by the method you want to learn more about as follows:

help puts

However, this assumes that you have configured your Ruby environment correctly for that (help) to work properly within irb. I usually just have another shell open, and just use the ri directly for quick access when I'm in doubt about a certain method or arguments to a method.

codewizard
  • 386
  • 3
  • 8