I am new to Ruby. I have a (already sorted) hash with the following values. I want to put the keys of these in multidimensional array on the basis of there values. So, for example
For
VALUES - KEYS
acemrs - a
acemrs - b
acrs - c
acrs - d
acrs - e
aeoopstt - f
for - g
foru -h
I am looking for an array like [ [a,b] , [c,d,e] , [f] , [g] , [h] ]
We use the previous hash
valuesaskeysfor the new hash. If undefined, we create a new array for each newkeyswithnew_hash[value] ||= []then just push all keys in the new hash with<< key. The new hash will be something likeAt the end, you just want those values so
new_hash.valuesand you are done.Hash API.