Ruby create an array of hashes

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Ruby create an array of hashes



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.





Prefer cust_array over custArray in Ruby. Also, "c1001" and :c1001 are not equivalent hash keys.
– yzalavin
May 7 at 15:11



cust_array


custArray





I suggest that in future you not be in such a rush to select an answer. There's no hurry.
– Cary Swoveland
May 7 at 18:02




5 Answers
5



You can resolve it with:


if c = custArray.find
c.dig('c1001', 'purchases') << "prod2"
end



Or if you can have more than one result with this key:


custArray.select .each do |c|
c.dig('c1001', 'purchases') << "prod2"
end



If you only want to update the first instance of the array you can do:


target = custArray.find hash
target['c1001']['purchases'] << 'prod02' if target



If you want to update all instances of the array you can do (backslashes are for console purposes only):


custArray
.select hash
.each hash



You can use select from the array of hashes to see if the key is there:


target = custArray.find
target['c1001']['purchases'] << "prod02" unless target.nil?



Or if array contains multiple hashes with the same key:


custArray.select .each do |h|
h['c1001']['purchases'] << "prod02"
end





Apologies, I edited the question. That absolutely worked.
– Toph
May 7 at 15:13





I edited my answer as the first answer I gave was incorrect
– lacostenycoder
May 7 at 15:15





See last update. But also be aware, an array may contain multiple hashes that have the same keys. So make sure however you're building your array, those keys are unique. Also prefer snake case in ruby cust_array vs custArray.
– lacostenycoder
May 7 at 15:29


cust_array


custArray





That did the trick! Thanks so much!
– Toph
May 7 at 15:29



Also, you can write something similar to code that you already provide


custArray.each do |h|
h['c1001']['purchases'] << 'prod02' if h.keys.include?('c1001')
end



That allows reducing the count of iteration loops





This solution will also handle if you have duplicate array values with the same hash key c1001
– lacostenycoder
May 7 at 15:41


c1001


custArray.find h&.dig('c1001', 'purchases')&.push("prod02")
#=> ["prod01", "prod02"]
custArray
#=> ["c1001"=>"purchases"=>["prod01", "prod02"]]

custArray.find h.key?('c1002') &.dig('c1002', 'purchases')&.push("prod02")
#=> nil
custArray
#=> ["c1001"=>"purchases"=>["prod01"]]

custArray.find h&.dig('c1001', 'popsicles')&.push("prod02")
#=> nil
custArray
#=> ["c1001"=>"purchases"=>["prod01"]]



& is Ruby's Safe Navigation Operator. See also Hash#dig. Both made their debut in Ruby v2.3.


&






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS