diff options
author | Matt Huggins <matt.huggins@gmail.com> | 2014-02-12 11:13:23 -0700 |
---|---|---|
committer | Matt Huggins <matt.huggins@gmail.com> | 2014-02-12 11:13:23 -0700 |
commit | 4302d2de94df61e5bb12e8076e1108f2dd641e17 (patch) | |
tree | 7096ff1acf53154b0412c2f0159810825fbe6e10 | |
parent | 3321005d0bd77e0811a2cea6dbbd302d8e86851f (diff) | |
download | hashie-4302d2de94df61e5bb12e8076e1108f2dd641e17.tar.gz |
Fix broken Hashie::Hash#to_hash(:symbolize_keys => true)
-rw-r--r-- | lib/hashie/hash.rb | 12 | ||||
-rw-r--r-- | spec/hashie/hash_spec.rb | 12 |
2 files changed, 18 insertions, 6 deletions
diff --git a/lib/hashie/hash.rb b/lib/hashie/hash.rb index c36bb91..35d660c 100644 --- a/lib/hashie/hash.rb +++ b/lib/hashie/hash.rb @@ -7,19 +7,19 @@ module Hashie class Hash < ::Hash include HashExtensions - # Converts a mash back to a hash (with stringified keys) + # Converts a mash back to a hash (with stringified or symbolized keys) def to_hash(options={}) out = {} keys.each do |k| + assignment_key = k.to_s + assignment_key = assignment_key.to_sym if options[:symbolize_keys] if self[k].is_a?(Array) - k = options[:symbolize_keys] ? k.to_sym : k.to_s - out[k] ||= [] + out[assignment_key] ||= [] self[k].each do |array_object| - out[k] << (Hash === array_object ? array_object.to_hash : array_object) + out[assignment_key] << (Hash === array_object ? array_object.to_hash : array_object) end else - k = options[:symbolize_keys] ? k.to_sym : k.to_s - out[k] = Hash === self[k] ? self[k].to_hash : self[k] + out[assignment_key] = Hash === self[k] ? self[k].to_hash : self[k] end end out diff --git a/spec/hashie/hash_spec.rb b/spec/hashie/hash_spec.rb index 30571d2..dd66e03 100644 --- a/spec/hashie/hash_spec.rb +++ b/spec/hashie/hash_spec.rb @@ -19,4 +19,16 @@ describe Hash do hash.should == Hashie::Hash[:a => "hey", 123 => "bob"] stringified_hash.should == Hashie::Hash["a" => "hey", "123" => "bob"] end + + it "#to_hash should return a hash with stringified keys" do + hash = Hashie::Hash["a" => "hey", 123 => "bob", "array" => [1, 2, 3]] + stringified_hash = hash.to_hash + stringified_hash.should == {"a" => "hey", "123" => "bob", "array" => [1, 2, 3]} + end + + it "#to_hash with symbolize_keys set to true should return a hash with symbolized keys" do + hash = Hashie::Hash["a" => "hey", 123 => "bob", "array" => [1, 2, 3]] + symbolized_hash = hash.to_hash(:symbolize_keys => true) + symbolized_hash.should == {:a => "hey", :"123" => "bob", :array => [1, 2, 3]} + end end
\ No newline at end of file |