Hash#count

I

Intransition

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.

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

Proposed:

{:a=>1,:b=2,:c=>1}.count(1) #=> 2
{:x=>1,:b=2,:c=>1}.count(1) #=> 2

It would be more useful that way. And, in the same vain, a #has_pair?
seems like an obvious addition.
 
P

Paul Smith

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.

Current:

=A0{:a=3D>1,:b=3D2,:c=3D>1}.count([:a,1]) #=3D> 1
=A0{:x=3D>1,:b=3D2,:c=3D>1}.count([:a,1]) #=3D> 0
=A0{:a=3D>1,:b=3D2,:c=3D>1}.count(1) =A0 =A0 =A0#=3D> 0


{:a=3D>1,:b=3D2,:c=3D>1}.count{|x| x[1]=3D=3D1} #=3D> 2


--=20
Paul Smith
http://www.nomadicfun.co.uk

(e-mail address removed)
 
R

Robert Klemme

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:

=A0{:a=3D>1,:b=3D2,:c=3D>1}.count([:a,1]) #=3D> 1
=A0{:x=3D>1,:b=3D2,:c=3D>1}.count([:a,1]) #=3D> 0
=A0{:a=3D>1,:b=3D2,:c=3D>1}.count(1) =A0 =A0 =A0#=3D> 0


{:a=3D>1,:b=3D2,:c=3D>1}.count{|x| x[1]=3D=3D1} =A0 =A0 =A0#=3D> 2

Btw, there is a typo before 2. This does not even compile on my Ruby versi=
ons:

09:18:06 tmp$ allruby -ce '{:a=3D>1,:b=3D2,:c=3D>1}.count{|x| x[1]=3D=3D1}'
CYGWIN_NT-5.1 padrklemme1 1.5.25(0.156/4/2) 2008-06-12 19:34 i686 Cygwin
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
ruby 1.8.7 (2008-08-11 patchlevel 72) [i386-cygwin]
-e:1: syntax error, unexpected tINTEGER, expecting tASSOC
{:a=3D>1,:b=3D2,:c=3D>1}.count{|x| x[1]=3D=3D1}
^
-e:1: warning: useless use of a literal in void context
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
ruby 1.9.1p129 (2009-05-12 revision 23412) [i386-cygwin]
-e:1: syntax error, unexpected tINTEGER, expecting tASSOC
{:a=3D>1,:b=3D2,:c=3D>1}.count{|x| x[1]=3D=3D1}
^
09:18:17 tmp$

You can also do

irb(main):019:0> {:a=3D>1,:b=3D>2,:c=3D>1}.count {|k,v| v =3D=3D 1}
=3D> 2

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
I

Intransition

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.

=A0{:a=3D>1,:b=3D2,:c=3D>1}.count([:a,1]) #=3D> 1
=A0{:x=3D>1,:b=3D2,:c=3D>1}.count([:a,1]) #=3D> 0
=A0{:a=3D>1,:b=3D2,:c=3D>1}.count(1) =A0 =A0 =A0#=3D> 0

{:a=3D>1,:b=3D2,:c=3D>1}.count{|x| x[1]=3D=3D1} =A0 =A0 =A0#=3D> 2

Sure. And there other ways to do it too. My point is not that there is
a difficulty in achieving this behavior that must be addressed, but
that Hash#count (without using a block) is much less useful as
currently defined. My suggestion is simply to maximize it's utility
for the majority of usecases. This is not unlike what other Enumerable
Hash methods do. Consider Hash#include?. It has a Hash specific
definition whereby it checks only keys.

Let me put it another way. There is no point in counting Hash keys.
There is only ever one or none. So Hash#count should only count
values.

Now one might ask about counting all pairs with keys matching a
pattern, but such cases are going to be the exception rather then the
rule, and can be achieved almost as simply using other means, for
instance:

hash.to_a.count{ |(k,v)| ... }

or

hash.keys.count{ |k| hash[k] =3D~ /.../ }

Oh, and b/c of enumerators, I think we can even do:

hash.each.count{ |k,v| ... }

T.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top