# Summing values in a hash?

Hi,

Is there an inbuilt method to sum all values in a hash and return the
result?

I.E something like puts hash[values].sum

If so could someone please point me at the correct section of the ruby
docs?

thanks,
Jen.

On Tue, Mar 22, 2011 at 12:57 PM, Jen [email protected] wrote:

Is there an inbuilt method to sum all values in a hash and return the
result?

I.E something like puts hash[values].sum

You could try

hash.values.inject { |a, b| a + b }

Alternatively, you have the shorter form, inject(&:+) or inject(:+)
depending on your version of Ruby. 1.8.7 and higher should support all
three.

There’s no built in method to do that. You can do it with:

hash = { :a => 1, :b => 6, :c => 20 }

p hash.inject(0) { |sum, tuple| sum += tuple[1] } # => 27

On Tue, Mar 22, 2011 at 2:01 PM, Adam P. [email protected]
wrote:

hash.values.inject { |a, b| a + b }

Alternatively, you have the shorter form, inject(&:+) or inject(:+)
depending on your version of Ruby. 1.8.7 and higher should support all
three.

You should rather use a different idiom because your solution breaks
for empty Hash:

irb(main):001:0> {}.values.inject {|a,b|a+b}
=> nil
irb(main):002:0> {}.values.inject(0) {|a,b|a+b}
=> 0

Kind regards

robert

On Tue, Mar 22, 2011 at 2:05 PM, Robert K.
[email protected] wrote:

=> nil
irb(main):002:0> {}.values.inject(0) {|a,b|a+b}
=> 0
slightly better, because working
.inject(0){|a,(_,b)|a+b}
Cheers
R.

On Tue, Mar 22, 2011 at 3:13 PM, Robert D. [email protected]
wrote:

irb(main):001:0> {}.values.inject {|a,b|a+b}
=> nil
irb(main):002:0> {}.values.inject(0) {|a,b|a+b}
=> 0
slightly better, because working
.inject(0){|a,(_,b)|a+b}

What do you mean by that? Where was my solution not working?

robert

On Tue, Mar 22, 2011 at 4:08 PM, Robert K.
[email protected] wrote:
jruby -e ‘p( {:a=>42}.inject(0){|a,b|a+b})’
TypeError: Array can’t be coerced into Fixnum
+ at org/jruby/RubyFixnum.java:333
file at -e:1
each at org/jruby/RubyHash.java:1167
inject at org/jruby/RubyEnumerable.java:816
(root) at -e:1

I recon you where coming from Hash#values, however on applying the
code to an empy hash, where it works of course the reader could be
missled

Cheers
Robert

On Tue, Mar 22, 2011 at 12:57 PM, Jen [email protected] wrote:

Is there an inbuilt method to sum all values in a hash and return the
result?
I.E something like puts hash[values].sum
If so could someone please point me at the correct section of the ruby docs?

Maybe write your own method because:

1. it may be quicker than searching for an “inbuilt” method;
2. it may run faster than using inject.

require “benchmark”
kt = 100_000
nn = 20

hash = Hash.new
(1 … nn).each { |nv| hash[ nv.to_s ] = nv }
s1 = s2 = s3 = s4 = nil

class Hash
def mysum()
s = 0
each_value { |v| s += v }
s
end
end

sv = (nn * (nn + 1)) / 2
puts; puts “summing 1…#{nn} #=> #{sv}; kt=#{kt};”

Benchmark.bmbm do |bm|
bm.report(“1”){kt.times{s1=hash.values.inject{ |a, b| a + b } }}
bm.report(“2”){kt.times{s2=hash.inject(0){ |sum, tuple| sum +=
tuple[1] } }}
bm.report(“3”){kt.times{s3=hash.inject(0){|a,(_,b)|a+b} }}
bm.report(“4”){kt.times{s4=hash.mysum }}
end
puts “#{s1.inspect} #{s2.inspect} #{s3.inspect} #{s4.inspect}”

ruby 1.9.1p430 (2010-08-16 revision 28998) [i386-mingw32]

summing 1…20 #=> 210; kt=100000;
user system total real
1 3.057000 0.000000 3.057000 ( 3.057547)
2 4.587000 0.000000 4.587000 ( 4.630071)
3 4.524000 0.000000 4.524000 ( 4.525069)
4 1.653000 0.000000 1.653000 ( 1.650026)
210 210 210 210

summing 1…2000 #=> 2001000; kt=1000;
user system total real
1 2.340000 0.000000 2.340000 ( 2.335036)
2 4.711000 0.000000 4.711000 ( 4.705072)
3 4.617000 0.000000 4.617000 ( 4.635071)
4 1.529000 0.000000 1.529000 ( 1.530024)
2001000 2001000 2001000 2001000

On Tue, Mar 22, 2011 at 4:31 PM, Robert D. [email protected]
wrote:

I recon you where coming from Hash#values, however on applying the
code to an empy hash, where it works of course the reader could be
missled

My code explicitly included #values so it would not be affected by the
error you posted - even for non empty Hash. Please read what I wrote.

Cheers

robert