# The MethodicHash is some method_missing magic that tries to lookup values # when keys are passed in as method calls. For instance, if # # mh = MethodicHash.new # mh['four'] = 'iv' # mh[:seven] = 'vii' # # then # # mh['four'] ---> 'iv' # mh[:four] ---> 'iv' # mh.four ---> 'iv' # # and # # mh['seven'] ---> 'vii' # mh[:seven] ---> 'vii' # mh.seven ---> 'vii' # # This allows access to simply declared facts to be embedded in Ruby code and # leverages the possibility of hashing procs. # # Note that if the hash uses anything but strings or symbols as keys, the # magic stands a good chance of failing, raising an error or acting in a # bizarre manner. class MethodicHash < Hash # Try to look up using a key directly, or if that fails as a string, or as # a symbol as last resort. If a symbol conversion doesn't exists, rescue # with a nil. def [](key) begin super(key) || super(key.to_s) || super(key.to_sym) rescue nil end end # A missing method is assummed to be the value of a key. def method_missing(method,*args) self[method] end end