Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
だから私は配列に基づいてフィルタリングしたいハッシュを持っています:
h = {a: 'test1', b: 'test2', c: 'test3'} a = [:a, :poo1, :poo2]
私の最初の考えは、試してみることでした:
h.slice(a)
しかし、{}私がそれが戻ってくることを望んだとき、それは戻ってき{:a=>"test1"}ます。シンボルの配列に基づいてハッシュをフィルタリングする簡単な方法が見つからないようです。考え?
{}
{:a=>"test1"}
これは、splat 演算子を使用して解決できます。
h.slice(*a)
これにより、
> {:a=>"test1"}
アスタリスクの機能については、このリンクを参照してください。