diff 开眼界
Galaxy Note 2
Programmming Ruby 1.9 更好,阅读起来更有乐趣 Ruby Programming Language 也还不错
单行方案:
[a1, a2, a3].transpose.map{ |x| x.reduce(&:+) }
还是 Rails 的 sum 方法更优美、自然:
module Enumerable
def sum(identity = 0, &block)
if block_given?
map(&block).sum(identity)
else
inject { |sum, element| sum + element } || identity
end
end
end
[a1,a2,a3].transpose.map(&:sum)
其实 Python 的也不错:
map(sum, zip(a1, a2, a3))