optimized Ruby reduce