Skip to content

Instantly share code, notes, and snippets.

@taiansu
Created July 2, 2013 08:00
Show Gist options
  • Save taiansu/5907506 to your computer and use it in GitHub Desktop.
Save taiansu/5907506 to your computer and use it in GitHub Desktop.
Dynamic hash
class DynamicHash < Hash
def []=(key, val)
super
self.class.class_eval do
define_method(key) do
self[key]
end
define_method("#{key}=") do |value|
self[key] = value
end
self
end
end
end
dh = DynamicHash.new
dh["foo"] = "bar"
p dh.foo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment