hi Christoph,

On Sun, 2002-07-14 at 06:01, Christoph wrote:

> I guess I would write #count (not sure if this is such a descriptive
> name) as
> 
> class Array
>     def count() uniq.size end
> end

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

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)

> module Enumerable
>     def uniq
>         hsh = {}
>         each {|e| hsh[e] = true }
>         hsh.keys
>     end
> end
> 

again for a string this returns slightly odd results due to strings
unique definition of #each. but i do see your reasoning and think there
is no reason #uniq can not also be fashioned into Enumberable.

-- 
~transami

  __("<  berk! berk!
  \__/  
   ^^