hash and each statement

Strengthening week to crush the part of lack of understanding! So, it seems that one week is not enough.

Lack of understanding of hash (tears)

I knew that I would assign a value to hash The problem is then.

Let's analyze it.

Hash and each statement

スクリーンショット 2020-05-24 16.18.40.png

--The value is assigned to hash in the 1st to 5th lines.

each statement

●hash.each do |key, value|

-By the description of each do, the process is repeated for the number of hash elements. In this case, name, height, weight, BMI 4 times. -After the four processes are repeatedly executed, the process exits the block and ends.

● Advantages of each statement

Since there is no variable initialization or end judgment processing, it is more concise to use each method.

In addition, it is guaranteed that processing will not be executed beyond the range of the object. Since the range to be repeated is fixed, there is less concern about errors.

Output result

If this is output to the terminal, it is successful. スクリーンショット 2020-05-24 16.37.29.png

Each value is output, and the process is completed after being repeated 4 times.

at the end

I don't have enough understanding, so it takes more than an hour to get hungry after checking these contents. Understanding is deepened by outputting. Thank you.

Don't forget what you understand!

Recommended Posts

hash and each statement
About for statement and if statement
☾ Java / Iterative statement and iterative control statement
[Java] Make variables in extended for statement and for Each statement immutable
hash
About if statement and branch processing
Regarding hash key and value mapping
[Ruby] Simplify each using map and inject
[Java] for Each and sorted in Lambda
Basics of java basics ② ~ if statement and switch statement ~