views:

72

answers:

2

I know two arrays can be zipped and the result can be iterated with #each. But how do you do it with an unknown number of enumerables? Let's say

anand = %w(1-0 0.5-0.5 0.5-0.5 1.0)
carlsen = %w(0-1 0.5-0.5 0.5-0.5 1.0)
kramnik = %w(0.5-0.5 0.5-0.5 0.5-0.5 1.0)
players= [anand, carlsen, kramnik]
#something smart

players.each{|round|puts round} #first line should be  "1-0 0-1 0.5-0.5" 
+3  A: 
anand.zip(carlsen, kramnik) do |round| puts round.join(" ") end

This will print:

1-0 0-1 0.5-0.5
0.5-0.5 0.5-0.5 0.5-0.5
0.5-0.5 0.5-0.5 0.5-0.5
1.0 1.0 1.0
sepp2k
+6  A: 
players.transpose.map {|a| a.join(" ")}
glenn mcdonald
+1: Such a simple solution.
Mark Byers