aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2008-06-10 03:42:43 -0700
committerJeremy Kemper <jeremy@bitsweat.net>2008-06-10 14:01:16 -0700
commitf5cbad21ac09822a61d6326cbadea16bbe86b623 (patch)
tree5e11137a337cb6c7331f49fffc603ac444038892
parent19895f087c338d8385dff9d272d30fb87cb10330 (diff)
downloadrails-f5cbad21ac09822a61d6326cbadea16bbe86b623.tar.gz
rails-f5cbad21ac09822a61d6326cbadea16bbe86b623.tar.bz2
rails-f5cbad21ac09822a61d6326cbadea16bbe86b623.zip
Rubinious: work around h[k] ||= v returning []= result instead of v
-rw-r--r--activesupport/lib/active_support/core_ext/enumerable.rb17
-rw-r--r--activesupport/lib/active_support/ordered_hash.rb1
2 files changed, 15 insertions, 3 deletions
diff --git a/activesupport/lib/active_support/core_ext/enumerable.rb b/activesupport/lib/active_support/core_ext/enumerable.rb
index f1469aa0e3..e7f537d045 100644
--- a/activesupport/lib/active_support/core_ext/enumerable.rb
+++ b/activesupport/lib/active_support/core_ext/enumerable.rb
@@ -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:
diff --git a/activesupport/lib/active_support/ordered_hash.rb b/activesupport/lib/active_support/ordered_hash.rb
index 59ceaec696..9757054e43 100644
--- a/activesupport/lib/active_support/ordered_hash.rb
+++ b/activesupport/lib/active_support/ordered_hash.rb
@@ -12,6 +12,7 @@ module ActiveSupport
else
self << [key, value]
end
+ value
end
def [](key)