Nested reductions alternative in ruby