Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Retrieve value from Array of Hashes Ruby

I have an array of hashes in ruby like this

blah = [{"key1"=>"value1","key2"=>"value2","key3"=>"value3"....}]

Now let’s say I want to get the value of key2.

What I am doing is puts "key 2 is #{blah["key2"]}", but then I get ERROR: "no implicit conversion of String into Integer (TypeError)"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

blah it’s an array so you could have more than one hash inside it with a "key2" key. let’s find them all

p blah.map { |h| h['key2'] }

or if you know there is just one hash in your array

p blah[0]['key2']
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading