I have a hash that maps integers to arrays. For example
{1 => ["abc"], 2 => ["ccc", "ddd"]}
How do I get all the keys from my hash that have arrays with at least 2 elements in them?
I have a hash that maps integers to arrays. For example
{1 => ["abc"], 2 => ["ccc", "ddd"]}
How do I get all the keys from my hash that have arrays with at least 2 elements in them?
{1 => ["abc"], 2 => ["ccc", "ddd"]}.select{|_, a| a.length > 1}.keys
# => [2]
Anything like this?
hash.each_key.select { |key| hash[key].count >= 2 }
One more possible solution :)
{1 => ["abc"], 2 => ["ccc", "ddd"]}.map { |k, v| k if v.size > 1 }.compact
# => [2]