Ruby: Efficiently check if a hash's keys include all elements of an Array.

Posted by Jason on Stack Overflow See other posts from Stack Overflow or by Jason
Published on 2010-05-05T01:36:11Z Indexed on 2010/05/05 2:18 UTC
Read the original article Hit count: 170

Filed under:
|
|
|
|

I want to compare the keys in a hash of parameters against an array of elements for a match.

For example:

params          = {"key1", "key2", "key3"}
params_to_match = ["key2","key3"]

I could do this, but I'm sure there is a much more elegant way to acheive the same result

params.each_key{|key|
  if params_to_match.include?(key.to_s)
    return
  end
}

© Stack Overflow or respective owner

Related posts about ruby

Related posts about algorithm