On Wed, Nov 4, 2009 at 6:10 PM, Aldric Giacomoni <aldric / trevoke.net> wrote:
> Robert Klemme wrote:
>>
>> IMHO this solution is even simpler because it does not need the method:
>>
>> h = Hash.new {|h,k| h[k] = Hash.new(&h.default_proc)}
>
> Robert - I agree. I like this one better. Feels a little more ruby-ish.
> Is there a way to use Proc (or whatever does work) to save this block
> and reuse it when creating a new hash ?
> Something like :
> a = Proc.new {|h,k| h[k] = Hash.new(&h.default_proc)}
> b = Hash.new a
>
> Note, I know this is wrong.. It'll just put the proc as the default
> value, instead of triggering the proc.



irb(main):001:0> a = Proc.new {|h,k| h[k] = Hash.new(&h.default_proc)}
=> #<Proc:0xb7d81730@(irb):1>
irb(main):002:0> b = Hash.new &a
=> {}
irb(main):003:0> b[1][2] = 3
=> 3
irb(main):004:0> b
=> {1=>{2=>3}}

Jesus.