Skip to content

Instantly share code, notes, and snippets.

@lian
Created November 6, 2013 23:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lian/7346363 to your computer and use it in GitHub Desktop.
Save lian/7346363 to your computer and use it in GitHub Desktop.
class Options < Hash
def self.new(&a)
a = lambda{|h,k| h[k] = new(&a) } unless a
super(&a)
end
def method_missing(m, *a)
if m[-1] == "="
key = keys.find{|k| k.to_s == m[0...-1].to_s } || m[0...-1].to_sym
self[key] = a.first
else
key = keys.find{|k| k.to_s == m.to_s }
self[key]
end
end
end
h = Options.new
h[:a][:b][:c] = :foo
h[:d] = :bar
p h
p h.a
p h.a.b.c
h.e.f = :baz
p h.e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment