Last active
April 1, 2017 20:03
-
-
Save markedmondson/b0fe71351e3246932bac to your computer and use it in GitHub Desktop.
States
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
canada = OpenStruct.new(name: "Canada", iso: "CA") | |
usa = OpenStruct.new(name: "United States", iso: "US") | |
australia = OpenStruct.new(name: "Australia", iso: "AU") | |
all_countries = [ | |
canada, | |
usa, | |
australia | |
] | |
all_states = [ | |
bc = OpenStruct.new(name: "British Columbia", iso: "BC", fips: nil, country: canada), | |
on = OpenStruct.new(name: "Ontario", iso: "ON", fips: nil, country: canada), | |
qc = OpenStruct.new(name: "Quebec", iso: "QC", fips: nil, country: canada), | |
og = OpenStruct.new(name: "Oregon", iso: nil, fips: "US.OR", country: usa), | |
wa = OpenStruct.new(name: "Washington", iso: nil, fips: "US.WA", country: usa), | |
ca = OpenStruct.new(name: "California", iso: nil, fips: "US.CA", country: usa), | |
wtf = OpenStruct.new(name: "Western Australia", iso: "WA", fips: nil, country: australia) | |
] | |
state_counts = [ | |
OpenStruct.new(name: "BC", country: "Canada", count: 5), | |
OpenStruct.new(name: "Ontario", country: "CA", count: 15), | |
OpenStruct.new(name: "QC", country: nil, count: 25), | |
OpenStruct.new(name: "Oregon", country: "US", count: 35), | |
OpenStruct.new(name: "WA", country: "United States", count: 45), | |
OpenStruct.new(name: "CA", country: nil, count: 55) | |
] | |
# Current solution | |
all_states.inject(Hash.new) do |memo, state| | |
count = state_counts.find(name: state.name).first.count || state_counts.find(iso: state.name).first.count || 0 | |
memo[state.iso] = count | |
memo | |
end | |
# => {bc => 5, on => 15, qc => 25, og => 35, wa => 45, ca => 55} |
because all_states is an array, those assignation are unnecessary IMHO..
I just used :
all_states = [
OpenStruct.new(name: "British Columbia", iso: "BC", fips: nil, country: canada),
OpenStruct.new(name: "Ontario", iso: "ON", fips: nil, country: canada),
OpenStruct.new(name: "Quebec", iso: "QC", fips: nil, country: canada),
OpenStruct.new(name: "Oregon", iso: nil, fips: "US.OR", country: usa),
OpenStruct.new(name: "Washington", iso: nil, fips: "US.WA", country: usa),
OpenStruct.new(name: "California", iso: nil, fips: "US.CA", country: usa),
OpenStruct.new(name: "Western Australia", iso: "WA", fips: nil, country: australia)
]
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
or (Oregon) is a reserved keyword