mercredi 6 janvier 2016

Searching inside a hash of hashes, and returning the keys

I have the following hash:

{1=>{"label"=>"New", "color"=>"#whatver"}, 2=>{"label"=>"In-progress", "color"=>"#whatever"}, 3=>{"label"=>"Closed", "color"=>"#whatever"}}

And I have an array like: ['New', 'In-progress']

I need to return the keys [1, 2] in an array from that hash.

What I did try:

labels = ['New', 'In-progress']
labels.map { |label| statuses.detect { |hash| hash.second[:label] == label }.first }
# => [1, 2]

Which is correct and the exact thing I want, but is there a more direct way of doing this?

Aucun commentaire:

Enregistrer un commentaire