Created
January 15, 2014 14:48
-
-
Save edymerchk/8437570 to your computer and use it in GitHub Desktop.
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
class Hash | |
def method_missing(method, *opts) | |
m = method.to_s | |
if self.has_key?(m) | |
return self[m] | |
elsif self.has_key?(m.to_sym) | |
return self[m.to_sym] | |
end | |
super | |
end | |
end | |
waza = {hello: 'world'} | |
waza.hello # => "world" | |
# Note: this implementation has only one known bug: | |
x = { 'test' => 'aValue', :test => 'bar'} | |
x.test # => 'aValue' | |
#if you prefer symbol lookups rather than string lookups, then swap the two 'if' condition |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment