How do I summarize hashes within a hash?

Viewed 184

I have a hash like this:

Some_hash =
    {"Albania"=>"Europe", 
    "Andorra"=>"Europe", 
    "Austria"=>"Europe",
    Lebanon"=>"Asia", 
    "Macau"=>"Asia", 
    "Malaysia"=>"Asia",
    "Papua New Guinea"=>"Asia",
    "Jamaica"=>"North America",
    "Martinique"=>"North America",
    "Argentina"=>"South America",
    "Chile"=>"South America", 
    "Sao Tome and Principe"=>"Africa", 
    "Senegal"=>"Africa",
    "Somalia"=>"Africa",}

I would like to identify the five continents individually, and the countries that belong to them, such that I end up with something like this:

{"Africa" => ["Senegal", "Somalia"]}
{"Europe" => ["Albania", "Andorra", "Austria"]}

for all of the continents.

I tried this:

def country
  inflation_hash = {}
  XPath.match( data, "//country").map do |element|
    inflation_hash[element.attributes["name"]] = element.attributes["continent"]
  end
  inflation_hash.each do |country, continent|
    new_hash = {}
    if inflation_hash.has_value?("Africa") == true
      new_hash["Africa"] = inflation_hash.keys
      puts new_hash
    end
  end
end

and it works much too well. I get a new hash:

{Africa => []} 

but I have two problems:

  1. I create a new hash for each African country.
  2. Each new hash contains all the keys, which includes all non-African countries.

I think the first problem has to do with the each method so I have to set some conditional, right?

The second problem, I have no idea how to fix.

Any pointers would be greater appreciated.

5 Answers
Related