Problem using back-ticks + eval

R

Richard

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
print "Arg[#{(i+1).to_s}]: "
puts ARGV.inspect
puts "Quitting without inspecting addition arguments,
if any!" if i == MAXARGS-1
}
 
E

Eric Hodel

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.

def show(command)
puts command
puts '=>'
puts `#{command}`
end
 
R

Richard

Hi Erik,

I was working in making "show" simpler, but I couldn't come up with
that last step. Beautiful.

Many thanks,
Richard

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.def show(command)
puts command
puts '=>'
puts `#{command}`
end
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,152
Latest member
LorettaGur
Top