Using ri inside irb -- error in Pickaxe?

D

Donkey Agony

The chapter in the Pickaxe 2nd ed. on irb gives some code for using `ri`
from within it:

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

But that throws an error here. I'm just getting started with Ruby, so I
don't quite understand yet why you have to go through all that
rigamarole just to pass a parameter to `system 'ri keyword'`, nor what
exactly the rigamarole does :), but looking at it I can see there's an
unmatched closing curly brace before the final parens. So I changed it
to:

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

I tried it from within irb as "ri Proc" both in Cygwin and plain
Windows. It caused the Cygwin version to core dump (!), and the Windows
version to return this:

'name' is not recognized as an internal or external command,
operable program or batch file.
=> false

So what is proper way to write that method so I can invoke `ri` from
within irb?

Also, what is the % sign for in the `system` command above?
 
S

ssmoot

"*names.map" should be "names.map". The * before a variable name in a
method definition is basically equiv to the "params" keyword in
Microsoft languages if that helps.

%{} is basically short-form for a HEREDOC. I don't like it personally.
A slightly cleaner example (that works) might look like this:

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

Donkey Agony

"*names.map" should be "names.map".

Ah, my error in copying.
The * before a variable name in a method definition is
basically equiv to the "params" keyword in Microsoft
languages if that helps.

That much I knew. But why do you have to do all this (using your
version):

system("ri #{names.map { |name| name.to_s }.join(' ')}")

instead of just

system("ri #{names}.to_s"}

?
%{} is basically short-form for a HEREDOC.

Ah, thanks.
I don't like it personally.
A slightly cleaner example (that works) might look like this:

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

Both your version, and the original one modified with the * removed from
"names" still cause Cygwin to dump core in irb:

--------------------------------------------------------------C:\cygwin\bin\ruby.exe (3132): *** unable to remap
C:\cygwin\lib\ruby\1.8\i386-cygwin\readline.so to same address as
parent(0x370000) != 0x2760000
C:\cygwin\bin\ruby.exe (3132): *** unable to remap
C:\cygwin\lib\ruby\1.8\i386-cygwin\readline.so to same address as
parent(0x370000) != 0x2760000
19 [main] 2888 fork_parent: child 3132 died waiting for dll
loading /home/Jelks/.irbrc:4: [BUG] rb_sys_fail() - errno == 0
ruby 1.8.3 (2005-09-21) [i386-cygwin]

Aborted (core dumped)

[blackie] ~
$

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

I get the same error, though without the core dump if I do it as a Ruby
program.

Under plain Windows, your version above returns "false", while the
modified original still returns:

'name' is not recognized as an internal or external command,
operable program or batch file.
=> false

Perhaps I should go fire up a Linux box. If I can get it working there,
then it must be a Ruby under Cygwin and a Ruby under Windows issues.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top