Skip to content

Instantly share code, notes, and snippets.

@geeksam
Created September 16, 2011 18:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geeksam/1222811 to your computer and use it in GitHub Desktop.
Save geeksam/1222811 to your computer and use it in GitHub Desktop.
HashProxy
class HashProxy
instance_methods.each { |meth| undef_method(meth) unless meth =~ /^__/ }
def self.new(delegate)
return delegate unless delegate.kind_of?(Hash)
super
end
def initialize(hash)
@hash = hash
end
def method_missing(meth, *args, &block)
case meth.to_s
when /^\[\]/ then @hash.send(meth, *args)
when /^(.*)=$/ then @hash[$1] = *args
else @hash[meth]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment