From: Sam Duncan Date: 2012-06-21T10:07:14+09:00 Subject: Re: Benchmark obsession? This is a multi-part message in MIME format. --------------060702020303000309010907 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit On 21/06/12 12:49, Avdi Grimm wrote: > > > On Jun 20, 2012 5:57 PM, "Ryan Davis" > 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. > > -- > Avdi > Is that basically the same thing wrapped in another method so that counter and o are yielded to a block? def each_with_object(memo) return to_enum :each_with_object, memo unless block_given? each do |element| yield element, memo end memo end Sam --------------060702020303000309010907 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit On 21/06/12 12:49, Avdi Grimm 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.

--
Avdi

Is that basically the same thing wrapped in another method so that counter and o are yielded to a block?

def each_with_object(memo)
  return to_enum :each_with_object, memo unless block_given?
  each do |element|
    yield element, memo
  end
  memo
end

Sam
--------------060702020303000309010907--