ruby on rails - I have two hash and want to check whether the key in first hash exist in second hash -


hash1 = simple_hash = {"month" => "january", "number" => "1"} hash2 = {"number" => "2"} 

want check whether second hash contains key in first hash.

i using this, not approach.

simple_hash.each |k,v|   hash2.each |k1,v1|     if k1==v1       puts k1     end   end end 

to common keys:

hash1.keys & hash2.keys # => ["number"]  

incase hash2 has single key:

hash1.keys.include?(hash2.keys.first) # => true 

if interested in:

want check whether second hash contains key in first hash.

(irrespective of key). should sufficient:

!(hash1.keys & hash2.keys).empty? # => true 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -