You can solve this by using the splat operator:
h.slice(*a)
This will produce
> {:a=>"test1"}
For an explanation what the asterisk does, please see this link.
Pergunta
So I have a hash that I would like to filter based on an array:
h = {a: 'test1', b: 'test2', c: 'test3'}
a = [:a, :poo1, :poo2]
My first thought was to try:
h.slice(a)
But that returns {}
, when I hoped it would return {:a=>"test1"}
. I can't seem to find a simple way of filtering my hash based on an array of symbols. Thoughts?
Solução
You can solve this by using the splat operator:
h.slice(*a)
This will produce
> {:a=>"test1"}
For an explanation what the asterisk does, please see this link.