From: "Peña, Botp" Date: 2008-08-08T18:59:38+09:00 Subject: Re: Most compact command for associate array 'totalling'? From: Marc Heiler [mailto:shevegen@linuxmail.org] # I guess you have a point here, but visually I find the # shorter solution # a = Hash.new(0) # a['abc'] += 1 # more appealing. careful, it's more than a face; the previous block example creates/assigns objects when called. compare this, irb(main):001:0> h=Hash.new 0 => {} irb(main):002:0> h[1] => 0 irb(main):003:0> h[2] => 0 irb(main):004:0> h => {} w this, irb(main):005:0> h=Hash.new{|h,k| h[k]=0} => {} irb(main):006:0> h[1] => 0 irb(main):007:0> h[2] => 0 irb(main):008:0> h => {1=>0, 2=>0} and this, irb(main):011:0* h=Hash.new{|h,k| 0} => {} irb(main):012:0> h[1] => 0 irb(main):013:0> h[2] => 0 irb(main):014:0> h => {} (note that Hash.new{|h,k| 0} may evolve to Hash.new{0} or Hash.new(0); ie all of them do not assign/create to the hash) the common denominator of the above is that they all do the ff (w same result) irb(main):021:0> h[1] += 1 => 1 irb(main):022:0> h => {1=>1} So if you ask me, i find the hash block form very generic. kind regards -botp (just another block fan :)