Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if an hash is "completely" included in another hash?

Tags:

I am using Ruby on Rails 3.1.0 and I would like to check if an hash is "completely" included in another hash and return a boolean value.

Say I have those hashes:

hash1 = {   :key1 => 'value1',   :key2 => 'value2',   :key3 => 'value3' }  hash2 = {   :key1 => 'value1',   :key2 => 'value2',   :key3 => 'value3',   :key4 => 'value4',   :key5 => 'value5',   ... } 

I would like to check if the hash1 is included in the hash2 even if in the hash2 there are more values than hash1 (in the above case the response that I am looking for should be true)? Is it possible to do that by using "one only code line"\"a Ruby method"?

like image 760
user12882 Avatar asked Sep 28 '11 14:09

user12882


1 Answers

That will be enough

(hash1.to_a - hash2.to_a).empty? 
like image 124
fl00r Avatar answered Sep 28 '22 01:09

fl00r