Tuesday, 15 July 2014

ruby - How do I compare hashes and output duplicates? -



ruby - How do I compare hashes and output duplicates? -

this question has reply here:

merge 2 ruby hashes no new keys 5 answers

i have 3 hashes, , want output keys mutual them:

hash1 = {a: "3", b: "2" c: "1"} hash2 = {b: "2" c: "1"} hash3 = {b: "2" c: "1", d: "4"}

for illustration above, mutual keys b , c.

i need scale many hashes. efficient , scalable way this?

> hashes = [{a: 3, b: 4}, {a: 5, c: 6}, {a: 6, d: 2}] => [{:a=>3, :b=>4}, {:a=>5, :c=>6}, {:a=>6, :d=>2}] > hashes.map(&:keys).inject { |shared, keys| shared & keys } => [:a]

ruby hash

No comments:

Post a Comment