diff --git a/activesupport/lib/active_support/ordered_hash.rb b/activesupport/lib/active_support/ordered_hash.rb index a19d6c3532..6b563b9063 100644 --- a/activesupport/lib/active_support/ordered_hash.rb +++ b/activesupport/lib/active_support/ordered_hash.rb @@ -162,6 +162,10 @@ def replace(other) self end + def invert + OrderedHash[self.to_a.map!{|key_value_pair| key_value_pair.reverse}] + end + def inspect "#" end diff --git a/activesupport/test/ordered_hash_test.rb b/activesupport/test/ordered_hash_test.rb index f3d2ec0286..fc467b41de 100644 --- a/activesupport/test/ordered_hash_test.rb +++ b/activesupport/test/ordered_hash_test.rb @@ -257,4 +257,10 @@ def test_update_sets_keys @updated_ordered_hash.update(:name => "Bob") assert_equal [:name], @updated_ordered_hash.keys end + + def test_invert + @ordered_hash = ActiveSupport::OrderedHash[[["foo", "FOO"], ["bar", "BAR"]]] + @inverted_ordered_hash = @ordered_hash.invert + assert_equal [["FOO", "foo"], ["BAR", "bar"]], @inverted_ordered_hash.to_a + end end