How do I replace age with 31?
[{"name"=>"Bob"}, {"age"=>"30"}]
A Hash is a dictionary-like collection of unique keys and their values. Also called associative arrays, they are similar to Arrays, but where an Array uses integers as its index, a Hash allows you to use any object type. Hashes enumerate their values in the order that the corresponding keys were inserted.
Another way, using find
1.9.3p194 :007 > array1 = [{"name"=>"Bob"}, {"age"=>"30"}]
=> [{"name"=>"Bob"}, {"age"=>"30"}]
1.9.3p194 :008 > hash1 = array1.find { |h| h['age'] == "30" }
=> {"age"=>"30"}
1.9.3p194 :009 > hash1['age'] = 31
=> 31
1.9.3p194 :010 > array1
=> [{"name"=>"Bob"}, {"age"=>31}]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With