Array -> list

Is there a way to convert an array to a (parameter-)list?

I often find myself doing things like:

year,mon,day,hour,minute = ParseDate.parse(datestring)
printf("%04d-%02d-%02d %02d:%02d\n",year,mon,day,hour,minute)

while it would be a lot nicer to do:

printf("%04d-%02d-%02d %02d:%02d\n", ParseDate.parse(datestring)

It is of course perfectly possible to redefine printf and make it check
whether it is offered an array or a parameter list, but it would be a lot
easier when ruby did so automatically.

Alternatively, there could be something like Array#flatten which flattens
an array to a list.

···


Wybo

   printf("%04d-%02d-%02d %02d:%02d\n", ParseDate.parse(datestring)

svg% ruby -rparsedate \

-e 'printf("%04d-%02d-%02d %02d:%02d\n", *ParseDate.parsedate(Time.now.to_s))'

2003-11-15 13:31
svg%

Guy Decoux

just use a * just before an array to make it a list of parameters.

Btw, maybe you could just use
print Date.strftime(yourformat)

···

il Sat, 15 Nov 2003 21:23:09 +0900, Wybo Dekker wybo@servalys.nl ha scritto::

Is there a way to convert an array to a (parameter-)list?

I often find myself doing things like:

year,mon,day,hour,minute = ParseDate.parse(datestring)
printf(“%04d-%02d-%02d %02d:%02d\n”,year,mon,day,hour,minute)

while it would be a lot nicer to do:

printf(“%04d-%02d-%02d %02d:%02d\n”, ParseDate.parse(datestring)

Hi Wybo

Wybo Dekker schrieb:

Is there a way to convert an array to a (parameter-)list?

Do you mean *anArray ?

I often find myself doing things like:

year,mon,day,hour,minute = ParseDate.parse(datestring)
printf(“%04d-%02d-%02d %02d:%02d\n”,year,mon,day,hour,minute)

while it would be a lot nicer to do:

printf(“%04d-%02d-%02d %02d:%02d\n”, ParseDate.parse(datestring)

I tested the following

···

dateArray = [1973,11,9,11,30]
printf(“%04d-%02d-%02d %02d:%02d\n”, *dateArray)

It works.

I don’t have a class ParseDate with a method parse,
but if it returns an Array, I think

printf(“%04d-%02d-%02d %02d:%02d\n”, *ParseDate.parse(datestring))
^ ^

will work.

I hope, I understand your problem.

Regards

Fred from Wuppertal, Germany

Great! That’s just what I was looking for;
I now could even find it in the book; thanks!

···

On Sat, 15 Nov 2003, ts wrote:

ruby -rparsedate
-e ‘printf(“%04d-%02d-%02d %02d:%02d\n”,
*ParseDate.parsedate(Time.now.to_s))’
2003-11-15 13:31


Wybo