Reputation: 4393
I'm still learning Ruby and I have a question concerning hash of hashes. The hash bellow is what I would like to access :
reserved_instance_price = [
'us-east-1' => ['t1.micro' => 0.02, 'm1.small' => 0.08, 'm1.medium' => 0.160 ],
'us-west-1' => ['t1.micro' => 0.02, 'm1.small' => 0.08, 'm1.medium' => 0.160 ],
'eu-west-1' => ['t1.micro' => 0.02, 'm1.small' => 0.085, 'm1.medium' => 0.170 ]
]
My questions: Is it the right way to implement hashes of hashes in ruby ? and how to access a particular value ?
Thank you
Upvotes: 1
Views: 113
Reputation: 4115
[]
syntax is for arrays. To construct hashes use {}
Your example becomes
reserved_instance_price = {
'us-east-1' => {'t1.micro' => 0.02, 'm1.small' => 0.08, 'm1.medium' => 0.160 }
}
For accessing a particular value simply do
reserved_instance_price['us-east-1']['t1.micro']
which will return 0.02
If you would like your indexes to be symbols rather than string, you can also use the syntax
h = { useast1: {t1micro: 0.02}}
Access becomes
h[:useast1][:t1micro]
Upvotes: 3