On Mon, Jan 21, 2013 at 11:21 AM, Rob Biedenharn
<r...@agileconsultingllc.com> wrote:
> I tend to use a method like:
>
> module Enumerable
>   # Returns a Hash keyed by the value of the block to the number times that
>   # value was returned.  If you have experience with the #group_by from
>   # ActiveSupport, this would be like .group_by(&block).map{|k,a|[k,a.size]}
>   # (except it is a Hash rather than an Array).
>   def count_by
>     counts = Hash.new(0)
>     each {|e| counts[block_given? ? yield(e) : e] += 1}
>     counts
>   end
> end

Please do not teach people to monkey patch a class directly, if you
plan to play dirty and reopen a class be nice enough to make your own
module and then send(:include, MyModuleExt) so that people know where
it is coming from easily, rather than leaving people in the dark as to
what the hell is going on when they start looking for the method in
the stdlib docs.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to