Problem using back-ticks + eval

Hi,

I’m trying to demo how Ruby handles various argument formats.

For example, I write the statement:
show %@ruby DisplayArgs.rb Arg1 Arg2@

and I get the output:
ruby DisplayArgs.rb Arg1 Arg2
=>
Arg[1]: “Arg1”
Arg[2]: “Arg2”

I’d like avoid having the back-ticks appear in the output. I’d like to
achieve this by moving the back-ticks from show invocation to show’s
definition. In the following code, comments indicate what I tried, but
it failed.

Any ideas?

Thanks in Advance,
Richard

ShowCmdLineArgs.rb

def show(stmt)
print stmt
puts "\n=> "
eval("puts " + stmt).inspect # Put
back-ticks around “stmt”?
puts
end

puts “\n========= Examples =========”
show %@ruby DisplayArgs.rb Arg1 Arg2@
show %@ruby DisplayArgs.rb "Embedded "" quotes"@ # Remove back-ticks
from this?

DisplayArgs.rb

MAXARGS = 10
puts “No arguments” unless ARGV[0]
(0…MAXARGS).each { |i|
break unless ARGV[i]
print "Arg[#{(i+1).to_s}]: "
puts ARGV[i].inspect
puts “Quitting without inspecting addition arguments,
if any!” if i == MAXARGS-1
}

On Jan 25, 2007, at 10:40, Richard wrote:

I’d like avoid having the back-ticks appear in the output.

def show(command)
puts command
puts ‘=>’
puts #{command}
end


Eric H. - [email protected] - http://blog.segment7.net

I LIT YOUR GEM ON FIRE!

Hi Erik,

I was working in making “show” simpler, but I couldn’t come up with
that last step. Beautiful.

Many thanks,
Richard