On Thu, Jun 21, 2012 at 8:49 AM, Avdi Grimm <groups / inbox.avdi.org> wrote:
>
> On Jun 20, 2012 5:57 PM, "Ryan Davis" <ryand-ruby / zenspider.com> wrote:
>>
>>
>>
>>  counter = Hash.new 0
>>  thingies.each do |o|
>>   counter[o.key] += 1
>>  end
>>  return counter
>>
>
> I'm curious if you consider #each_with_object a reasonable choice for this.

indeed. i have 3 solns for this using 1) tap, 2) inject, 3)
each_with_object (or .each.with_object).

(Hash.new(0)).tap{|h| thingies.each{|i| h[i] += 1} }

thingies.inject(Hash.new(0)){|h,i| h[i] += 1; h} }

thingies.each.with_object(Hash.new(0)){|i,h| h[i] += 1}

looking at inject.. hmm, not sure. it does not seem so bad.. unless i
be so dogmatic.. nah, i've multiple religion.. it's more fun :)

best regards -botp