How can I optimize this array.each in ruby? I'm trying to optimize this bit of code and I thought I'd throw it out here to see if anyone has any ideas. I've been testing the performance using Benchmark.measure {...} and wrapping the entire function.

Yes, the item has a date that is a Ruby Date

  puts Benchmark.measure {
    grouped_items
  }

  def grouped_items
    unless @grouped
      @grouped = {}
      @items.each do |item|
        key = item.date
        if @grouped.has_key?(key)
          @grouped[key] &lt;&lt; item
        else
          @grouped[key] = [item]
        end
      end
    end
    @grouped
  end


Thanks for any insight you care to share.

Edit #1: My first optimization. I was aiming for a slightly more succinct function and gained a .2 seconds reduction in time to process 100_000 items.

  def grouped_items
    unless @grouped
      @grouped = Hash.new { |h, k| h[k] = [] }
      @items.map {|item| @grouped[item.date] &lt;&lt; item }
    end
    @grouped
  end


Edit #2: My second iteration with more or less the same performance profile but with much less code.

def grouped_items
  @grouped ||= @items.group_by { |item| item.date }
end


Edit #3: An alternative way to do the same thing above.

def grouped_items
  @grouped ||= @items.group_by &amp;:date
end
Are you aware of Enumerable#group_by?

def grouped_items
  @items.group_by{|item| item.date}
end