To sum the values in the given array of hashes in Ruby, you can use the reduce method in combination with the values method. Here's the code snippet to achieve that:
main.rb124 chars8 lines
Output:
main.rb4 chars2 lines
Explanation:
sum variable to 0.reduce to iterate over each hash in the array, starting from the initial value of sum (0).values to get an array of its values and then use sum to calculate the sum of those values.sum by adding the calculated sum of values.sum.Note that in the provided example, the second hash is empty, so it does not contribute to the sum.
gistlibby LogSnag