ruby - Create an array from equal values between hash's keys and an array -


i need create new array equal values between hash's keys , array example, have this:

my_array = [1,2,3,4,5,6,7,8,9,10] my_hash  = { 2 => "two", 5 => "five", 7 => "seven", 10 => "ten" } 

and then, need new array contains equivalent values. in example, it’ll be:

equal_value = [2,5,7,10] 

how that?

equal_value = my_array.select{|e| my_hash.key?(e)} 

or (perhaps slower)

equal_value = my_array & my_hash.keys 

Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -