I've got a variable I'd like to use as a key to a hash that contains its own key and array.
e.g.
custArray = Array.new
custArray << {"c1001" => {"purchases" => ["prod01"]}}
I want to be able to do something like:
if custArray[:c1001].exists?
custArray[{:c1001["purchases"]} << "prod02"]
end
but I'm just totally stuck.
You can resolve it with:
if c = custArray.find { |h| h.key? 'c1001' }
c.dig('c1001', 'purchases') << "prod2"
end
Or if you can have more than one result with this key:
custArray.select { |h| h.key? 'c1001' }.each do |c|
c.dig('c1001', 'purchases') << "prod2"
end
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