Skip to content

Commit

Permalink
Add OrderedHash#invert to preserve order in ruby 1.8
Browse files Browse the repository at this point in the history
Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information
chaitanyav authored and josevalim committed Jun 26, 2010
1 parent 05defcd commit 449cf50
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
4 changes: 4 additions & 0 deletions activesupport/lib/active_support/ordered_hash.rb
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions activesupport/test/ordered_hash_test.rb
Expand Up @@ -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.