MethodQuery gives you the ability to check for the truthiness of a key via method calls. Note that it will return false if the key is set to a non-truthful value, not if the key isn't set at all. Use key? for checking if a key has been set.
MethodQuery will check against both string and symbol names of the method for existing keys. It also patches respond_to to appropriately detect the query methods.
@example
class MyHash < Hash include Hashie::Extensions::MethodQuery end h = MyHash.new h['abc'] = 123 h.abc? # => true h['def'] = nil h.def? # => false h.hji? # => NoMethodError
# File lib/hashie/extensions/method_access.rb, line 104 def method_missing(name, *args) if args.empty? && name.to_s =~ /(.*)\?$/ && (key?($1) || key?($1.to_sym)) return self[$1] || self[$1.to_sym] end super end
# File lib/hashie/extensions/method_access.rb, line 99 def respond_to?(name, include_private = false) return true if name.to_s =~ /(.*)\?$/ && (key?($1) || key?($1.to_sym)) super end