2009/11/5 Jes=FAs Gabriel y Gal=E1n <jgabrielygalan / gmail.com>:
> On Thu, Nov 5, 2009 at 8:25 AM, Robert Klemme
> <shortcutter / googlemail.com> wrote:
>> On 11/04/2009 06:10 PM, Aldric Giacomoni wrote:
>>>
>>> Robert Klemme wrote:
>>>>
>>>> IMHO this solution is even simpler because it does not need the method=
:
>>>>
>>>> h =3D Hash.new {|h,k| h[k] =3D 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 a=
nd
>>> reuse it when creating a new hash ?
>>
>> The block *is* saved and reused when creating nested Hashes!
>>
>>> Something like :
>>> a =3D Proc.new {|h,k| h[k] =3D Hash.new(&h.default_proc)}
>>> b =3D Hash.new a
>>>
>>> Note, I know this is wrong.. It'll just put the proc as the default val=
ue,
>>> instead of triggering the proc.
>>
>> No need for that. =A0Please look at my code again.
>
> I think he might want to save typing:
>
> a =3D Proc.new {|h,k| h[k] =3D Hash.new(&h.default_proc)}
> b =3D Hash.new &a
> c =3D Hash.new &a
> d =3D Hash.new &a
>
> Maybe this is his use case?

If I wanted to save typing I'd place the whole construction in a method

def cnh # silly name "create nested hash"
  Hash.new {|h,k| h[k] =3D Hash.new(&h.default_proc)}
end

a =3D cnh
b =3D cnh
c =3D cnh
d =3D cnh

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/