Creating a hash from two arrays with identical values in Ruby -


i'm having issues creating hash 2 arrays when values identical in 1 of arrays. e.g.

names = ["test1", "test2"] numbers = ["1", "2"] hash[names.zip(numbers)] 

works gives me need => {"test1"=>"1", "test2"=>"2"}

however if values in "names" identical doesn't work correctly

names = ["test1", "test1"] numbers = ["1", "2"] hash[names.zip(numbers)]  

shows {"test1"=>"2"} expect result {"test1"=>"1", "test1"=>"2"}

any appreciated

hashes can't have duplicate keys. ever.

if permitted, how access "2"? if write myhash["test1"], value expect?

rather, if expect have several values under 1 key, make hash of arrays.

names = ["test1", "test1", "test2"] numbers = ["1", "2", "3"]  hash.new.tap { |h| names.zip(numbers).each { |k, v| (h[k] ||= []) << v } } # => {"test1"=>["1", "2"], "test2"=>["3"]} 

Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -