Even for the same fruit, the amount of money, such as what is said to be high-class, size, and closeout, will change. I would like to make such a calculation.

```
fruits_price = [["Apple", [200, 250, 220]], ["Orange", [100, 120, 80]], ["Grape", [1200, 1500]]
```

This time, the array is included in the array, and the name of the fruit and the total amount of each are output.

First, use the each statement to retrieve each fruit one by one. Also, describe the process for adding the total amount.

```
fruits_price = [["Apple", [200, 250, 220]], ["Orange", [100, 120, 80]], ["Grape", [1200, 1500]]
fruits_price.each do |f|
sum = 0
end
```

With this description

```
["Apple", [200, 250, 220]
["Orange", [100, 120, 80]
["Grape", [1200, 1500]
```

I will make it possible to take it out in the form of. Next, I will describe the process of adding the amount of each fruit.

```
f[1].each do |p|
sum += p
end
```

Extracts the values one by one from the first value of f [200, 250, 220], self-assigns, and finally outputs sum.

The final description is

```
fruits_price = [["Apple", [200, 250, 220]], ["Orange", [100, 120, 80]], ["Grape", [1200, 1500]]
fruits_price.each do |f|
sum = 0
f[1].each do |p|
sum += p
end
puts "#{f[0]}The total amount of#{sum}It's a yen"
end
```

By the way, f [0] contains [apple], [orange], [grape], and f [1] contains [200, 250, 220], [100, 120, 80], [1200, 1500]. After processing the apples, process the oranges and then the grapes in that order.

Recommended Posts