I have a hash
h = {a=> 1, b=> 2, c=> 3}
and an array
a = [a, b]
Is it possible to use
h.select {|k,v| k == array_here?}
To select all elements from the array that exists in the hash?
I Found the Solution
h.select {|k,v| a.include?(k) }
You're going about it backwards. Try this:
a.select {|e| h.has_key? e }
You could achieve that with something like:
a.each do |arr_elem|
new_hash[arr_elem] = h[arr_elem] unless h[arr_elem].nil?
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With