Page 1 of 1

Append array indexes in ruby?

Posted: Wed Sep 28, 2016 3:01 pm
by kortezzzz
Hi,

Is there any way to append array indexes in ruby?

My arrays:

array 1:

red
green
white

array 2:

tomato
frog
snow

I would like to get at the output :

red tomato
green frog
white snow

I guess the comma between the indexes in the final result should be also defined in the ruby code, so would be nice if we could add a 3th input in to this ruby code which would define the comma.
For instance, if the string comma input would be "@", the final result would be then:

red@tomato
green@frog
white@snow

People, it would be even nicer if you post your answer as a code as well. Take into account that most of us are not reasonable coders :)

Thanks!

Re: Append array indexes in ruby?

Posted: Wed Sep 28, 2016 3:32 pm
by KG_is_back

Code: Select all

output @array1.zip(@array2).map{|p| p.join(@comma||"")}



This one is a one-liner...

Re: Append array indexes in ruby?

Posted: Wed Sep 28, 2016 7:34 pm
by kortezzzz
Thanks KG!
One liners are the best 8-)