Sha256: c11194743e3aa403911bdc3f91fb854ec4fecfe15886b55c57e2a59a10b0fc52
Contents?: true
Size: 887 Bytes
Versions: 8
Compression:
Stored size: 887 Bytes
Contents
class ExtendHash < Hash class << self def [](hash) case hash when ExtendHash hash when Hash eh = self.new eh.replace deep_convert(hash) else raise ArgumentError, "must be a Hash or ExtendHash" end end private # convert string key to symbol key. # I'm rescurive def deep_convert(hash) ret = {} hash.each { |k,v| ret[k.to_sym] = Hash===v ? deep_convert(v) : v } ret end end def []=(key, value) key = key.to_sym if String === key super(key, value) end def [](key) key = key.to_sym if String === key super(key) end def method_missing(name, *args) if name =~ /=$/ store(name[0...-1].to_sym, args[0]) elsif has_key?(name) fetch(name) else raise NoMethodError, "-- :#{name}, #{args.inspect}" end end end
Version data entries
8 entries across 8 versions & 1 rubygems