Created
September 16, 2011 18:53
-
-
Save geeksam/1222811 to your computer and use it in GitHub Desktop.
HashProxy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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