Skip to content

Instantly share code, notes, and snippets.

@zekus
Last active December 14, 2015 20:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save zekus/5141731 to your computer and use it in GitHub Desktop.
Save zekus/5141731 to your computer and use it in GitHub Desktop.
intersect two hashes based on their keys.
class Hash
def intersect_by_keys(another_hash)
delete_if {|k, v| !another_hash.has_key?(k) }
end
end
{a: 'one', b: 'two'}.intersect_by_keys({a: 'three', d: 'four'}) #=> {:a=>"one"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment