Forum: Ruby two arrays problem (although different from the other two ar

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Kev J. (Guest)
on 2006-03-29 10:49
(Received via mailing list)
I have a bunch of japanese encoded words in Kanji and Hirigana (I
think).  Each set is broken into two pieces, name and type, which can be
combined to produce many more words, so this looks like a classic
'iterate over two arrays problem'

This is what I have, I tried messing with inject and I couldn't get the
desired output.  If anyone wants to golf it I'd like to learn what I
should be doing instead

  corp_data_1.each do |cd1|
      corp_data_2.each do |cd2|
        rd << cd1 + cd2

ChrisH (Guest)
on 2006-03-29 12:31
(Received via mailing list)
Kev J. wrote:
>       corp_data_2.each do |cd2|
>         rd << cd1 + cd2
>       end
>     end
> Thanks
> Kev

Not necessarily better but:

rd ={|cd1|{|cd2| cd1 + cd2}}

unknown (Guest)
on 2006-03-29 20:04
(Received via mailing list)
I'm not really clear what you're trying to do. Could you provide a bit
more detail? Do the elements of corp_data1 and corp_data2 correspond to
one another or are they independent? Are you trying to get output with
the same number of elements, or with a number of elements equal to the
product of the two (a sort of cartesian product)?

If it's the former, you should be able to do it with zip and map, if
it's the latter, it seems like what you have should be appropriate.
This topic is locked and can not be replied to.