Skip to content
This repository
Browse code

Hash#symbolize_keys behaves well with integer keys. Closes #9890.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7945 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit cfffedb4d86bcf3190da5e6433b694fc2e9f1bd9 1 parent a2172e7
Jeremy Kemper authored
2  activesupport/CHANGELOG
... ...
@@ -1,5 +1,7 @@
1 1
 *SVN*
2 2
 
  3
+* Hash#symbolize_keys behaves well with integer keys.  #9890 [PotatoSalad]
  4
+
3 5
 * Multibyte: String#slice supports regexp argument.  #9646 [yob]
4 6
 
5 7
 * object.duplicable? returns true if object.dup is safe. False for nil, true, false, symbols, and numbers; true otherwise.  #9333 [sur]
6  activesupport/lib/active_support/core_ext/hash/keys.rb
@@ -24,7 +24,7 @@ def stringify_keys!
24 24
         # Return a new hash with all keys converted to symbols.
25 25
         def symbolize_keys
26 26
           inject({}) do |options, (key, value)|
27  
-            options[key.to_sym] = value
  27
+            options[key.to_sym || key] = value
28 28
             options
29 29
           end
30 30
         end
@@ -32,8 +32,8 @@ def symbolize_keys
32 32
         # Destructively convert all keys to symbols.
33 33
         def symbolize_keys!
34 34
           keys.each do |key|
35  
-            unless key.is_a?(Symbol)
36  
-              self[key.to_sym] = self[key]
  35
+            unless key.is_a?(Symbol) || (new_key = key.to_sym).nil?
  36
+              self[new_key] = self[key]
37 37
               delete(key)
38 38
             end
39 39
           end
6  activesupport/test/core_ext/hash_ext_test.rb
@@ -5,6 +5,7 @@ def setup
5 5
     @strings = { 'a' => 1, 'b' => 2 }
6 6
     @symbols = { :a  => 1, :b  => 2 }
7 7
     @mixed   = { :a  => 1, 'b' => 2 }
  8
+    @fixnums = {  0  => 1,  1  => 2 }
8 9
   end
9 10
 
10 11
   def test_methods
@@ -33,6 +34,11 @@ def test_symbolize_keys!
33 34
     assert_raises(NoMethodError) { { [] => 1 }.symbolize_keys }
34 35
   end
35 36
 
  37
+  def test_symbolize_keys_preserves_fixnum_keys
  38
+    assert_equal @fixnums, @fixnums.symbolize_keys
  39
+    assert_equal @fixnums, @fixnums.dup.symbolize_keys!
  40
+  end
  41
+
36 42
   def test_stringify_keys
37 43
     assert_equal @strings, @symbols.stringify_keys
38 44
     assert_equal @strings, @strings.stringify_keys

0 notes on commit cfffedb

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