# Use ri from irb?

**URL:** https://rubytalk.org/t/use-ri-from-irb/13975
**Category:** ruby-talk
**Created:** [5 October 2004 19:04 UTC](https://rubytalk.org/t/use-ri-from-irb/13975 "2004-10-05T19:04:50Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![Its\_Me](https://avatars.discourse-cdn.com/v4/letter/i/958977/32.png) [@Its\_Me](https://rubytalk.org/u/Its_Me)
#### Post date: [5 October 2004 19:04 UTC](https://rubytalk.org/t/use-ri-from-irb/13975/1 "2004-10-05T19:04:50Z")

</div>

What do I need to do to use ri from within irb? I can use it fine from the  
(dos) command line.

Thanks

---

<div class="post-metadata">

### Author: ![George5](https://avatars.discourse-cdn.com/v4/letter/g/7c8e57/32.png) [@George5](https://rubytalk.org/u/George5)
#### Post date: [5 October 2004 20:01 UTC](https://rubytalk.org/t/use-ri-from-irb/13975/2 "2004-10-05T20:01:55Z")

</div>

Its Me wrote:

> What do I need to do to use ri from within irb? I can use it fine from the  
> (dos) command line.

That's handled in Programming Ruby 2 (Pickaxe II), page 191.

Add the following to your .irbrc file:

&nbsp;&nbsp;&nbsp;def ri(\*names)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;system(%{ri #{names.map {|name| name.to\_s}.join(" ")}})  
&nbsp;&nbsp;&nbsp;end

Regards,

&nbsp;&nbsp;&nbsp;Michael

---

<div class="post-metadata">

### Author: ![Mark\_Hubbart1](https://avatars.discourse-cdn.com/v4/letter/m/a3d4f5/32.png) [@Mark\_Hubbart1](https://rubytalk.org/u/Mark_Hubbart1)
#### Post date: [5 October 2004 20:41 UTC](https://rubytalk.org/t/use-ri-from-irb/13975/3 "2004-10-05T20:41:53Z")

</div>

> Its Me wrote:  
> \> What do I need to do to use ri from within irb? I can use it fine from the  
> \> (dos) command line.
> 
> That's handled in Programming Ruby 2 (Pickaxe II), page 191.
> 
> Add the following to your .irbrc file:
> 
> &nbsp;&nbsp;&nbsp;def ri(\*names)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;system(%{ri #{names.map {|name| name.to\_s}.join(" ")}})  
> &nbsp;&nbsp;&nbsp;end

Is it just me, or does that look a bit kludgy? ri itself is a ruby  
script, and a ruby lib. It seems to me that there should be a clean  
way of including the ri documentation in every object's methods... ie:

> require 'irb/ri'

&nbsp;&nbsp;==\> true

> Kernel.docs

[... paged documentation output ...]  
&nbsp;&nbsp;==\> Kernel

> Kernel.docs(:puts)

[...]

... and so on. This should be able to be one programatically, rather  
than spawning an ri process.

cheers,  
Mark

> **···**
>
> On Wed, 6 Oct 2004 05:01:55 +0900, Michael Neumann \<mneumann@ntecs.de\> wrote:

---

<div class="post-metadata">

### Author: ![Dave\_Mail\_Archive](https://avatars.discourse-cdn.com/v4/letter/d/f04885/32.png) [@Dave\_Mail\_Archive](https://rubytalk.org/u/Dave_Mail_Archive)
#### Post date: [5 October 2004 20:56 UTC](https://rubytalk.org/t/use-ri-from-irb/13975/4 "2004-10-05T20:56:02Z")

</div>

It is a tad kludgy, but not too bad... In the chapter I wanted to illustrate extending irb, rather than working on the issues on integrating something such as ri.

The extension that you describe would be a wonderful add on: I just didn't have space in the book to show its development.

Cheers

Dave

> **···**
>
> On Oct 5, 2004, at 15:41, Mark Hubbart wrote:
> 
> > > Add the following to your .irbrc file:
> > > 
> > > &nbsp;&nbsp;&nbsp;def ri(\*names)  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;system(%{ri #{names.map {|name| name.to\_s}.join(" ")}})  
> > > &nbsp;&nbsp;&nbsp;end
> > 
> > Is it just me, or does that look a bit kludgy? ri itself is a ruby  
> > script, and a ruby lib. It seems to me that there should be a clean  
> > way of including the ri documentation in every object's methods... ie:

---

<div class="post-metadata">

### Author: ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)
#### Post date: [5 October 2004 21:56 UTC](https://rubytalk.org/t/use-ri-from-irb/13975/5 "2004-10-05T21:56:24Z")

</div>

Well, look at the source of /usr/local/bin/ri and you get only three lines:

&nbsp;&nbsp;require 'rdoc/ri/ri\_driver'  
&nbsp;&nbsp;ri = RiDriver.new  
&nbsp;&nbsp;ri.process\_args

It's a shame that that process\_args doesn't take an (optional) array  
parameter; it forces you to use ARGV. But you can frig it:

$ cat .irbrc  
require 'rdoc/ri/ri\_driver'  
def ri(\*names)  
&nbsp;&nbsp;oargv = Object.const\_get(:ARGV)  
&nbsp;&nbsp;Object.const\_set(:ARGV, names)  
&nbsp;&nbsp;RiDriver.new.process\_args  
&nbsp;&nbsp;Object.const\_set(:ARGV, oargv)  
end

$ irb  
irb(main):001:0\> ri 'Enumerable'

Regards,

Brian.

> **···**
>
> On Wed, Oct 06, 2004 at 05:41:53AM +0900, Mark Hubbart wrote:
> 
> > \> Add the following to your .irbrc file:  
> > \>  
> > \> def ri(\*names)  
> > \> system(%{ri #{names.map {|name| name.to\_s}.join(" ")}})  
> > \> end
> > 
> > Is it just me, or does that look a bit kludgy? ri itself is a ruby  
> > script, and a ruby lib. It seems to me that there should be a clean  
> > way of including the ri documentation in every object's methods... ie:
> > 
> > \> require 'irb/ri'  
> > &nbsp;&nbsp;==\> true  
> > \> Kernel.docs  
> > [... paged documentation output ...]  
> > &nbsp;&nbsp;==\> Kernel  
> > \> Kernel.docs(:puts)  
> > [...]
> > 
> > ... and so on. This should be able to be one programatically, rather  
> > than spawning an ri process.

---

<div class="post-metadata">

### Author: ![Mark\_Hubbart1](https://avatars.discourse-cdn.com/v4/letter/m/a3d4f5/32.png) [@Mark\_Hubbart1](https://rubytalk.org/u/Mark_Hubbart1)
#### Post date: [5 October 2004 21:49 UTC](https://rubytalk.org/t/use-ri-from-irb/13975/6 "2004-10-05T21:49:54Z")

</div>

🙂 I wasn't intending to criticize your code (or your book)... It's a  
good fix for the problem, especially when seen in the light of it  
being a concise example of how to extend the behavior of irb. I have  
something vaguely similar to that in my .irbrc file (but mine looks  
much more kludgy, makes generous use of backticks, and occupies the  
greatest part of the file 🙂

I just think that eventually, it would be nice to have it built into  
the irb libraries, and when it does, it should be custom tailored for  
it's purpose.

The question is, how difficult is it to get at the documentation via  
the ri libraries? hmmm...

cheers,  
Mark

> **···**
>
> On Wed, 6 Oct 2004 05:56:02 +0900, Dave Thomas \<dave@pragprog.com\> wrote:
> 
> > On Oct 5, 2004, at 15:41, Mark Hubbart wrote:  
> > \>\> Add the following to your .irbrc file:  
> > \>\>  
> > \>\> def ri(\*names)  
> > \>\> system(%{ri #{names.map {|name| name.to\_s}.join(" ")}})  
> > \>\> end  
> > \>  
> > \> Is it just me, or does that look a bit kludgy? ri itself is a ruby  
> > \> script, and a ruby lib. It seems to me that there should be a clean  
> > \> way of including the ri documentation in every object's methods... ie:
> > 
> > It is a tad kludgy, but not too bad... In the chapter I wanted to  
> > illustrate extending irb, rather than working on the issues on  
> > integrating something such as ri.
> > 
> > The extension that you describe would be a wonderful add on: I just  
> > didn't have space in the book to show its development.

---

<div class="post-metadata">

### Author: ![Bill\_Atkins](https://avatars.discourse-cdn.com/v4/letter/b/c4cdca/32.png) [@Bill\_Atkins](https://rubytalk.org/u/Bill_Atkins)
#### Post date: [6 October 2004 00:00 UTC](https://rubytalk.org/t/use-ri-from-irb/13975/7 "2004-10-06T00:00:57Z")

</div>

Or maybe

# untested  
class RiDriver  
&nbsp;&nbsp;alias\_method :oldpa, :process\_args

&nbsp;&nbsp;def process\_args \*args  
&nbsp;&nbsp;&nbsp;&nbsp;args = ARGV if args.nil?  
&nbsp;&nbsp;&nbsp;&nbsp;oldpa \*args  
&nbsp;&nbsp;end  
end

Then you can just do "ri.process\_args arg\_array"

Bill Atkins

> **···**
>
> On Wed, 6 Oct 2004 06:56:24 +0900, Brian Candler \<b.candler@pobox.com\> wrote:
> 
> > On Wed, Oct 06, 2004 at 05:41:53AM +0900, Mark Hubbart wrote:  
> > \> \> Add the following to your .irbrc file:  
> > \> \>  
> > \> \> def ri(\*names)  
> > \> \> system(%{ri #{names.map {|name| name.to\_s}.join(" ")}})  
> > \> \> end  
> > \>  
> > \> Is it just me, or does that look a bit kludgy? ri itself is a ruby  
> > \> script, and a ruby lib. It seems to me that there should be a clean  
> > \> way of including the ri documentation in every object's methods... ie:  
> > \>  
> > \> \> require 'irb/ri'  
> > \> ==\> true  
> > \> \> Kernel.docs  
> > \> [... paged documentation output ...]  
> > \> ==\> Kernel  
> > \> \> Kernel.docs(:puts)  
> > \> [...]  
> > \>  
> > \> ... and so on. This should be able to be one programatically, rather  
> > \> than spawning an ri process.
> > 
> > Well, look at the source of /usr/local/bin/ri and you get only three lines:
> > 
> > &nbsp;&nbsp;require 'rdoc/ri/ri\_driver'  
> > &nbsp;&nbsp;ri = RiDriver.new  
> > &nbsp;&nbsp;ri.process\_args
> > 
> > It's a shame that that process\_args doesn't take an (optional) array  
> > parameter; it forces you to use ARGV. But you can frig it:
> > 
> > $ cat .irbrc  
> > require 'rdoc/ri/ri\_driver'  
> > def ri(\*names)  
> > &nbsp;&nbsp;oargv = Object.const\_get(:ARGV)  
> > &nbsp;&nbsp;Object.const\_set(:ARGV, names)  
> > &nbsp;&nbsp;RiDriver.new.process\_args  
> > &nbsp;&nbsp;Object.const\_set(:ARGV, oargv)  
> > end
> > 
> > $ irb  
> > irb(main):001:0\> ri 'Enumerable'
> > 
> > Regards,
> > 
> > Brian.

---

<div class="post-metadata">

### Author: ![Dave\_Mail\_Archive](https://avatars.discourse-cdn.com/v4/letter/d/f04885/32.png) [@Dave\_Mail\_Archive](https://rubytalk.org/u/Dave_Mail_Archive)
#### Post date: [6 October 2004 02:19 UTC](https://rubytalk.org/t/use-ri-from-irb/13975/8 "2004-10-06T02:19:13Z")

</div>

That's a simple fix, methinks. I'm heads down on this amazon talk at the moment, but when I get a chance...

Dave

> **···**
>
> On Oct 5, 2004, at 16:56, Brian Candler wrote:
> 
> > It's a shame that that process\_args doesn't take an (optional) array  
> > parameter; it forces you to use ARGV. But you can frig it:

---

<div class="post-metadata">

### Author: ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)
#### Post date: [6 October 2004 09:05 UTC](https://rubytalk.org/t/use-ri-from-irb/13975/9 "2004-10-06T09:05:42Z")

</div>

Erm, except the method 'oldpa' still doesn't take any arguments, so what's  
it going to do with the ones you're passing to it! I can't see what you're  
trying to achieve there. The method oldpa/process\_args uses the contents of  
the ARGV constant directly. Those are the arguments passed on the command  
line, not anything to do with method call arguments.

Anyway, here's a better solution to go in .irbrc:

def ri(\*names)  
&nbsp;&nbsp;require 'rdoc/ri/ri\_driver'  
&nbsp;&nbsp;oargv = ARGV.dup  
&nbsp;&nbsp;ARGV.replace names.map{|n| n.to\_s}  
&nbsp;&nbsp;RiDriver.new.process\_args  
ensure  
&nbsp;&nbsp;ARGV.replace oargv  
end

\* putting require \_inside\_ the method means that the library won't be  
&nbsp;&nbsp;loaded until it's first used  
\* using 'replace' gets rid of the warnings about constants being changed  
\* using to\_s means that class names don't need to be quoted:

irb(main):001:0\> ri Enumerable  
------------------------------------------------------ Class: Enumerable  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;The +Enumerable+ mixin provides collection classes with several  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;traversal and searching methods, and with the ability to sort. The  
... etc

Regards,

Brian.

> **···**
>
> On Wed, Oct 06, 2004 at 09:00:57AM +0900, Bill Atkins wrote:
> 
> > Or maybe
> > 
> > # untested  
> > class RiDriver  
> > &nbsp;&nbsp;alias\_method :oldpa, :process\_args
> > 
> > &nbsp;&nbsp;def process\_args \*args  
> > &nbsp;&nbsp;&nbsp;&nbsp;args = ARGV if args.nil?  
> > &nbsp;&nbsp;&nbsp;&nbsp;oldpa \*args  
> > &nbsp;&nbsp;end  
> > end
> > 
> > Then you can just do "ri.process\_args arg\_array"

---

<div class="post-metadata">

### Author: ![Kristof\_Bastiaensen1](https://avatars.discourse-cdn.com/v4/letter/k/9fc29f/32.png) [@Kristof\_Bastiaensen1](https://rubytalk.org/u/Kristof_Bastiaensen1)
#### Post date: [6 October 2004 10:44 UTC](https://rubytalk.org/t/use-ri-from-irb/13975/10 "2004-10-06T10:44:57Z")

</div>

> I just think that eventually, it would be nice to have it built into the  
> irb libraries, and when it does, it should be custom tailored for it's  
> purpose.
> 
> The question is, how difficult is it to get at the documentation via the  
> ri libraries? hmmm...

Not so difficult. That's what I did to create my emacs-extension.  
You may be interested to look at ri-emacs.rb in the ri-emacs distribution.  
It reimplements ri-driver.rb, which can be adapted easily for other  
purposes.

> cheers,  
> Mark

Regards,  
KB

> **···**
>
> On Wed, 06 Oct 2004 06:49:54 +0900, Mark Hubbart wrote:

---

<div class="post-metadata">

### Author: ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)
#### Post date: [6 October 2004 13:02 UTC](https://rubytalk.org/t/use-ri-from-irb/13975/11 "2004-10-06T13:02:34Z")

</div>

There is another problem with this though: if you do  
&nbsp;&nbsp;ri 'foo'  
then irb exits completely. This is because process\_args calls exit(1).

I think it should do 'return 1', and then /usr/local/bin/ri can do  
&nbsp;&nbsp;exit ri.process\_args  
instead.

Just another suggestion for Dave while he's looking at this...

Cheers,

Brian.

> **···**
>
> On Wed, Oct 06, 2004 at 10:05:35AM +0100, Brian Candler wrote:
> 
> > def ri(\*names)  
> > &nbsp;&nbsp;require 'rdoc/ri/ri\_driver'  
> > &nbsp;&nbsp;oargv = ARGV.dup  
> > &nbsp;&nbsp;ARGV.replace names.map{|n| n.to\_s}  
> > &nbsp;&nbsp;RiDriver.new.process\_args  
> > ensure  
> > &nbsp;&nbsp;ARGV.replace oargv  
> > end
