[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: Substitution within system quoted string

Stefano Crocco

8/27/2007 8:34:00 PM

Alle lunedì 27 agosto 2007, Victor Reyes ha scritto:
> Team,
>
> I would like to enter a value at execution time within the *system* key
> world environment.
> However, I am having difficulties entering a value within a quoted string.
>
> For example, I can hard code the following and it works fine:
>
> system("start putty.exe -X -ssh -pw mypassword myuserid@myhostname")
>
> But this requires hard coding the password in the script.
> I would like to do something like the following:
>
> system("start putty.exe -X -ssh -pw " ARGV[0] " myuserid@myhostname")
>
> Where: ARGV[0] holds the password entered by the invoker of the script.
>
> Is there a way to substitute within a quoted string?
> Is there a better way to do this?
>
> Thank you
>
> Victor

You need string interpolation:

system("start putty.exe -X -ssh -pw #{ARGV[0]} myuserid@myhostname")

In a double quoted string (as well as in a regexp or backtick expression) you
can substitute insert the value of any expression by enclosing it in #{}:

a = 2

puts "a is #{a}"

=> a is 2

This doesn't work in single-quoted strings.

puts 'a is #{a}'

=> a is #{a}

I hope this helps

Stefano