"adi","2"=>"amar","3"=>"rave","4"=>"sum"} arr=["1","5","3"] I want ...">

Get multiple values ​​from a hash in a very efficient way

My code

a={"1"=>"adi","2"=>"amar","3"=>"rave","4"=>"sum"} arr=["1","5","3"] 

I want to extract all values ​​like this if array values ​​exist in hash

 result =["adi","rave"] 

without using any cycle. Is it possible

+8
ruby hash
source share
1 answer

You can do:

 a.values_at(*arr).compact # => ["adi", "rave"] 
+27
source share

All Articles