mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Rubinious: work around h[k] ||= v returning []= result instead of v
This commit is contained in:
parent
19895f087c
commit
f5cbad21ac
2 changed files with 15 additions and 3 deletions
|
@ -1,3 +1,5 @@
|
|||
require 'active_support/ordered_hash'
|
||||
|
||||
module Enumerable
|
||||
# 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'
|
||||
|
@ -18,10 +20,19 @@ module Enumerable
|
|||
# "2006-02-24 -> Transcript, Transcript"
|
||||
# "2006-02-23 -> Transcript"
|
||||
def group_by
|
||||
inject ActiveSupport::OrderedHash.new do |grouped, element|
|
||||
(grouped[yield(element)] ||= []) << element
|
||||
grouped
|
||||
assoc = ActiveSupport::OrderedHash.new
|
||||
|
||||
each do |element|
|
||||
key = yield(element)
|
||||
|
||||
if assoc.has_key?(key)
|
||||
assoc[key] << element
|
||||
else
|
||||
assoc[key] = [element]
|
||||
end
|
||||
end
|
||||
|
||||
assoc
|
||||
end unless [].respond_to?(:group_by)
|
||||
|
||||
# Calculates a sum from the elements. Examples:
|
||||
|
|
|
@ -12,6 +12,7 @@ module ActiveSupport
|
|||
else
|
||||
self << [key, value]
|
||||
end
|
||||
value
|
||||
end
|
||||
|
||||
def [](key)
|
||||
|
|
Loading…
Reference in a new issue