How to "zip" two arrays into hash
arrays, hashmap, ruby
Solution
I would do it this way:
keys = ['BO','BR']
values = ['BOLIVIA','BRAZIL']
Hash[keys.zip(values)]
# => {"BO"=>"BOLIVIA", "BR"=>"BRAZIL"}
If you want symbols for keys, then:
Hash[keys.map(&:to_sym).zip(values)]
# => {:BO=>"BOLIVIA", :BR=>"BRAZIL"}
In Ruby 2.1.0 or higher, you could write these as:
keys.zip(values).to_h
keys.map(&:to_sym).zip(values).to_h
As of Ruby 2.5 you can use `.transform_keys`:
Hash[keys.zip(values)].transform_keys { |k| k.to_sym }
Problem
I want to "zip" two arrays into a Hash. From: ``` ['BO','BR'] ['BOLIVIA','BRAZIL'] ``` To: ``` {BO: 'BOLIVIA', BR:'BRAZIL'} ``` How can I do it?