# Printf statement - "args" as an Array (or something...)

**URL:** <https://www.ruby-forum.com/t/printf-statement-args-as-an-array-or-something/150011>\
**Category:** Ruby\
**Created:** [October 2, 2008, 11:59am UTC](https://www.ruby-forum.com/t/printf-statement-args-as-an-array-or-something/150011 "2008-10-02T11:59:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnpetterson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/41988e/32.png) [@johnpetterson](https://www.ruby-forum.com/u/johnpetterson)\
**Post date:** [October 2, 2008, 11:59am UTC](https://www.ruby-forum.com/t/printf-statement-args-as-an-array-or-something/150011/1 "2008-10-02T11:59:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![johnpetterson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/41988e/32.png) [@johnpetterson](https://www.ruby-forum.com/u/johnpetterson)\
**Post date:** [October 2, 2008, 12:46pm UTC](https://www.ruby-forum.com/t/printf-statement-args-as-an-array-or-something/150011/2 "2008-10-02T12:46:34Z")

</div>

Splatted ! Works - Thanks Fred !

---

<div class="post-metadata">

**Author:** ![johnpetterson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/41988e/32.png) [@johnpetterson](https://www.ruby-forum.com/u/johnpetterson)\
**Post date:** [October 2, 2008, 12:05pm UTC](https://www.ruby-forum.com/t/printf-statement-args-as-an-array-or-something/150011/3 "2008-10-02T12:05:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![johnpetterson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/41988e/32.png) [@johnpetterson](https://www.ruby-forum.com/u/johnpetterson)\
**Post date:** [October 2, 2008, 1:15pm UTC](https://www.ruby-forum.com/t/printf-statement-args-as-an-array-or-something/150011/4 "2008-10-02T13:15:15Z")

</div>

Hi –

On Thu, 2 Oct 2008, F. Senault wrote:

> =\> [“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

And in 1.9 you can do:

[\*args, “third”]

David
