I am learning hashes and haven't figured out why it won't print the last line (capital_city
).
more_states = {
"Oklahoma": "OK",
"Texas": "TX",
"Colorado": "CO",
"Kansas": "KS",
"New Mexico": "NM"
}
puts "-" * 10
# Printing states and their abbreviations
more_states.each do |state, abbrev|
puts "The abbreviation for #{state} is #{abbrev}"
end
more_cities = {
"OK": "Oklahoma City",
"TX": "Austin",
"CO": "Denver",
"KS": "Topeka",
"NM": "Santa Fe"
}
puts "-" * 10
# Printing state abbreviations and the corresponding state capitals
more_cities.each do |abbrev, capital|
puts "#{capital} is the capital of #{abbrev}"
end
puts "-" * 10
# Printing states, their abbreviations, and their capitals
more_states.each do |state, abbrev|
capital_city = more_cities[abbrev]
puts "The abbreviation for #{state} is #{abbrev}, and its capital is #{capital_city}"
end
My first two puts
are working fine, but when I use puts
within more_states.each
, I'm unable to access more_cities
.
I tried using more_cities.fetch
also:
capital_city = more_cities.fetch(abbrev)
With this, I got an error that it can't find the key "OK" in more_cities
, but that key is definitely in there.
Any suggestions? I feel like it's a syntax error, or I'm missing something.