Permalink
Browse files

Fixes minor ruby 1.8 inconsistency

ActiveSupport::OrderedHash did not behave identically to Hash when given
a block with a splat.
  • Loading branch information...
1 parent d1c7470 commit c9cc36bde9f779c917171e7c378285b57f748414 @AndrewRadev AndrewRadev committed May 27, 2011
Showing with 28 additions and 1 deletion.
  1. +5 −1 activesupport/lib/active_support/ordered_hash.rb
  2. +23 −0 activesupport/test/ordered_hash_test.rb
@@ -158,7 +158,11 @@ def each
self
end
- alias_method :each_pair, :each
+ def each_pair
+ return to_enum(:each_pair) unless block_given?
+ @keys.each {|key| yield key, self[key]}
+ self
+ end
alias_method :select, :find_all
@@ -114,6 +114,9 @@ def test_each_pair
end
assert_equal @values, values
assert_equal @keys, keys
+
+ expected_class = RUBY_VERSION < '1.9' ? Enumerable::Enumerator : Enumerator
+ assert_kind_of expected_class, @ordered_hash.each_pair
end
def test_find_all
@@ -257,6 +260,26 @@ def test_each_after_yaml_serialization
assert_equal @values, values
end
+ def test_each_when_yielding_to_block_with_splat
+ hash_values = []
+ ordered_hash_values = []
+
+ @hash.each { |*v| hash_values << v }
+ @ordered_hash.each { |*v| ordered_hash_values << v }
+
+ assert_equal hash_values.sort, ordered_hash_values.sort
+ end
+
+ def test_each_pair_when_yielding_to_block_with_splat
+ hash_values = []
+ ordered_hash_values = []
+
+ @hash.each_pair { |*v| hash_values << v }
+ @ordered_hash.each_pair { |*v| ordered_hash_values << v }
+
+ assert_equal hash_values.sort, ordered_hash_values.sort
+ end
+
def test_order_after_yaml_serialization
@deserialized_ordered_hash = YAML.load(YAML.dump(@ordered_hash))

0 comments on commit c9cc36b

Please sign in to comment.