[RAILS] [Ruby] Extracting a two-dimensional array

This article uses Ruby 2.6.5 installed on macOS Catalina 10.15.6.

What is a two-dimensional array?

--_ This is a form in which an array is contained in an array __. --Each item is stored as an array in the hash of items_price. ――In addition, there is an array of names and prices in each item.

items_price = [["pen", [100, 200, 120]], ["book", [120, 150, 220]], ["pen_case", [1000, 1500]]]

To retrieve a two-dimensional array

--Use each statement to retrieve the hash. ――This time it's two-dimensional, so you use each statement twice.

items_price = [["pen", [100, 200, 120]], ["book", [120, 150, 220]], ["pen_case", [1000, 1500]]]

items_price.each do |item|
  sum = 0
  item[1].each do |price|
    sum += price
  end
  puts "#{item[0]}The price of#{sum}It is a circle."
end

Let's take pen as an example and look at it one by one.

1st each (2nd line)

items_price.each do |item| ~ end
# => items_In the price array["pen", [100, 200, 120]]Is taken out and assigned to the block variable item

2nd each (4th line)

item[1].each do |price| ~ end
# =>In the item array[100, 200, 120]And assign it to the block variable price

--By the way, item [1] is the number of elements in the price array, that is, it will be repeated 3 times here.

Output

puts "#{item[0]}The price of#{sum}It is a circle."

--item [0] is the "pen" part of the item array with 0 subscripts and ["pen", [100, 200, 120]].

result

The price of the pen is 420 yen.
The price of the book is 490 yen.
pen_The price of the case is 2500 yen.

Recommended Posts

[Ruby] Extracting a two-dimensional array
Ruby two-dimensional array
Multiplication in a Ruby array
[Ruby] Array
Sorting hashes in a Ruby array
[Ruby] Extracting double hash in array
What is a Ruby 2D array?
How to divide a two-dimensional array into four with ruby
Learning Ruby with AtCoder 13 How to make a two-dimensional array
Ruby array manipulation
[Ruby] Calculation by extracting elements from an array
Calculate the difference between numbers in a Ruby array
I got stuck in a clone of a two-dimensional array
Ruby Learning # 2 Drawing a Shape
What is a Ruby module?
<Java> When creating a fixed-length (row) * variable-length (column) two-dimensional array
How to change a string in an array to a number in Ruby
Creating a calendar using Ruby
Ruby Learning # 11 Building a Calculator
Create a fortune using Ruby
Ruby Learning # 32 Building a Quiz
[Ruby] Get array elements alternately.
Ruby ① Build a Windows environment
Cut out a Ruby string
Try to imitate the idea of a two-dimensional array with a one-dimensional array
[Ruby] I want to put an array in a variable. I want to convert to an array
Ruby Learning # 19 Building a Better Calculator
Ruby: I made a FizzBuzz program!
[Ruby] It's a convenient guy ~ before_action ~
[Ruby] Extracting elements with slice method
Ruby Learning # 22 Building a Guessing Game
Implement a gRPC client in Ruby
Hanachan in Ruby (non-destructive array manipulation)
Explanation about Array object of Ruby
How to make a Java array
Make a typing game with ruby
[Java] How to turn a two-dimensional array with an extended for statement