[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Problem using back-ticks + eval

RichardOnRails

1/25/2007 6:37:00 PM

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
}

2 Answers

Eric Hodel

1/25/2007 6:45:00 PM

0

On Jan 25, 2007, at 10:40, Richard wrote:
> 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

--
Eric Hodel - drbrain@segment7.net - http://blog.se...

I LIT YOUR GEM ON FIRE!


RichardOnRails

1/25/2007 7:22:00 PM

0

Hi Erik,

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

Many thanks,
Richard

On Jan 25, 1:44 pm, Eric Hodel <drbr...@segment7.net> wrote:
> On Jan 25, 2007, at 10:40, Richard wrote:
>
> > 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
>
> --
> Eric Hodel - drbr...@segment7.net -http://blog.se...
>
> I LIT YOUR GEM ON FIRE!