From: Robert Klemme Date: 2009-03-02T00:53:58+09:00 Subject: Re: Newbie question > Hash workings On 01.03.2009 12:35, AD60 wrote: > I see the use with an integer default, I specifically meant the > Hash.new([]) syntax as problematic, but it probably all depends on > knowing what your doing. Ah. Since you talked about "idiom" I thought you were referring to the general pattern. > Just to detail the difference: > > > hav = Hash.new { |h,k| h[k] = []} > hav[0] += [1] Actually this is very inefficient. With this idiom you should rather do hav[0] << [1] because otherwise you'll end up creating and deleting Array instances all the time without any benefit. > p hav # {0=>[1]} > p hav[1] # [] > p hav # {0=>[1], 1=>[]} > > hiv = Hash.new 0 > hiv[0] += 1 > p hiv # {0=>1} > p hiv[1] # 0 > p hiv # {0=>1} > > it's the difference between "{0=>[1], 1=>[]}" and "{0=>1}" result that > had me wondering. Anyway thanks again, I understand how to use it and > that's more than enough for now. Good. Kind regards robert