"Tom Sawyer" wrote .... > the String definition of count is different from what you have here. in > the String class it returns the number of occurances of a given > sub-string, but is also a little more sophistacted, offering the option > for a string range, string negation, and multiple strings to be matched. > this would makes count difficult to implement in Enumerable, and also > relates to the strange definition of String#each. but a simple > definition would be: > > module Enumberable > def count(c) > self.select { |i| i == c }.length > end > end Aha, I see what you mean > > this works fine for array, and i imagine most other Enumberable mixin > classes. for string it is a little odd, counting matches against > segments of the string split by the record seperator $/. making it > useless to count characters, which is often what one would want. (hence > another reason to modifiy each as per our recent thread) anyway, while i > believe this inclusion of count in Enumberable is sensible and > practical, either the String'ss version of count would have to be > overridden to reamin compatible with the current count, or another name > would need to be used in Enumberable case, like #number_of, although i > point out that the above count method can be "spruced-up" to handle all > but String#count's alphanumeric ranges. (i don't see how ranges could > apply to arrays, for instance) The problem is that some Enumerable classes like the Hash class or a Set class (see for example rough/lib/set.rb - OT I am not sure why this implementation doesn't include Comparable) don't know the about the idea of multiple occurrences consequently you would optimize #count to def count(c) each {|e| return 1 if e == c } return 0 end which is sort of an indication (to me) that #count doesn't really into Enumerable ... /Christoph