Skip to content

Instantly share code, notes, and snippets.

@softr8
Created September 19, 2013 16:16
Show Gist options
  • Save softr8/6625828 to your computer and use it in GitHub Desktop.
Save softr8/6625828 to your computer and use it in GitHub Desktop.
hash ._magic accessor
class Hash
def method_missing( symbol, *args )
a, method = symbol.to_s.split('_')
if method
if self.has_key?(method.to_sym) || self.has_key?(method)
self[method.to_sym] || self[method]
else
if method.include?('=')
key = method.split('=').first.strip
if has_key?(key)
self[key] = args.first
else
self[key.to_sym] = args.first
end
else
nil
end
end
else
super symbol, *args
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment