From: Daniel Waite Date: 2008-05-08T04:51:22+09:00 Subject: Re: hash adding values Tim Wolak wrote: > Craig Demyanovich wrote: >> I posted some code just before you replied. Did it help? > > I did not store anything when I run it with my code I posted I get: > > > {} > {} > {} Hash.new { |hash, key| hash[key] = Array.new } Is what you will need if you intend to provide arrays as default values to hashes. So... (I've only skimmed your code, but I think I get the gist of your idea, so pardon if this isn't what you were asking for) Basically, store the raw values, then you can sum and do any number of operations on them as you wish. account_balances = Hash.new do |hash, key| hash[key] = Array.new end array_of_accounts.each do |account| accounts[account_name] << account.this_charge accounts[account_name] << account.that_charge ... end I'm going to say ab in place of account_balances here.... ab[some_account].sum # if using Rails, else try inject ab[some_account.inject(BigDecimal.new) do |sum, amount| sum += amount end -- Posted via http://www.ruby-forum.com/.