Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading docs in irb

One thing I miss about ipython is it has a ? operator which diggs up the docs for a particular function.

I know ruby has a similar command line tool but it is extremely inconvenient to call it while I am in irb.

Does ruby/irb have anything similar?

like image 206
disappearedng Avatar asked Aug 12 '11 06:08

disappearedng


People also ask

How do I load a Ruby file in IRB?

From the main menu, choose Tools | Load file/selection into IRB/Rails console.

How do I open an IRB file?

To invoke it, type irb at a shell or command prompt, and begin entering Ruby statements and expressions. Use exit or quit to exit irb.

How do you run a command in IRB?

You can start it on any computer where Ruby is installed by executing the command irb from your command line interface. The prompt indicates that you're running IRB and that anything you execute will run in the main context, which is the top-level default context of a Ruby program. It also shows a line number.

What is IRB command used for?

IRB stands for “interactive Ruby” and is a tool to interactively execute Ruby expressions read from the standard input. The irb command from your shell will start the interpreter.


1 Answers

Pry is a Ruby version of IPython, it supports the ? command to look up documentation on methods, but uses a slightly different syntax:

pry(main)> ? File.dirname

From: file.c in Ruby Core (C Method):
Number of lines: 6

visibility:  public
signature:  dirname()

Returns all components of the filename given in file_name
except the last one. The filename must be formed using forward
slashes (/'') regardless of the separator used on the
local file system.

   File.dirname("/home/gumby/work/ruby.rb")   #=> "/home/gumby/work"

You can also look up sourcecode with the $ command:

pry(main)> $ File.link

From: file.c in Ruby Core (C Method):
Number of lines: 14

static VALUE
rb_file_s_link(VALUE klass, VALUE from, VALUE to)
{
    rb_secure(2);
    FilePathValue(from);
    FilePathValue(to);
    from = rb_str_encode_ospath(from);
    to = rb_str_encode_ospath(to);

    if (link(StringValueCStr(from), StringValueCStr(to)) < 0) {
    sys_fail2(from, to);
    }
    return INT2FIX(0);
}

See http://pry.github.com for more information :)

like image 117
horseyguy Avatar answered Oct 02 '22 01:10

horseyguy