Exploring Ruby Documentation with ri

In Ruby, the command ri is used to access the built-in documentation (also known as “RDoc”) of a library, class, method, or specific topic. ri is an abbreviation for “Ruby Interactive” or “Ruby Index.”

When you run the ri command in the terminal, followed by a class, method, or topic name, you will see the documentation related to that element.

For example, if you run the ri Array command in the terminal, you will see something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
(from ruby core)
------------------------------------------------------------------------
An Array is an ordered, integer-indexed collection of objects, called
elements. Any object (even another array) may be an
array element, and an array can contain objects of different types.

== Array Indexes

Array indexing starts at 0, as in C or Java.

A positive index is an offset from the first element:

* Index 0 indicates the first element.
* Index 1 indicates the second element.
* ...

A negative index is an offset, backwards, from the end of the array:

* Index -1 indicates the last element.

If you run ri Array#each, you will see the documentation for the each method of the Array class.

1
ri Array#each
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
= Array#each

(from ruby core)
------------------------------------------------------------------------
  array.each {|element| ... } -> self
  array.each -> Enumerator

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

Iterates over array elements.

When a block given, passes each successive array element to the block;
returns self:

  a = [:foo, 'bar', 2]
  a.each {|element|  puts "#{element.class} #{element}" }

Output:

  Symbol foo
:

It’s a helpful way to consult Ruby documentation without needing to search in external resources or on the web.

Finding the definition of a method in your Rails application:

Sometimes, you need to find the definition of a method in your Rails application. For instance, you might want to locate the definition of a class or instance method. You can use the source_location method for this purpose. For example:

For instance methods:

1
2
Movie.instance_method(:rating).source_location
# => ["/app/models/movie.rb", 210]

For class methods:

1
2
Movie.singleton_method(:rating).source_location
# => ["/app/models/movie.rb", 21]

You can also use the source method to view the source code of a method.

1
2
Movie.instance_method(:rating).source
# => "def rating\n  read_attribute(:rating)\nend\n"

For a better code visualization, you can use the display method.

1
2
3
4
Movie.instance_method(:rating).source.display
def rating
  read_attribute(:rating)
end

This will be very useful, especially if your code is full of metaprogramming.

Finding the definition of a method in a gem:

To locate the definition of a method in a gem, you can use the bundle open command. For instance, to find the definition of the save method in the activerecord gem:

1
bundle open activerecord

This will open the file activerecord/lib/active_record/core.rb in your default text editor. You can then directly search for the save method in the project.