# Quick way for total count of a Hash object or a Tally class

**URL:** <https://www.ruby-forum.com/t/quick-way-for-total-count-of-a-hash-object-or-a-tally-class/115298>\
**Category:** Ruby\
**Created:** [September 23, 2007, 1:49pm UTC](https://www.ruby-forum.com/t/quick-way-for-total-count-of-a-hash-object-or-a-tally-class/115298 "2007-09-23T13:49:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [September 23, 2007, 1:49pm UTC](https://www.ruby-forum.com/t/quick-way-for-total-count-of-a-hash-object-or-a-tally-class/115298/1 "2007-09-23T13:49:26Z")

</div>

for the hash

[“apple” =\> 3, “banana” =\> 2]

is there an instant way to tally up all the counts? (instead of looping  
through all keys and add up all counts, because the hash can be very  
big, like thousands of items, and looping can be quite expensive for CPU  
time)

does the following look right?

class Tally \< Hash

attr\_reader :total

def initialize  
@total = 0  
super(0)  
end

def []=(key, new\_value)  
@total -= self[key] # previous value goes  
@total += new\_value # new value in

super  
end

def inspect  
super + " total = #{@total}"  
end

end

t = Tally.new

t[“apple”] += 1

puts; p t

t[“banana”] += 1

puts; p t

t[“banana”] = 1000

puts; p t

t[“apple”] += 10

puts; p t

t[“apple”] -= 5

puts; p t

t[“apple”] = 10000

puts; p t

t[123] = 10

puts; p t

[~/depot] 360 $ ruby test\_tally.rb

{“apple”=\>1} total = 1

{“apple”=\>1, “banana”=\>1} total = 2

{“apple”=\>1, “banana”=\>1000} total = 1001

{“apple”=\>11, “banana”=\>1000} total = 1011

{“apple”=\>6, “banana”=\>1000} total = 1006

{“apple”=\>10000, “banana”=\>1000} total = 11000

{“apple”=\>10000, 123=\>10, “banana”=\>1000} total = 11010

---

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [September 23, 2007, 8:17pm UTC](https://www.ruby-forum.com/t/quick-way-for-total-count-of-a-hash-object-or-a-tally-class/115298/2 "2007-09-23T20:17:54Z")

</div>

On 9/23/07, SpringFlowers AutumnMoon [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> @total -= self[key] # previous value goes  
> Nope RHS can be nil  
> @total += new\_value # new value in
> 
> super  
> I would put super first just in case it crashes anyway

```
  old_value = fetch(key,0)
  begin
      super
      @total += new_value - old_value
 rescue

```

HTH  
Robert

---

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [September 23, 2007, 9:42pm UTC](https://www.ruby-forum.com/t/quick-way-for-total-count-of-a-hash-object-or-a-tally-class/115298/3 "2007-09-23T21:42:36Z")

</div>

Robert D. wrote:

> On 9/23/07, SpringFlowers AutumnMoon [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > @total -= self[key] # previous value goes  
> > Nope RHS can be nil

coz i created the Hash to default value to 0, by super(0)

> > @total += new\_value # new value in
> > 
> > super  
> > I would put super first just in case it crashes anyway
> 
> ```
> old_value = fetch(key,0)
> begin
> super
> @total += new_value - old_value
> rescue
> 
> ```

why would it crash? no key error? i thought Ruby won’t have a no key  
error.
