2012-06-21 @ 00:00

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class HashHash < Hash
  def initialize
    super { |h,k| h[k] = HashHash.new }
  end

  def []= k, v
    if k =~ /\./ then
      keys = k.split(/\./)
      keys[0..-2].inject(self) { |h,k| h[k] }[keys.last] = v
    else
      super
    end
  end
end

h = HashHash.new
h["a.b.c"] = 10
p h               # => {"a"=>{"b"=>{"c"=>10}}}
« go back