Skip to content
Browse files

Add OrderedHash#invert to preserve order in ruby 1.8

Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information...
1 parent 05defcd commit 449cf50d858c9a1f7cc9fcae3ad02841da0440be @chaitanyav chaitanyav committed with josevalim Jun 26, 2010
Showing with 10 additions and 0 deletions.
  1. +4 −0 activesupport/lib/active_support/ordered_hash.rb
  2. +6 −0 activesupport/test/ordered_hash_test.rb
View
4 activesupport/lib/active_support/ordered_hash.rb
@@ -147,6 +147,10 @@ def replace(other)
self
end
+ def invert
+ OrderedHash[self.to_a.map!{|key_value_pair| key_value_pair.reverse}]
+ end
+
def inspect
"#<OrderedHash #{super}>"
end
View
6 activesupport/test/ordered_hash_test.rb
@@ -228,4 +228,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

0 comments on commit 449cf50

Please sign in to comment.
Something went wrong with that request. Please try again.