2012-06-20 @ 00:00

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Object
  def self.const_missing x
    if x.to_s =~ /^HashOf(.+)/ then
      subtype = $1
      eval "#{x} = Class.new Hash do
              def initialize
                super { |h,k| h[k] = #{subtype}.new }
              end
            end"
    else
      super
    end
  end
end

h = HashOfHashOfArray.new
# => creates HashOfHashOfArray
# =>       creates HashOfArray

h[:a][:b] << 42

p h # => {:a=>{:b=>[42]}}
« go back