[Solved] hash inside liste of hash extracted from array [closed]


I’m not sure what exactly you want to do, but you can try the following:

arr = ["Japan", "Egypt", "Spain", "Brazil"]
arr.each { |a| Object.const_set(a, Hash.new(a)) } #initialize hashes

Country = arr.map {|a| [a, 0]}.to_h
#=> {"Japan"=>0, "Egypt"=>0, "Spain"=>0, "Brazil"=>0}

or

Country = Hash[arr.map {|a| [a, 0]}]
#=> {"Japan"=>0, "Egypt"=>0, "Spain"=>0, "Brazil"=>0}

Note: Ruby variables should be in lowercase like japan and egypt and constants in uppercase like JAPAN and EGYPT.

2

solved hash inside liste of hash extracted from array [closed]