编辑问题
我有以下方法:
def stats_by_day(league)
league.days_of_league.collect do |day|
roster_for(league).collect do |celeb|
celeb.tweets_this_day(day).inject(0) {|sum, n| sum + n.retweet_count + n.favorite_count}
end
end
end
这给了我:
Day1, Day2, Day3 Day1 Day2 Day3
[345, 647, 567], [344, 222, 675] etc
TEAM1 TEAM2
然而我想要的是
Team1 Team2 Team3 Team1 Team2 Team3
[345, 344, 567] [647, 222, 876]
DAY1 DAY2
我已尝试切换方法的第2行和第3行,但这不起作用。我有我需要的东西来获得我想要的输出吗?
旧问题(供参考)
如果我有......
[[a1,b1,c1],[a2,b2,c2],[a3,b3,c3]]
我怎么会......
[[a1,a2,a3],[b1,b2,b3],[c1,c2,c3]]
寻找红宝石的方法。谢谢!
答案 0 :(得分:4)
如果嵌套数组的大小相同,则可以使用Array#transpose
方法:
ary = [[:a1, :b1, :c1],[:a2, :b2, :c2],[:a3, :b3, :c3]]
ary.transpose
# => [[:a1, :a2, :a3], [:b1, :b2, :b3], [:c1, :c2, :c3]]
答案 1 :(得分:1)
你可以做到
a.flatten.group_by { |x| x[/[a-zA-Z]+/] }.values
=> [["a1", "a2", "a3"], ["b1", "b2", "b3"], ["c1", "c2", "c3"]]