Skip to content

Instantly share code, notes, and snippets.

@edymerchk
Created January 15, 2014 14:48
Show Gist options
  • Save edymerchk/8437570 to your computer and use it in GitHub Desktop.
Save edymerchk/8437570 to your computer and use it in GitHub Desktop.
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