Search Ruby Methods and Classes in the Command Line with Ruby Interactive (ri)

If you’re new to Ruby programming, there’s a good chance you’re spending a lot of your time googling what methods to use in particular situations and how to apply them syntactically.

This is an inherent part of programming at any level and completely fine. But you may be unaware that many of the answers you’re looking for on Google are built into Ruby and readily available in the command line.

If, for instance, you want to figure out how to use Ruby’s select method, simply type “ri select” into the terminal

and it will return all of the documentation on the select method. If you want to see how to implement select on a hash and also see what it would return, you can scroll down to this exact information:

You can even skip the scrolling altogether by being more specific in your initial search. Ruby Interactive allows you to specify what object you want to call a method on as follows:

This is all fine when you know the name of the method you’re looking for, but you of course don’t always have that luxury. Sometimes you’re forced to google a particular problem to see how other people have solved it (“return array of all hash values less than 200,” for instance). Ruby Interactive can’t necessarily replace Google in this situation, but it can still be helpful when you only have a vague sense of what you’re looking for. If all you know is that you need to pull some specific values from a hash, you can search the hash class itself:

In addition to telling you what sort of object a hash is and when you might want to use one, Ruby Interactive will give a list of class and instance methods you can call on one:

This won’t necessarily solve your problem, but some of the method names may jump out as relevant-sounding (fetch_values, select), and you can then quickly search them within Ruby Interactive to see if they do what you want them to.

It’s worth noting that the information in Ruby Interactive is largely identical to the information on Ruby’s own documentation site (http://ruby-doc.org/core-2.2.0/). A search of the array class in both Ruby Interactive and on ruby-doc.org, for example, gives us word-for-word the same information and examples:

I only mention this because when I first started learning Ruby I spent a lot of time on ruby-doc.org, and I find Ruby Interactive much easier to navigate.

You can of course never bother to use Ruby Interactive and live a perfectly happy programming life. There is nothing within it that can’t be obtained elsewhere on the internet, and sometimes your best bet really is to see the full range of solutions forum users have come up with to answer a question that matches the one you’ve searched. Where Ruby Interactive shines is as a workflow booster. Sometimes you really do just need a quick reference for how to apply a method or to double-check a method’s exact name and what classes you can call it on, and the fact that you can get all of that without looking through Google search results or even leaving your terminal is massively handy.

Here are a few commands that are worth knowing about:

  • ri -i launches interactive mode, where you can search methods and classes one after another without typing ri, and also hit tab to auto-complete if you only know part of what you’re looking for
  • ri -l lists all of the classes Ruby knows about
  • ri -h pulls up the help menu, which among other things gives some usage specifics for other types of searches
  • q will quit out of Ruby interactive

Happy programming!

Full-stack web developer and Flatiron School Graduate https://github.com/smitham50

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store