shops = [
OpenStruct.new(:prefacture => "pre1", :city => "city1"),
OpenStruct.new(:prefacture => "pre1", :city => "city1"),
OpenStruct.new(:prefacture => "pre1", :city => "city2"),
OpenStruct.new(:prefacture => "pre2", :city => "city3"),
]
counts = Hash[shops.group_by(&:prefacture).map do |prefacture, shops_in_prefacture|
[prefacture, Hash[shops_in_prefacture.group_by(&:city).map do |city, shops_in_city|
[city, shops_in_city.size]
end]]
end]
# {"pre1"=>{"city1"=>2, "city2"=>1}, "pre2"=>{"city3"=>1}}