# Hash#count

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.

On Wed, Nov 4, 2009 at 4:19 PM, Intransition [email protected]
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.

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

Paul S.

[email protected]

2009/11/4 Paul S. [email protected]:

On Wed, Nov 4, 2009 at 4:19 PM, Intransition [email protected] 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:

# 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

Hi,

In message “Re: Hash#count”
on Thu, 5 Nov 2009 01:19:13 +0900, Intransition
[email protected] writes:

|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.

That is based on viewpoint to see hashes are collections of values
indexed by arbitrary object. But in reality, Ruby hashes are designed
as collections of key-value pair. I am not particularly against the
object-indexed view, but I am not going to change the existing method
behavior.

|And, in the same vain, a #has_pair?
|seems like an obvious addition.

Let’s address the issue one by one.

``          matz.``

On Nov 9, 5:13 am, Yukihiro M. [email protected] wrote:

|
object-indexed view, but I am not going to change the existing method
behavior.

Okay. I only point out, if Hash is a collection key-value pairs, I
would expect Hash#include? to check pairs too.

Personally I like to see methods behave according to most common uses,
not an abstract viewpoint. I realize that is not always easy to
determine (YMMV and all that), but I think it’s a worthy pursuit
nonetheless.

|And, in the same vain, a #has_pair?
|seems like an obvious addition.

Let’s address the issue one by one.

Sure.

Hi,

In message “Re: Hash#count”
on Wed, 11 Nov 2009 00:49:04 +0900, Intransition
[email protected] writes:

|Okay. I only point out, if Hash is a collection key-value pairs, I
|would expect Hash#include? to check pairs too.
|
|Personally I like to see methods behave according to most common uses,
|not an abstract viewpoint.

Hash#include? checks keys only (not pairs) according to most common
uses, and purpose of hash tables (key -> value look-up).

``          matz.``

On Nov 4, 11:32 am, Paul S. [email protected] wrote:

{:a=>1,:b=2,:c=>1}.count{|x| x[1]==1} #=> 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] =~ /…/ }

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

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

T.