Created
July 23, 2012 00:09
-
-
Save scalabl3/3161435 to your computer and use it in GitHub Desktop.
Even better than has_key?, is has_key_and_value?
This file contains 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 | |
# if key doesn't exist, returns false | |
# if self[key] == false, returns false | |
# if self[key] == nil, empty, or blank, returns false | |
# if self[key] exists and self[key] == true, or evaluates to true, returns true | |
def has_key_and_value? (key) | |
return false unless has_key?(key) | |
return false if self[key].nil? | |
return false if self[key].respond_to?(:empty?) && self[key].empty? | |
return false if self[key].respond_to?(:blank) && self[key].blank? | |
self[key] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment