From: Robert Klemme Date: 2009-11-05T17:18:49+09:00 Subject: Re: Hash#count 2009/11/4 Paul Smith : > On Wed, Nov 4, 2009 at 4:19 PM, Intransition wrote: >> I want to suggest Hash#count be defined such that it counts hash >> values. A it stands (using Enumerable#count) it only can ever return 0 >> or 1. I opt against because that would make Hash's #count behave differently than other Enumerable's #count plus there is a solution already as show by Paul. >> Current: >> >>  {:a=>1,:b=2,:c=>1}.count([:a,1]) #=> 1 >>  {:x=>1,:b=2,:c=>1}.count([:a,1]) #=> 0 >>  {:a=>1,:b=2,:c=>1}.count(1)      #=> 0 >> > > > {:a=>1,:b=2,:c=>1}.count{|x| x[1]==1}      #=> 2 Btw, there is a typo before 2. This does not even compile on my Ruby versions: 09:18:06 tmp$ allruby -ce '{:a=>1,:b=2,:c=>1}.count{|x| x[1]==1}' CYGWIN_NT-5.1 padrklemme1 1.5.25(0.156/4/2) 2008-06-12 19:34 i686 Cygwin ======================================== ruby 1.8.7 (2008-08-11 patchlevel 72) [i386-cygwin] -e:1: syntax error, unexpected tINTEGER, expecting tASSOC {:a=>1,:b=2,:c=>1}.count{|x| x[1]==1} ^ -e:1: warning: useless use of a literal in void context ======================================== ruby 1.9.1p129 (2009-05-12 revision 23412) [i386-cygwin] -e:1: syntax error, unexpected tINTEGER, expecting tASSOC {:a=>1,:b=2,:c=>1}.count{|x| x[1]==1} ^ 09:18:17 tmp$ You can also do irb(main):019:0> {:a=>1,:b=>2,:c=>1}.count {|k,v| v == 1} => 2 Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/