How to call multiple names at once in the same category

【Overview】

1. Conclusion </ b>

2. What is a double hash </ b>

3. How to use </ b>

4. What I learned from here (used in case of error) </ b>

  1. Conclusion

Use each method with double hashes and arrays!


2. What is a double hash?

The state where the hash is included in the hash In other words, it says the following!

student = {human: {gender: {name: 'John'}}}


3. How to use

I think the combination of hashes and arrays is easy!

students = [
{human: {gender: {name: 'John'}}},
{human: {gender: {name: 'Nick'}}},
{human: {gender: {name: 'Alan'}}},
]

Just put the one in 2. in the array and it will be the above. Then use the each method when calling the name. Otherwise you will not be able to call all the names.

1


students = [
{human: {gender: {name: 'John'}}},
{human: {gender: {name: 'Nick'}}},
{human: {gender: {name: 'Alan'}}},
]

students.each do |student|
 puts  sutudent[:human][:gender][:name]
end
<br>

Now you can call all the names.

Another method is the dig method.

2


(abridgement)

students.each {|student| puts student.dig(:human, :gender, :name)}

It will be written as. There is no "do ~ end" for each do end Each has both No. 1 and No. 2 writing styles.
4. What I learned from here

Because it's an array when you want to output all the names I thought I should get the information of all the sequences (three information of [0] ~ [2]). Then, I thought it would be good to assign the three information to a variable and output it with puts, but I thought it was meaningless to release the array. Since each is a process that repeats the variables of the array one by one, it was a perfect method including dig.

Recommended Posts