[Ruby] Review about nesting of each

Overview

If it is an "array", it can be easily retrieved, but if it is an "array inside an array",

――I think a little ――I can't get the value I want

It is an article for those who say. I hope you find it helpful.

table of contents

--Practice --Problem --Answer (commentary) --Process

--Summary --References

Practice

problem

Output the fruit name and total amount from the following array.

problem


fruits_price = [["apple", [200, 300, 400]], ["banana", [100, 200, 300]], ["grape", [2000, 3000]]]

Answer (commentary)

Answer example


fruits_price.each do |fruit|
# fruits_Extract the array one by one from price and extract it with fruit(By using each, you can process the array in the array.)

  sum = fruit[1].sum
# fruit[1](Amountpart)sum(Totalamount)

  puts "#{fruit[0]}The total amount of#{sum}It's a yen"
#output
end

Another solution


fruits_price.each do |fruits, prices|
  puts "#{fruits}The total amount of#{prices.sum}It's a yen"
end

process

** 1. Think about what you want to get **

--Fruit name --Total amount of each fruit

** 2. How to retrieve elements for arrays **

--Retrieve with each ――What number of them to get

** 3. How to get the total value **

--Output sum of elements with sum

Summary

--Think about what value you want to take --Isolate the problem and find out how to output it. --You can easily get the sum of elements with sum

References

-Ruby 3.0.0 Reference Manual (sum)

Recommended Posts

[Ruby] Review about nesting of each
[Ruby] Nesting each
[Ruby] Various types of each
Review of Ruby basic grammar
About the behavior of ruby Hash # ==
Ruby Review 2
Extraction of "ruby" double hash * Review
Basics of Ruby ~ Review of confusing parts ~
[Ruby] Review about nesting of each
Explanation about Array object of Ruby
Ruby Basics 2 ~ Review of confusing parts ~
Ruby Review 1
Gem list of each ruby version 2.5.x
Review about bundler
About Ruby symbols
About ruby ​​form
About Ruby Hashes
[Ruby] each nested
Basics of Ruby
About Ruby arrays
About Ruby inheritance
About ruby block
About Ruby Hashes
[Output] About each
About Ruby Symbols
About Ruby variables
About Ruby methods
About Ruby Kernel Module
About Ruby error messages
About Ruby exception handling
About disconnect () of HttpURLConnection
About Ruby Hashes (continued)
About eval in Ruby
Review of java Shilber
[ruby] About here documents
[Ruby] How to find the sum of each digit
About selection of OpenJDK
About Ruby if statement
Nested structure of each
About Ruby instance methods
About DI of Spring ②
A note about th: field th: each th: object of thymeleaf
definition of ruby method
[Java] About Objects.equals () and Review of String comparisons (== and equals)
[Technical memo] About the advantages and disadvantages of Ruby
Ruby cherry book review
NIO review of java
[Ruby] Class nesting, inheritance, and the basics of self
About form. ○○ of form_with
About @Accessors of Lombok
About the [ruby] operator
Thinking about logic Ruby
Review the basic knowledge of ruby that is often forgotten
[Ruby] Questions and verification about the number of method arguments
A note about the seed function of Ruby on Rails
[Ruby] About the difference between 2 dots and 3 dots of range object.
Basic methods of Ruby hashes
About the handling of Null
Basic methods of Ruby arrays
About simple operation of Docker
About the description of Docker-compose.yml