Created
August 21, 2019 19:23
-
-
Save khalilgharbaoui/e6123c915109b03d47aac580cbca9afd to your computer and use it in GitHub Desktop.
Ruby Hash#has_keys?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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