Skip to content

Instantly share code, notes, and snippets.

@pedroteixeira
Created March 18, 2012 18:11
Show Gist options
  • Save pedroteixeira/2078983 to your computer and use it in GitHub Desktop.
Save pedroteixeira/2078983 to your computer and use it in GitHub Desktop.
hash syntax
class Hash
def id
self["id"] || self[:id]
end
def type
self["type"] || self[:type] || self.type
end
def method_missing(meth, *args, &block)
if args.size == 0
self[meth.to_s] || self[meth.to_sym]
else
if meth.to_s.end_with?("=")
value = meth.to_s.chomp("=")
self[value] = args[0]
end
end
end
end
@pedroteixeira
Copy link
Author

allows one to write:

a = {}
a.x = 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment