Skip to content

Instantly share code, notes, and snippets.

@scalabl3
Created July 23, 2012 00:09
Show Gist options
  • Save scalabl3/3161435 to your computer and use it in GitHub Desktop.
Save scalabl3/3161435 to your computer and use it in GitHub Desktop.
Even better than has_key?, is has_key_and_value?
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