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
[]
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]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.