[Ruby] How to call multiple names at once in the same category

1 minute read

【Overview】


1. Conclusion

2. What is a double hash?

3. How to use

4. What I learned from here (use in case of error)

  1. Conclusion

    Use each method with double hashes and arrays!


  1. What is a double hash

    It means that there is a hash in the hash That means the following!

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


  1. How to use

    I think the combination of hash and array is easy!

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

You can get the above by just putting the one that was in 2. in the array. Then use the each method when calling the name. If you don’t do that, you won’t be able to call the entire name.

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 recall all 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” of each do end, Each has both 1st and 2nd ways of writing.

  1. What I learned from here

    It’s an array when I want to print all names I thought I should get the information of all sequences (three information of [0] to [2]). And I thought it would be nice to assign the three pieces of information to variables and output them with puts, but I didn’t think there was a point in removing the array. Since each is a process that iterates the array variables one by one, it was a perfect method including dig.