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.
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.
|
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.
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).
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.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.