Append array indexes in ruby?

For general discussion related FlowStone
Post Reply
User avatar
kortezzzz
Posts: 763
Joined: Tue Mar 19, 2013 4:21 pm

Append array indexes in ruby?

Post 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!
KG_is_back
Posts: 1196
Joined: Tue Oct 22, 2013 5:43 pm
Location: Slovakia

Re: Append array indexes in ruby?

Post by KG_is_back »

Code: Select all

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



This one is a one-liner...
User avatar
kortezzzz
Posts: 763
Joined: Tue Mar 19, 2013 4:21 pm

Re: Append array indexes in ruby?

Post by kortezzzz »

Thanks KG!
One liners are the best 8-)
Post Reply