Looking up a hash in an array

I have created a STATES constant in my application-an array of hashes:

STATES = [
{‘code’ => ‘AL’, ‘name’ => ‘Alabama’},
{‘code’ => ‘AK’, ‘name’ => ‘Alaska’},
{‘code’ => ‘AZ’, ‘name’ => ‘Arizona’},
{‘code’ => ‘AR’, ‘name’ => ‘Arkansas’},
{‘code’ => ‘CA’, ‘name’ => ‘California’},
etc.
]

I’m storing the state code in the database with an object (i.e.,
event, address, etc.) When I retrieve, say, an Event from the
database, I want to use the code ‘AZ’ to look up the name ‘Arizona’
and display it.

I don’t seem to be able to get my head around how to accomplish this.
Maybe it’s because it’s late and I’m really tired…

STATES.find{ |state| state[“code”] == “AL” }[“name”]
=> “Alabama”

The way you currently have your codes setup you can not locate
anything by state code.

Just use a hash as follows:

states = {‘AL’ => ‘Alabama’, ‘AK’ => ‘Alaska’, …

then you can locate the name by states[code].

Michael