Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save khalilgharbaoui/e6123c915109b03d47aac580cbca9afd to your computer and use it in GitHub Desktop.
Save khalilgharbaoui/e6123c915109b03d47aac580cbca9afd to your computer and use it in GitHub Desktop.
Ruby Hash#has_keys?
class Hash
def has_keys?(*keys)
keys.inject(self) do |pointer, key|
return false if !pointer.respond_to?(:has_key?) || !pointer.has_key?(key)
pointer[key]
end
true
end
end
h = {:one => {:two => :three}}
p h.has_keys?(:one, :two)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment