diff options
-rw-r--r-- | activesupport/lib/active_support/ordered_options.rb | 6 | ||||
-rw-r--r-- | activesupport/test/ordered_options_test.rb | 13 |
2 files changed, 19 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/ordered_options.rb b/activesupport/lib/active_support/ordered_options.rb index b2b1b0e438..3172f62f0d 100644 --- a/activesupport/lib/active_support/ordered_options.rb +++ b/activesupport/lib/active_support/ordered_options.rb @@ -18,6 +18,12 @@ module ActiveSupport pair = assoc(key) pair ? pair.last : nil end + + def delete(key) + pair = assoc(key) + pair ? array_index = index(pair) : nil + array_index ? delete_at(array_index).last : nil + end def keys collect { |key, value| key } diff --git a/activesupport/test/ordered_options_test.rb b/activesupport/test/ordered_options_test.rb index 1a1d0c7648..3d537a0ae4 100644 --- a/activesupport/test/ordered_options_test.rb +++ b/activesupport/test/ordered_options_test.rb @@ -29,6 +29,19 @@ class OrderedHashTest < Test::Unit::TestCase assert_equal value, @ordered_hash.values.last assert_equal value, @ordered_hash[key] end + + def test_delete + key, value = 'white', 'ffffff' + bad_key = 'black' + + @ordered_hash[key] = value + assert_equal @keys.length + 1, @ordered_hash.length + + assert_equal value, @ordered_hash.delete(key) + assert_equal @keys.length, @ordered_hash.length + + assert_nil @ordered_hash.delete(bad_key) + end end class OrderedOptionsTest < Test::Unit::TestCase |