2007-02-09 09:04:31 +01:00
|
|
|
module Enumerable
|
2008-05-18 06:22:34 +02:00
|
|
|
# Ruby 1.8.7 introduces group_by, but the result isn't ordered. Override it.
|
|
|
|
remove_method(:group_by) if [].respond_to?(:group_by) && RUBY_VERSION < '1.9'
|
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
# Collect an enumerable into sets, grouped by the result of a block. Useful,
|
|
|
|
# for example, for grouping records by date.
|
|
|
|
#
|
2008-05-18 06:22:34 +02:00
|
|
|
# Example:
|
2007-01-22 14:43:50 +01:00
|
|
|
#
|
|
|
|
# latest_transcripts.group_by(&:day).each do |day, transcripts|
|
2008-05-18 06:22:34 +02:00
|
|
|
# p "#{day} -> #{transcripts.map(&:class).join(', ')}"
|
2007-01-22 14:43:50 +01:00
|
|
|
# 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
|
2008-05-18 06:22:34 +02:00
|
|
|
inject ActiveSupport::OrderedHash.new do |grouped, element|
|
|
|
|
(grouped[yield(element)] ||= []) << element
|
|
|
|
grouped
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
2008-05-18 06:22:34 +02:00
|
|
|
end unless [].respond_to?(:group_by)
|
2007-02-09 09:04:31 +01:00
|
|
|
|
|
|
|
# Calculates a sum from the elements. Examples:
|
|
|
|
#
|
|
|
|
# payments.sum { |p| p.price * p.tax_rate }
|
|
|
|
# payments.sum(&:price)
|
|
|
|
#
|
2008-05-18 06:22:34 +02:00
|
|
|
# The latter is a shortcut for:
|
|
|
|
#
|
|
|
|
# payments.inject { |sum, p| sum + p.price }
|
|
|
|
#
|
|
|
|
# It can also calculate the sum without the use of a block.
|
2007-02-09 09:04:31 +01:00
|
|
|
#
|
2008-05-18 06:22:34 +02:00
|
|
|
# [5, 15, 10].sum # => 30
|
|
|
|
# ["foo", "bar"].sum # => "foobar"
|
|
|
|
# [[1, 2], [3, 1, 5]].sum => [1, 2, 3, 1, 5]
|
2007-02-09 09:04:31 +01:00
|
|
|
#
|
2008-05-18 06:22:34 +02:00
|
|
|
# The default sum of an empty list is zero. You can override this default:
|
2007-02-09 09:04:31 +01:00
|
|
|
#
|
2008-05-18 06:22:34 +02:00
|
|
|
# [].sum(Payment.new(0)) { |i| i.amount } # => Payment.new(0)
|
2007-02-09 09:04:31 +01:00
|
|
|
#
|
|
|
|
def sum(identity = 0, &block)
|
|
|
|
return identity unless size > 0
|
2007-12-21 08:48:59 +01:00
|
|
|
|
2007-02-09 09:04:31 +01:00
|
|
|
if block_given?
|
|
|
|
map(&block).sum
|
|
|
|
else
|
|
|
|
inject { |sum, element| sum + element }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Convert an enumerable to a hash. Examples:
|
|
|
|
#
|
|
|
|
# people.index_by(&:login)
|
|
|
|
# => { "nextangle" => <Person ...>, "chade-" => <Person ...>, ...}
|
|
|
|
# people.index_by { |person| "#{person.first_name} #{person.last_name}" }
|
|
|
|
# => { "Chade- Fowlersburg-e" => <Person ...>, "David Heinemeier Hansson" => <Person ...>, ...}
|
|
|
|
#
|
|
|
|
def index_by
|
|
|
|
inject({}) do |accum, elem|
|
|
|
|
accum[yield(elem)] = elem
|
|
|
|
accum
|
|
|
|
end
|
|
|
|
end
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|