Search⌘ K

Exercise 3: Sum the Rows of a Matrix

Explore how to sum each row in a matrix represented as a nested array using Ruby blocks. This lesson helps you understand iterators and return values in practice, building your skills in Ruby programming with arrays and blocks.

We'll cover the following...

Problem statement

In this exercise, you’re given a nested array. Sum the entries in each row of the matrix and return an array containing these sums.

Example

For example, consider the following array:

nested_array = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9]
]

We get the following output in return:

[6, 15, 24]

Try it yourself

Ruby
def sum_the_cols(nested_array)
result = []
# Start your code here
return result
end