Created
January 24, 2010 08:23
-
-
Save rubyworks/285097 to your computer and use it in GitHub Desktop.
OpenHash is a hash with a #method_missing that routes to [] and []=.
This file contains 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
# OpenHash is a hash with a #method_missing method | |
# that routes to [] and []=. | |
class OpenHash < Hash | |
def method_missing(s, *a) | |
case s | |
when /\?$/ | |
self.key?(s.chomp('?')) | |
when /\=$/ | |
self[s] = a[0] | |
else | |
a.empty? ? self[s] : super | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment