Here is a summary of how to use the each method in a nested manner for learning.

- What the each method is is not mentioned in this content.

Ruby 2.6.5

As an example, for the following sequence

```
vegetables_price = [["tomato", [120, 150, 80]], ["potato", [50, 60, 40]], ["carrot", [120, 150]]]
```

Suppose you want the following output result.

```
The total amount of tomato is 350 yen
The total amount of potato is 150 yen
The total amount of carrot is 270 yen
```

Description

```
vegetables_price = [["tomato", [120, 150, 80]], ["potato", [50, 60, 40]], ["carrot", [120, 150]]]
vegetables_price.each do |vegetable| #Array vegetables_Use each method for price.
sum = 0 #Define a variable sum to store the total value
vegetable[1].each do |price| #Variable vegetable[1]Use each method for.[1]Has a price.
sum += price #Turn each to add the price.
end
puts "#{vegetable[0]}The total amount of#{sum}It's a yen" #vegetable[0]Contains the name of vegetable.
end
```

- Remarks The subscript 0 is the name of the vegetable, and 1 is the price.

```
vegetables_price = [["tomato", [120, 150, 80]], ["potato", [50, 60, 40]], ["carrot", [120, 150]]]
(Subscript) 0 1
```

that's all

Recommended Posts