[ruby] How to assign a value to a hash by referring to the value and key of another hash

It's not a big story, but I was pretty confused at first.

For example, suppose you have the following hash.

key value
Taro Miyagi
Jiro Aomori
Hanako Okinawa

This can be simply written as hash = {"Taro "=>" Miyagi "," Jiro "=>" Aomori "," Hanako "=>" Okinawa "}, but there is also such a hash. And.

key value
Miyagi 400 yen
Aomori 700 yen
Okinawa 1200 yen

I'm not sure if the shipping cost for each prefecture is about this, so please forgive me for the random numbers, but it looks like a table like the shipping cost for each prefecture. This is also like ref = {"Miyagi "=> 400," Aomori "=> 700," Okinawa "=> 1200}.

As a reminder, I couldn't find a description of the case where I wanted to convert it to a hash of a name and shipping pair.

How you commented

I used to do the annoying thing of converting it to a two-dimensional array and then back to the hash again, but it was better to use transform_values, a method that allows you to change all the hash values ​​directly. Thank you for telling me.

list = hash.transform_values{ |x| ref[x] }

p list
# { "Taro" => 400 , "Jiro" => 700, "Hanako" => 1200 }



But it seems to be okay. smart!

How I did it myself

list = hash.to_a.each{|x| x[1] = ref[x[1]]}.to_h 

p list
# { "Taro" => 400 , "Jiro" => 700, "Hanako" => 1200 }

I want to process them in order, so I just need to convert them to a two-dimensional array with to_a, put the hash value of ref in the second (x [1]) in order, and finally convert it to a hash.

It's simple, but it's surprisingly confusing. I think there might be an easier way (there was).

Recommended Posts

[ruby] How to assign a value to a hash by referring to the value and key of another hash
[Ruby] I want to extract only the value of the hash and only the key
[Ruby] How to retrieve the contents of a double hash
[Ruby] How to get the value by specifying the key. Differences between hashes, symbols and fetch
[Ruby] How to use the map method. How to process the value of an object and get it by hash or symbol.
Use the where method to narrow down by referring to the value of another model.
How to retrieve the hash value in an array in Ruby
How to pass the value to another screen
How to change the value of a variable at a breakpoint in intelliJ
How to find the cause of the Ruby error
How to operate IGV using socket communication, and the story of making a Ruby Gem using that method
[Note] [Beginner] How to write when changing the value of an array element in a Ruby iterative statement
[Ruby] How to find the sum of each digit
[Java] How to get the maximum value of HashMap
[Ruby] How to get the tens place and the ones place
How to find the total value, average value, etc. of a two-dimensional array (multidimensional array)-java
How to write offline real time Implementation example by ruby and C99 of F04
How to request by passing an array to the query with HTTP Client of Ruby
Takes out the HashMap value and returns the result sorted by the value of value as a list.
Pass arguments to the method and receive the result of the operation as a return value
Sample code to assign a value in a property file to a field of the expected type
[Ruby] When you want to assign the result obtained by conditional branching to a variable and put it in the argument
How to use a foreign key with FactoryBot ~ Another solution
Determine that the value is a multiple of 〇 in Ruby
The nth and n + 1st characters of a Ruby string
How to change the setting value of Springboot Hikari CP
How to move another class with a button action of another class.
[Rails] How to display the list of posts by category
How to check the extension and size of uploaded files
[jsoup] How to get the full amount of a document
[Ruby] I want to reverse the order of the hash table
How to deal with different versions of rbenv and Ruby
About the behavior of ruby Hash # ==
[Rails] How to get the URL of the transition source and redirect
[Swift5] How to get an array and the complement of arrays
How to set the IP address and host name of CentOS8
How to display 0 on the left side of the standard input value
How to make a key pair of ecdsa in a format that can be read by Java
How to use the certificate and private key created by Docker's BASIC authentication with AWS ALB
How to get the id of PRIMAY KEY auto_incremented in MyBatis
[Swift5] How to create a .gitignore file and the code that should be written by default
Ruby memorandum (acquisition of key value)
[chown] How to change the owner of a file or directory
[Swift5] How to communicate from ViewController to Model and pass a value
How to pick up the input value by asynchronous communication and output the desired data from the server
How to check for the contents of a java fixed-length string
I want to change the value of Attribute in Selenium of Ruby
Regarding hash key and value mapping
How to increment the value of Map in one line in Java
How to output the sum of any three numbers, excluding the same value
How to return a value from Model to Controller using the [Swift5] protocol
The value is taken out from the double hash and output by the getter.
I tried to summarize the key points of gRPC design and development
How to find out the Java version of a compiled class file
[Java] How to get to the front of a specific string using the String class
Sample program that returns the hash value of a file in Java
How to solve the local environment construction of Ruby on Rails (MAC)!
How to get the absolute path of a directory running in Java
[Java improvement case] How to reach the limit of self-study and beyond
[Rails] How to create a table, add a column, and change the column type
Android development, how to check null in the value of JSON object