[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

printf statement - "args" as an Array (or something...

John Pritchard-williams

10/2/2008 9:59:00 AM

I'm trying to tart up some logged prepared SQL statements like this:

SELECT col FROM table WHERE a =? AND b =?
PARAMS: 1='one', 2='two'

I thought about using a 'printf' like this, first alter the SQL
statement to:

sql="SELECT col FROM table where a='%s' AND b='%s'"

And then do this:

printf(sql,"one","two")

Trouble is I need a variable list as the args: I can't seem to pass in :
["one","two"] - is there an idiom for this ?

Thanks,

John
--
Posted via http://www.ruby-....

2 Answers

F. Senault

10/2/2008 10:04:00 AM

0

Le 02 octobre à 11:58, John Pritchard-williams a écrit :

> Trouble is I need a variable list as the args: I can't seem to pass in :
> ["one","two"] - is there an idiom for this ?

Splat it.

>> txt = "%s -> %s\n"
=> "%s -> %s\n"
>> args = [ "first", "second" ]
=> ["first", "second"]
>> printf(txt, *args)
first -> second
=> nil

>> txt = "%s -> %s -> %s\n"
=> "%s -> %s -> %s\n"
>> printf(txt, *[ args, "third" ].flatten)
first -> second -> third
=> nil

Fred
--
I was broken, bent out of shape I was naked in the clothes you made
Lips were dry, throat like rust You gave me shelter
From the heat and the dust There's no more water in the well
No more water in the well (U2, Trip Through Your Wires)

John Pritchard-williams

10/2/2008 10:45:00 AM

0

Splatted ! Works - Thanks Fred !

--
Posted via http://www.ruby-....