[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Use ri from irb ?

Its Me

10/5/2004 7:01:00 PM

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

Thanks



10 Answers

Michael Neumann

10/5/2004 8:02:00 PM

0

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:

def ri(*names)
system(%{ri #{names.map {|name| name.to_s}.join(" ")}})
end

Regards,

Michael


Mark Hubbart

10/5/2004 8:42:00 PM

0

On Wed, 6 Oct 2004 05:01:55 +0900, Michael Neumann <mneumann@ntecs.de> wrote:
> 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:
>
> 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:

irb> require 'irb/ri'
==> true
irb> Kernel.docs
[... paged documentation output ...]
==> Kernel
irb> Kernel.docs(:puts)
[...]

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

cheers,
Mark


Dave Thomas

10/5/2004 8:56:00 PM

0


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.

Cheers

Dave



Mark Hubbart

10/5/2004 9:50:00 PM

0

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.

:) 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


Brian Candler

10/5/2004 9:56:00 PM

0

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:
>
> irb> require 'irb/ri'
> ==> true
> irb> Kernel.docs
> [... paged documentation output ...]
> ==> Kernel
> irb> 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:

require 'rdoc/ri/ri_driver'
ri = RiDriver.new
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)
oargv = Object.const_get(:ARGV)
Object.const_set(:ARGV, names)
RiDriver.new.process_args
Object.const_set(:ARGV, oargv)
end

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

Regards,

Brian.


Bill Atkins

10/6/2004 12:01:00 AM

0

Or maybe

# untested
class RiDriver
alias_method :oldpa, :process_args

def process_args *args
args = ARGV if args.nil?
oldpa *args
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:
> >
> > irb> require 'irb/ri'
> > ==> true
> > irb> Kernel.docs
> > [... paged documentation output ...]
> > ==> Kernel
> > irb> 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:
>
> require 'rdoc/ri/ri_driver'
> ri = RiDriver.new
> 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)
> oargv = Object.const_get(:ARGV)
> Object.const_set(:ARGV, names)
> RiDriver.new.process_args
> Object.const_set(:ARGV, oargv)
> end
>
> $ irb
> irb(main):001:0> ri 'Enumerable'
>
> Regards,
>
> Brian.
>
>


Dave Thomas

10/6/2004 2:19:00 AM

0


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:

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


Dave



Brian Candler

10/6/2004 9:06:00 AM

0

On Wed, Oct 06, 2004 at 09:00:57AM +0900, Bill Atkins wrote:
> Or maybe
>
> # untested
> class RiDriver
> alias_method :oldpa, :process_args
>
> def process_args *args
> args = ARGV if args.nil?
> oldpa *args
> end
> end
>
> Then you can just do "ri.process_args arg_array"

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)
require 'rdoc/ri/ri_driver'
oargv = ARGV.dup
ARGV.replace names.map{|n| n.to_s}
RiDriver.new.process_args
ensure
ARGV.replace oargv
end

* putting require _inside_ the method means that the library won't be
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
The +Enumerable+ mixin provides collection classes with several
traversal and searching methods, and with the ability to sort. The
... etc

Regards,

Brian.


Kristof Bastiaensen

10/6/2004 10:47:00 AM

0

On Wed, 06 Oct 2004 06:49:54 +0900, Mark Hubbart wrote:
> 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

Brian Candler

10/6/2004 1:03:00 PM

0

On Wed, Oct 06, 2004 at 10:05:35AM +0100, Brian Candler wrote:
> def ri(*names)
> require 'rdoc/ri/ri_driver'
> oargv = ARGV.dup
> ARGV.replace names.map{|n| n.to_s}
> RiDriver.new.process_args
> ensure
> ARGV.replace oargv
> end

There is another problem with this though: if you do
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
exit ri.process_args
instead.

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

Cheers,

Brian.