Forum: Ruby Problem using back-ticks + eval

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
31fdda5f38fc6df4e193cfb7445ddc5a?d=identicon&s=25 Richard (Guest)
on 2007-01-25 19:40
(Received via mailing list)
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
}
58479f76374a3ba3c69b9804163f39f4?d=identicon&s=25 Eric Hodel (Guest)
on 2007-01-25 19:45
(Received via mailing list)
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 Hodel - drbrain@segment7.net - http://blog.segment7.net

I LIT YOUR GEM ON FIRE!
31fdda5f38fc6df4e193cfb7445ddc5a?d=identicon&s=25 Richard (Guest)
on 2007-01-25 20:25
(Received via mailing list)
Hi Erik,

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

Many thanks,
Richard
This topic is locked and can not be replied to.