Created
March 18, 2012 18:11
-
-
Save pedroteixeira/2078983 to your computer and use it in GitHub Desktop.
hash syntax
This file contains hidden or 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 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
allows one to write:
a = {}
a.x = 1