module Enumerable #:nodoc: def first_match match = nil each do |items| break if match = yield(items) end match end # Collect an enumerable into sets, grouped by the result of a block. Useful, # for example, for grouping records by date. # # e.g. # # latest_transcripts.group_by(&:day).each do |day, transcripts| # p "#{day} -> #{transcripts.map(&:class) * ', '}" # end # "2006-03-01 -> Transcript" # "2006-02-28 -> Transcript" # "2006-02-27 -> Transcript, Transcript" # "2006-02-26 -> Transcript, Transcript" # "2006-02-25 -> Transcript" # "2006-02-24 -> Transcript, Transcript" # "2006-02-23 -> Transcript" def group_by inject({}) do |groups, element| (groups[yield(element)] ||= []) << element groups end end end