fork download
  1. class Options < Hash
  2. def self.new(&a)
  3. a = lambda{|h,k| h[k] = new(&a) } unless a
  4. super(&a)
  5. end
  6. def method_missing(m, *a)
  7. if m[-1] == "="
  8. key = keys.find{|k| k.to_s == m[0...-1].to_s } || m[0...-1].to_sym
  9. self[key] = a.first
  10. else
  11. key = keys.find{|k| k.to_s == m.to_s }
  12. self[key]
  13. end
  14. end
  15. end
  16.  
  17. h = Options.new
  18. h[:a][:b][:c] = :foo
  19. h[:d] = :bar
  20.  
  21. p h
  22. p h.a
  23. p h.a.b.c
  24.  
  25. h.e.f = :baz
  26. p h.e
Success #stdin #stdout 0s 4760KB
stdin
Standard input is empty
stdout
{:a=>{:b=>{:c=>:foo}}, :d=>:bar}
{:b=>{:c=>:foo}}
:foo
{:f=>:baz}