0
votes

I have an usecase where i wanted to fetch the keys from hash. I wanted to return the all keys which are present in the array.

Example:

h = {"video"=>"MP4","audio"=>"MP3", "sharing"=>"NONE", "mix"=>"NONE"}

a = ["video", "audio", "txt"]

Expected result:

h.slice(a)
["video","audio"]

I am trying to use slice method on hash but it is not working for me.

Any help would be appreciated.

do u want to fetch only keys or keys with their values? - Mehmet Adil İstikbal
You want a = ["video", "audio", "txt"] ({"video", "audio", "txt"} is an invalid expression). If you want all keys of h that are in a, h.keys & a #=> ["video", "audio"]. If you want all key-value pairs in h for which the key is in a, h.slice(*a) #=> {"video"=>"MP4", "audio"=>"MP3"}. - Cary Swoveland